views:

90

answers:

2

hey i am trying to understand how to overload the operator= when there is an inheritance with no Success. code example:

class Person 
{
    private:
            char* m_name;
            char* m_lastName;
    ..... 
    public:
            virtual Person& operator=(const Person& other);
};
/********************/
cpp implementation
/********************/
#include "Person.h"
Person& Person:: operator=(const Person& other)
{
     if(this == &other)
         return *this;
     delete[] m_name;
     delete[] m_lastName;
     if (other.m_name!=NULL)
     {
         m_name = new char[strlen (other.m_name)+1];
         strcpy(m_name,other.m_name);
     }
     if (other.m_lastName!=NULL)
     {
         m_lastName = new char[strlen (other.m_lastName)+1];
         strcpy(m_lastName,other.m_lastName);
     }
     return (*this);
}

now lets say Student inherit from Person how should the = operator should be implemented i think it should be like the following please correct me cause i am probably being wrong:

#include "Person.h"
class Student : public Person
{
    private:
            char* m_collageName;
    ..... 
    public:
            virtual Person& operator=(const Person& other);
};
/********************/
cpp implementation
/********************/
#include "Student.h"
Person& Student:: operator=(const Person& other)
{
     if(this == &other)
         return *this;
     Person::operator=(other);
     delete[] m_collage;
     if ((Student)other.m_collageName != NULL)
     {
        m_collageName = new char[strlen((Student)other.m_collageName)+1];
        strcpy(m_collageName,(Student)other.m_collageName);
     }
     return (*this);
}

Thanks alot in advance much appriciate it.

+9  A: 

A virtual assignment operator seems an abomination to me. Assignment is for value types, while virtual is for polymorphic types. And these two are pretty much on opposite ends of the spectrum.

Imagine, for a while, this code:

void foo(person& x, const person& y)
{
  x = y;
}

plus we're having this

class teacher : public person {...};
class student : public person {...};

and now we're calling this as:

teacher t;
student s;

foo(s,t);

What should that do? Turn a student into a teacher? How so?

sbi
what would u suggest ?
Nadav Stern
@Nadav: I suggest that you decide whether you're making a value type or a polymorphic class hierarchy. You can't have both.
sbi
+1 for `Turn a student into a teacher?` otherwise i didn't properly understand value type vs polymorphic type.
Donotalo
@Donotalo: Basically, value types are types that behave similar to the built-in types: you can freely copy and assign them; if they are small enough, you might want to pass them per copy; they are not polymorphic; their dynamic type is the same as their static type... `int`, `std::string`, and `std::complex<int>` are all typical value types, whereas `std::istream` is a classic polymorphic type.
sbi
+3  A: 

Your implementation is not typesafe, because I can write:

Student s;
Person p;
s = p;

And it will compile the assignment successfully, but will result in U.B. (likely segfault or garbage read) at runtime, because you downcast the right argument of operator= from Person to Student, while it is not one.

More generally, a virtual assignment operator doesn't really make sense, since you'll have to define assignment from Person (rather than a specific subclass) in all derived classes, and it is very unlikely that it is a meaningful operation for them.

Pavel Minaev
and please use std::string
pm100
i am using char* just for learning how the mechanism work otherwise i dont really need to override the operator
Nadav Stern
and what is your suggestion for me ... using friend ?
Nadav Stern
@Nadav: re "...just for learning how the mechanism work...". Well, then be told that you got it completely wrong even in the few lines of code you showed. What happens with your assignment logic when `new` throws an exception? Or when `other.m_name` is `NULL`? And did you see you're assigning `m_name` twice and `m_lastName` not at all? Assignment should be done using the [copy and swap idiom](http://stackoverflow.com/questions/3279543/). And a signle object shouldn't try to (manually) manage more than one resource. Yours tries to manage two and fails.
sbi
well i wrote twice an allocation to m_name by a silly copy paste mistake ... sorry about that
Nadav Stern