I was wondering how should I (or can I? does it make any sense?) overload the assignment operator when working with inheritance and upcasting?
Let's say we have Base class and Derived class (inherited from Base). If i have something like:
/// supose we have one overloaded assignment operator in Base like Base& operator=(const Base&) and
///one in Derived like Derived& operator=(const Derived&)...
Base* a, *b;
Derived c,d;
a = &c;
b = &d;
*a = *b /// this will call the function in Base
If that calls the Base function, why should I overload "=" again in Derived? Is overloading assignment operator in Derived necessary only for working directly with objects, not upcasting (pointers) ?
Here's some code that hopefully helps you out.
Derived Does Not Own A Dynamic Resource
The Base class holds a dynamic resource, so we are required to follow the Rule of 3 (should be 5, but kept it at 3 for brevity). I did so by utilizing the copy/swap idiom.
I then derive Derived from Base. It does not hold a dynamic resource, so I follow the Rule of 0 and don't provide a custom copy constructor, destructor, assignment operator, move constructor, or move assignment.
You can see from the output that the Base portion of the Derived objects are able to deep-copy themsleves just fine, and the Derived-only portion gets by just fine with shallow copy. The final output leaks memory, but I chose to do that to demonstrate an actual overwrite using a pointer to Base.
#include <iostream>
class Base {
private:
int* m = nullptr;
public:
Base() = default;
Base(int v) : m(new int(v)) {}
Base(const Base& other) : m(new int(*(other.m))) {}
virtual ~Base() {
delete m;
m = nullptr;
}
Base& operator=(Base other) {
swap(*this, other);
return *this;
}
friend void swap(Base& lhs, Base& rhs) {
using std::swap;
swap(lhs.m, rhs.m);
}
virtual void print() const {
std::cout << "Address: " << m << "\nValue: " << *m << '\n';
}
};
class Derived : public Base {
private:
double x = 0.0;
public:
Derived() = default;
Derived(double v) : Base(), x(v) {}
Derived(int i, double v) : Base(i), x(v) {}
void print() const override {
std::cout << "Address: " << &x << "\nValue: " << x << '\n';
Base::print();
}
};
int main() {
std::cout << "A\n";
Base* a = new Derived(5, 3.14);
a->print();
std::cout << "\nB\n";
Derived b = *(dynamic_cast<Derived*>(a)); // Copy ctor
b.print();
std::cout << "\nC\n";
Derived c;
c = b;
c.print();
std::cout << "\nReplace A (This leaks)\n";
a = new Derived(7, 9.81);
a->print();
}
Output:
A
Address: 0x21712d0
Value: 3.14
Address: 0x21712e0
Value: 5
B
Address: 0x7ffdd62964c8
Value: 3.14
Address: 0x2171300
Value: 5
C
Address: 0x7ffdd62964b0
Value: 3.14
Address: 0x2171320
Value: 5
Replace A (This leaks)
Address: 0x2171350
Value: 9.81
Address: 0x2171360
Value: 7
Derived Owns A Dynamic Resource
Now, Derived has a dynamic of its own to manage. So I follow the Rule of 3 and provide a copy constructor, destructor, and assignment operator overload. You'll notice that the assignment operator looks identical to the Base version; this is intentional.
It's because I'm using the copy/swap idiom. So in the swap() function for Derived, I add a step where it swaps the Base portion, then swaps the Derived portion. I do this by invoking the Base swap() function through the dynamic cast.
And we can again observe that all objects have their own memory for each dynamically allocated piece.
#include <iostream>
class Base {
private:
int* m = nullptr;
public:
Base() = default;
Base(int v) : m(new int(v)) {}
Base(const Base& other) : m(new int(*(other.m))) {}
virtual ~Base() {
delete m;
m = nullptr;
}
Base& operator=(Base other) {
swap(*this, other);
return *this;
}
friend void swap(Base& lhs, Base& rhs) {
using std::swap;
swap(lhs.m, rhs.m);
}
virtual void print() const {
std::cout << "Address: " << m << "\nValue: " << *m << '\n';
}
};
class Derived : public Base {
private:
double* x = nullptr;
public:
Derived() = default;
Derived(double v) : Base(), x(new double(v)) {}
Derived(int i, double v) : Base(i), x(new double(v)) {}
Derived(const Derived& other) : Base(other), x(new double(*(other.x))) {}
~Derived() {
delete x;
x = nullptr;
}
Derived& operator=(Derived other) {
swap(*this, other);
return *this;
}
friend void swap(Derived& lhs, Derived& rhs) {
using std::swap;
swap(dynamic_cast<Base&>(lhs), dynamic_cast<Base&>(rhs));
swap(lhs.x, rhs.x);
}
void print() const override {
std::cout << "Address: " << &x << "\nValue: " << *x << '\n';
Base::print();
}
};
int main() {
std::cout << "A\n";
Base* a = new Derived(5, 3.14);
a->print();
std::cout << "\nB\n";
Derived b = *(dynamic_cast<Derived*>(a)); // Copy ctor
b.print();
std::cout << "\nC\n";
Derived c;
c = b;
c.print();
std::cout << "\nReplace A (This leaks)\n";
a = new Derived(7, 9.81);
a->print();
}
Output:
A
Address: 0x14812d0
Value: 3.14
Address: 0x14812e0
Value: 5
B
Address: 0x7fffe89e8d68
Value: 3.14
Address: 0x1481320
Value: 5
C
Address: 0x7fffe89e8d50
Value: 3.14
Address: 0x1481360
Value: 5
Replace A (This leaks)
Address: 0x14813b0
Value: 9.81
Address: 0x14813c0
Value: 7
Does the output of this example help clarify your question? You could always override the operator= in the derived class as follows:
#include <cstdio>
struct Base{
virtual ~Base() = default;
virtual void operator=(const Base&) {
std::printf("Base::=\n");
}
};
struct Derived: public Base {
void operator=(const Derived&) {
std::printf("Derived::=\n");
}
void operator=(const Base&) override{
std::printf("Derived::= Base\n");
}
};
int main() {
Base* a, *b;
Derived c,d;
a = &c;
b = &d;
*a = *b; //Dispatches the call to the derived class =
Base base;
Derived derived;
derived = base; //Usual case now after operator=(const Base&) in Derived
c = d; //Usual case
Base base1, base2;
base1 = base2; //Usual case
a = &base1;
b = &base2;
*a = *b; //Usual case
}
Output:
Derived::= Base
Derived::= Base
Derived::=
Base::=
Base::=
I'm struggled over some code where I don't know how to name it and how to solve it. I tried to reduce the code to the following example (so the example itself won't make sense, but it shows the problematic):
struct MyInterface {
virtual ~MyInterface() {
};
virtual void Output() = 0;
};
class A {
public:
MyInterface *myInterface;
A(MyInterface *myInterface) {
std::cout << "this in A constructor: " << this << std::endl;
this->myInterface = myInterface;
}
void CallA() {
this->myInterface->Output();
}
};
class B : public MyInterface, A {
public:
int v;
B(int v) : A(this) {
std::cout << "this in B constructor: " << this << std::endl;
this->v = v;
}
virtual void Output() override {
std::cout << "Whatever" << std::endl;
}
void CallB() {
std::cout << "this in CallB: " << this << std::endl;
this->CallA();
}
};
class Foo {
public:
B b;
Foo() : b(42) {
b = B(41); //This will make an "invalid" B:
//generates B on the Stack but assign the bytes to Foo.b (which is on the the heap)
//so b.myInterface will point to the stack
//after leaving this context b.other will be invalid
}
void Exec() {
b.CallB();
}
};
int main(int argc, char **args) {
Foo *foo = new Foo();
foo->Exec(); //Gives a segfault, because foo->b.myInterface is not valid
return 0;
}
First I thought it has something to do with the inheritance and its virtual methods. But I think the main problematic is the this pointer within the constructors.
So my questions: When b is constructed, the this pointer in the constructors points to the stack. Why doesn't show the this pointer to the target memory (in the heap)? No copy constructor is called - Why?
How can I Name this problem?
The copy constructor isn't called because you aren't creating a new object you are assigning to an existing object. This calls the assignment operator.
This is copy construction:
B b1(42); // construction
B b2(b1); // copy construction
B b3 = b1; // looks like assignment but is actually copy construction
This is assignment:
B b1(42); // construction
b1 = B(43); // b1 already exists we can't copy construct, construct a new object and assign to b1
You need to override the assignment operator:
class B
{
B& operator=(const B& other)
{
// fix references to this here
}
}
consider following code:
I know that when you create an object of class C first member variables will be constructed then order of construction will be abc and deconstruction CBA . now question is that if there is a way to call constructor of class C sooner than member variables ? to have order of cab and for deconstruction BAC
how i could change order that way first constructor of class be called then member variables.
#include <iostream>
#include <exception>
class A {
public:
A() {
std::cout << 'a';
}
~A() { std::cout << 'A'; }
};
class B {
public:
B() { std::cout << 'b'; }
~B() { std::cout << 'B'; }
};
class C {
public:
C() {
std::cout << 'c';
}
~C() { std::cout << 'C'; }
A m_a ;
B m_b;
};
void foo() { C c; }
int main() {
try {
foo();
}
catch (std::exception &) {
std::cout << "catch";
}
}
More of a workaround than actually breaking construction order (which isn't possible). Store the members by smart pointer.
class C {
public:
C() {
std::cout << 'c';
m_a = std::make_unique<A>();
m_b = std::make_unique<B>();
}
~C() {
m_b.reset();
m_a.reset();
std::cout << 'C';
}
std::unique_ptr<A> m_a;
std::unique_ptr<B> m_b;
};
The instances of the actual objects you care about can now be created at the end of C's constructor. Of course, you pay for it by doing dynamic memory allocation.
Another solution can be to use aligned storage and placement new construction:
class C {
public:
C() {
std::cout << 'c';
new(&m_a) A;
new(&m_B) B;
}
~C() {
m_b.~B();
m_a.~A();
std::cout << 'C';
}
std::aligned_storage<sizeof(A), alignof(A)>::type m_a;
std::aligned_storage<sizeof(B), alignof(B)>::type m_b;
};
But either way you have to be very careful and follow to rule of three/five.
Or simply:
class C {
public:
A *m_a;
B *m_b;
C() {
std::cout << 'c';
m_a = new A;
m_b = new B;
}
~C() {
delete m_b;
delete m_a;
std::cout << 'C'; }
};
Use smart pointers to ensure exception safeness
I'm getting confused why p->a() is calling B::a()?. Is there a paragraph somewhere in the C++ documentation/standard that describes this behavior well?
#include <iostream>
using namespace std;
class A {
public:
A() { cout << "A ctor" << endl; a_instance = this; }
static A *get_instance() { return a_instance; }
static A *a_instance;
void virtual a() { cout << "From base class" << endl; }
};
class B : public A {
public:
B() { cout << "B ctor" << endl; b_instance = this; }
static B *get_instance() { return b_instance; }
static B *b_instance;
void virtual a() { cout << "From derived class" << endl; }
};
A *A::a_instance = 0;
B *B::b_instance = 0;
main()
{
cout << "Create A" << endl;
A ab;
cout << "Create B" << endl;
B abc;
B *ptr = B::get_instance();
A *p = A::get_instance();
cout << "Called from A object type" << endl;
if (p) {
p->a();
}
}
When you create the variable abc, A's constructor sets a_instance to that instance. Despite p being a pointer to an A, since the instance is pointing to a B, it's correctly calling B::a().
To fix this behaviour, you could use the following:
A* A::get_instance()
{
static A a;
return &a;
}
B* B::get_instance()
{
static B b;
return &b;
}
and remove all code that has to do with a_instance and b_instance.
The B constructor calls the A constructor first. That replaces the a_instance that you'd already created.
This chaining of constructors is well defined in the standard. The base is always called first, so that the derived constructor is guaranteed to be working on a valid base object.
What you are experiencing is caused by the design error, which is based on A's constructor initializing the static member using this. Body of this constructor is invoked not only when you are creating the instance of A but also when you are creating the instance of any of its derived classes:
A() { /* ... */ a_instance = this; }
So when you create an instance of type B, before the body of B's constructor is executed, the body of A's constructor is executed at first - this overwrites member a_instance with this within a context of instance of type B, i.e. it makes a_instance to point to this new instance of type B.
What you could do is to place a static variable inside of the getInstance method:
class A
{
public:
static A* getInstance() {
static A s; // <-- instantiated upon first call
return &s;
}
void virtual foo() { std::cout << "From base class" << std::endl; }
protected:
A() { } // <-- protected constructor
~A() { }
A(const A&); // <-- protected copy constructor
A& operator=(const A&); // <-- protected assignment operator
};
then B:
class B : public A
{
public:
static B* getInstance() {
static B s; // <-- instantiated upon first call
return &s;
}
void virtual foo() { std::cout << "From derived class" << std::endl; }
protected:
B() { } // <-- protected constructor
~B() { }
B(const B&); // <-- protected copy constructor
B& operator=(const B&); // <-- protected assignment operator
};
and possible usage:
int main() {
A::getInstance()->foo();
B::getInstance()->foo();
}
that outputs:
From base class
From derived class
B constructor invokes A constructor...
What's a good strategy for preventing redundant function calls in the presence of diamond inheritance? Specifically, say we have a program:
#include <iostream>
struct A {
int a;
A(int a_) : a(a_) {}
virtual void print() {
std::cout << "a: " << a << std::endl;
}
};
struct B : virtual public A {
int b;
B(int a_,int b_) : A(a_), b(b_) {}
virtual void print() {
A::print();
std::cout << "b: " << b << std::endl;
}
};
struct C : virtual public A {
int c;
C(int a_,int c_) : A(a_), c(c_) {}
virtual void print() {
A::print();
std::cout << "c: " << c << std::endl;
}
};
struct D : public B,public C {
int d;
D(int a_,int b_,int c_,int d_) : A(a_), B(a_,b_), C(a_,c_), d(d_) {}
void print() {
B::print();
C::print();
std::cout << "d: " << d << std::endl;
}
};
int main() {
D d(1,2,3,4);
d.print();
}
When we call d.print(), we get:
a: 1
b: 2
a: 1
c: 3
d: 4
where a has been printed twice. Is there a good way to prevent this? Certainly, we could manually wire the connections with a code like:
#include <iostream>
struct A {
int a;
A(int a_) : a(a_) {}
virtual void print_() {
std::cout << "a: " << a << std::endl;
}
virtual void print() {
A::print_();
}
};
struct B : virtual public A {
int b;
B(int a_,int b_) : A(a_), b(b_) {}
virtual void print_() {
std::cout << "b: " << b << std::endl;
}
virtual void print() {
A::print_();
B::print_();
}
};
struct C : virtual public A {
int c;
C(int a_,int c_) : A(a_), c(c_) {}
virtual void print_() {
std::cout << "c: " << c << std::endl;
}
virtual void print() {
A::print_();
C::print_();
}
};
struct D : public B,public C {
int d;
D(int a_,int b_,int c_,int d_) : A(a_), B(a_,b_), C(a_,c_), d(d_) {}
virtual void print_() {
std::cout << "d: " << d << std::endl;
}
virtual void print() {
A::print_();
B::print_();
C::print_();
D::print_();
}
};
int main() {
D d(1,2,3,4);
d.print();
}
which correctly outputs
a: 1
b: 2
c: 3
d: 4
but I would like to know if there's a better way. In terms of where this arises, imagine a situation with the objects A, B, C, and D are complicated and need to be able to write themselves to disk. We only want to write the output code for each A, B, C, and D once and it's important that D not write information about A twice.
<---EDIT--->
Here's two more ways of fixing the problem, but they're still kind of obtuse. The first one is from Cristian and involves setting a flag on whether or not A has been printed
#include <iostream>
struct A {
int a;
bool have_printed;
A(int a_) : have_printed(false), a(a_) {}
virtual void print() {
if(have_printed) return;
std::cout << "a: " << a << std::endl;
have_printed=true;
}
void clear() {
have_printed=false;
}
};
struct B : virtual public A {
int b;
B(int a_,int b_) : A(a_), b(b_) {}
virtual void print() {
A::print();
std::cout << "b: " << b << std::endl;
}
};
struct C : virtual public A {
int c;
C(int a_,int c_) : A(a_), c(c_) {}
virtual void print() {
A::print();
std::cout << "c: " << c << std::endl;
}
};
struct D : public B,public C {
int d;
D(int a_,int b_,int c_,int d_) : A(a_), B(a_,b_), C(a_,c_), d(d_) {}
void print() {
B::print();
C::print();
std::cout << "d: " << d << std::endl;
}
};
int main() {
D d(1,2,3,4);
d.clear();
d.print();
}
This correctly outputs. A second way is more complicated, but may allow the structure to grow. Basically, we separate out the printer from the class and then register a list of printers inside each object. When we want to print, we iterate over the list of printers, which then gives us the correct output. I feel this uses too much machinery, but I'll include in case someone else gets a better idea:
// A simple unary function. Technically, the stl version doesn't require
// the operator
template <typename A,typename B>
struct unary {
virtual B operator () (A a) {};
};
struct A {
// Data
int a;
// A list of pointers to unary functions. We need pointers to unary
// functions rather than unary functions since we need the printer
// to be polymorphic.
std::list < unary<A*,void>* > printers;
A(int a_);
// We actually end up allocating memory for the printers, which is held
// internally. Here, we free that memory.
~A() {
for(std::list < unary<A*,void>* >::iterator printer
=printers.begin();
printer != printers.end();
printer++
)
delete (*printer);
}
private:
// Require for the dynamic cast used later
virtual void ___dummy() {};
};
// Prints out the data for A
struct A_Printer : public unary<A*,void>{
void operator () (A* a) {
std::cout << "a: " << a->a << std::endl;
}
};
// Adds the printer for a to the list of printers
A::A(int a_) : a(a_) {
printers.push_back(new A_Printer());
}
// Now that the structure is setup, we just need to define the data for b,
// it's printer, and then register the printer with the rest
struct B : virtual public A {
int b;
B(int a_,int b_);
};
struct B_Printer : public unary<A*,void>{
void operator () (A* b) {
std::cout << "b: " << dynamic_cast <B*>(b)->b << std::endl;
}
};
B::B(int a_,int b_) : A(a_), b(b_) {
printers.push_back(new B_Printer());
}
// See the discussion for B
struct C : virtual public A {
int c;
C(int a_,int c_);
};
struct C_Printer : public unary<A*,void>{
void operator () (A* c) {
std::cout << "c: " << dynamic_cast <C*>(c)->c << std::endl;
}
};
C::C(int a_,int c_) : A(a_), c(c_) {
printers.push_back(new C_Printer());
}
// See the discussion for B
struct D : public B,public C {
int d;
D(int a_,int b_,int c_,int d_);
};
struct D_Printer : public unary<A*,void>{
void operator () (A* d) {
std::cout << "d: " << dynamic_cast <D*>(d)->d << std::endl;
}
};
D::D(int a_,int b_,int c_,int d_) : A(a_), B(a_,b_), C(a_,c_), d(d_) {
printers.push_back(new D_Printer());
}
// This actually prints everything. Basically, we iterate over the printers
// and call each one in term on the input.
void print(A* a) {
for(std::list < unary<A*,void>* >::iterator printer
=a->printers.begin();
printer != a->printers.end();
printer++
)
(*(*printer))(a);
}
int main() {
D d(1,2,3,4);
// This should print 1,2,3,4
print(&d);
}
<---EDIT 2--->
tmpearce had a good idea to accumulate all of the information in a hash table prior to assembling it. In this way, it's possible to check if the individual information has been created yet and prevent redundancies. I think this is a good idea when the information can be assembled easily. If this is not the case, a slight variation may work, which combines the ideas of tmpearce and Cristian. Here, we pass around a set (or hashtable, or whatever) that keeps track of whether or not a function has been called. In this way, we can check whether or not some function has been computed. It doesn't require perpetual state, so it should be safe to call multiple times:
#include <iostream>
#include <set>
struct A {
int a;
A(int a_) : a(a_) {}
virtual void print_(std::set <std::string>& computed) {
if(computed.count("A") > 0) return;
computed.insert("A");
std::cout << "a: " << a << std::endl;
}
void print() {
std::set <std::string> computed;
print_(computed);
}
};
struct B : virtual public A {
int b;
B(int a_,int b_) : A(a_), b(b_) {}
virtual void print_(std::set <std::string>& computed) {
A::print_(computed);
if(computed.count("B") > 0) return;
computed.insert("B");
std::cout << "b: " << b << std::endl;
}
};
struct C : virtual public A {
int c;
C(int a_,int c_) : A(a_), c(c_) {}
virtual void print_(std::set <std::string>& computed) {
A::print_(computed);
if(computed.count("C") > 0) return;
computed.insert("C");
std::cout << "c: " << c << std::endl;
}
};
struct D : public B,public C {
int d;
D(int a_,int b_,int c_,int d_) : A(a_), B(a_,b_), C(a_,c_), d(d_) {}
virtual void print_(std::set <std::string>& computed) {
B::print_(computed);
C::print_(computed);
if(computed.count("D") > 0) return;
computed.insert("D");
std::cout << "d: " << d << std::endl;
}
};
int main() {
D d(1,2,3,4);
d.print();
}
In any case, I'll mark this problem off as solved. Though, I'd always like to hear additional answers.
My approach would sort of combine the ones you've mentioned. I'd make the virtual method do something a bit different:
class A
{
public:
virtual void getInfo(std::map<std::string,std::string>& output)
{
if(output.count("A") == 0)
{
output["A"] = "a: 1";
}
}
void print()
{
std::map<std::string,std::string> m;
getInfo(m); //virtual method (most derived will be called)
std::map<std::string,std::string>::iterator iter = m.begin();
for(; iter!=m.end(); ++iter)
{
std::cout<<iter->second();
}
}
};
class B : public A
{
virtual void getInfo(std::map<std::string,std::string>& output)
{
A::getInfo(output);
if(output.count("B") == 0)
{
output["B"] = "b: 2";
}
}
};
print is now a non-virtual method that uses getInfo to populate a container, then iterates over it display/save the output. Each class can thus check to make sure the container doesn't already contain the desired output for that level of the inheritance chain before writing and adding the string to the container.
I'd add a private flag to A struct (and to B and C if diamond extends beyond one level) and checking it & marking it as already traversed. This would also help in more complicated (nested) diamond patterns.
Like this:
struct A {
int a;
A(int a_) : a(a_) {traversed = false;}
virtual void print() {
if (traversed) return;
std::cout << "a: " << a << std::endl;
traversed = true;
}
private:
bool traversed;
};
Only one class constructs the virtual base (the most derived, D) so I would ensure only one class prints the A object, and like its construction, make it happen first (possibly important if you're writing objects to disk.)
You could add a void* argument to A's constructor and store it in a member of A. Each derived class would construct the virtual base as A(a, this).
Add a new member function to A, do_print(void*) and have every derived class call do_print(this) instead of A::print(). The do_print(void*) function compares its argument with the stored void* passed to the A ctor and only prints if it's the same. This relies on every derived class having a distinct address, which will be true if all the classes are non-empty, but if that assumption holds it ensures only the most derived object prints the virtual base.