How to Use Base Class'S Constructors and Assignment Operator in C++

How to use base class's constructors and assignment operator in C++?

You can explicitly call constructors and assignment operators:

class Base {
//...
public:
Base(const Base&) { /*...*/ }
Base& operator=(const Base&) { /*...*/ }
};

class Derived : public Base
{
int additional_;
public:
Derived(const Derived& d)
: Base(d) // dispatch to base copy constructor
, additional_(d.additional_)
{
}

Derived& operator=(const Derived& d)
{
Base::operator=(d);
additional_ = d.additional_;
return *this;
}
};

The interesting thing is that this works even if you didn't explicitly define these functions (it then uses the compiler generated functions).

class ImplicitBase { 
int value_;
// No operator=() defined
};

class Derived : public ImplicitBase {
const char* name_;
public:
Derived& operator=(const Derived& d)
{
ImplicitBase::operator=(d); // Call compiler generated operator=
name_ = strdup(d.name_);
return *this;
}
};

Calling copy and assignment operators from base class to create inherited class instances in C++

Yes, you would have to define something like that.

B(const A& other);

This would allow constructing B out of A. This would also allow assigning A to B by way of implicitly converting A to B and then assigning. So that alone should suffice. But you get an extra copy.

B& operator=(const A& other);

This makes assigning A to B more efficient since you avoid the extra copy of the temporary B. This should also allow assigning things that can be implicitly converted to A like:

B b = 1;

If you don't want that you might have to add some explicit. Did C++98 have explicit? That is so last millenium.

Note: In modern C++ this would be more efficient because of copy elision and because you could use move semantic and perfect forwarding references.

Copy assignment operator for derived class

You can check this stackoverflow thread (calling operators of base class... safe?) on how to use operator = from base class.

What I don't understand is why you are using swap. You should instead do something like this:

B& B::operator=(const B& other) {
A::operator = (other);
this->m_member = other.m_member;
return *this;
}

How to call derived assignment operator from base class?

The problem is that your B::operator= does not override the one in A. Change it to

virtual A& operator=(const A& other)  { cout << "assigned b" << endl; return *this;}

and it will work. Also, try to use the override keyword when overriding member functions (requires C++11). The code won't compile if you don't override. In your case, it would have caught your mistake

error: 'virtual B& B::operator=(const B&)' marked 'override', but does not override

PS: you were probably thinking about covariant return types. In order for it to work, the signature of your function has to be the same, except the return type. E.g., this will work:

virtual B& operator=(const A& other)  { cout << "assigned b" << endl; return *this;}

C++ assignment operator implementation on derived class

Like this:

Derived &operator=(const Derived &right )
{
if (&right == this) { return *this; } // prevent assigning to self
Base::operator=(right);
delete derivedMember; // remember to release any assigned memory (assumes derivedMember is assigned a default of nullPtr)
derivedMember = new double( *(right.derivedMember) );
return *this;
}

Assigning base class members in copy assignment operator

In the general case, you do that either by explicitly calling operator= for the base class subobject, or by using the copy&swap idiom, if available:

//explicit call to base operator=
Derived& operator=(const Derived& rhs)
{
Base::operator=(rhs); //
derivedInt = rhs.derivedInt;
return *this;
}


//or copy&swap:
Derived& operator=(const Derived& rhs)
{
Derived tmp(rhs); //copy
tmp.swap(*this);
return *this;
}

void swap(Derived& other)
{
Base::swap(other);
std::swap(derivedInt,other.derivedInt);
}

Update: since your base class is not meant to be copy-assigned, your derived class should not be copy-assigned either. There are some cases where classes contain noncopyable parts but are perfectly copyable. In those cases the noncopyable part is something that does not directly contribute to the class objects' state, e.g. a unique ID. Then those parts will normally not be changed during assignment. However, in those cases the noncopyable parts should not be contained by inheritance but rather by aggregation.

Shortly said: Iheritance means "is-A" relationship. Your Base cannot be copied. Your Derived is a Base. Thus your Derived cannot be copied either.

Assignment operator and copy constructor for class containing base class pointer to derived templated class

You should look at the clone pattern.

http://en.wikipedia.org/wiki/Cloning_(programming)

What you do is add a pure abstract member function to FieldBase which is defined in the most derived types (Field).

So:

virtual FieldBase* clone() const = 0; //! This goes in FieldBase

FieldBase* clone() const { return new Field<T>(m_value); } //! This goes in Field

Then in the copy constructor you iterate over the map and clone the underlying values before inserting them in the new instance's map.

Something like this:

Store(const Store& other)
{
typedef std::map<std::string, FieldBase*> StoreMap;
for (StoreMap::const_iterator it(other.m_data.begin()); it != other.m_data.end(); ++it)
m_data.insert(std::make_pair(it->first, it->second->clone()));
}


Related Topics



Leave a reply



Submit