Sign in to follow this  
Boldt

c++, inheritance and constructors

Recommended Posts

Hi Guys Assume a have a base class like class A { A(){ ... } }; and a inherited class like class B : public A { B() { ... } B( const B& ){ ... } } what are good practice for calling constructors in A from the constructors in B. Will the object created in B by the copy constructor also call the copy constructor in A by default or do I have to do this myself? Thanks Niels

Share this post


Link to post
Share on other sites
I think this code is same below..
----

class B : public A {

B() { ... }
B( const B& ){ ... }

}

----

class B : public A {

B() : A() { ... }
B( const B& ) : A() { ... }

}

----

Share this post


Link to post
Share on other sites
When you construct an object in C++ all of its base class subobjects are constructed first. The default copy constructor and copy assignment operator will automatically copy the base class parts of the object as well, although if you provide an implementation of the copy assignment operator you will have to explicitly call the base class copy assignment operator yourself. The destructor will automatically destroy the base class subobjects. The only other thing to watch out for is if you ever try to delete a dynamically allocated Derived class object via a pointer to Base class. In this case if the Base class does not have a virtual destructor the Derived class destructor will not be called and your object will not be correctly destroyed:
#include <iostream>

class Base
{

public:

Base()
{
std::cout << "Base::Base()\n";
}

Base(Base const & base)
{
std::cout << "Base::Base(Base const &)\n";
}

~Base()
{
std::cout << "Base::~Base()\n";
}

Base & operator=(Base const & base)
{
std::cout << "Base::operator=(Base const &)\n";
return *this;
}

};

class Derived1
:
public Base
{
};

class Derived2
:
public Base
{

public:

Derived2()
{
std::cout << "Derived2::Derived2()\n";
}

Derived2(Derived2 const & derived)
{
std::cout << "Derived2::Derived2(Derived2 const &)\n";
}

~Derived2()
{
std::cout << "Derived2::~Derived2()\n";
}

Derived2 & operator=(Derived2 const & derived)
{
std::cout << "Derived2::operator=(Derived2 const &)\n";
// must explicitly call base class copy assignment operator
Base::operator=(derived);
return *this;
}

};

int main()
{
std::cout << "Base:\n\n";
{
Base base; // calls Base::Base()
Base base2(base); // calls Base::Base(Base const &)
base = base2; // calls Base::operator=(Base const &)
} // two calls to Base::~Base()

std::cout << "\nDerived1:\n\n";
{
Derived1 derived; // calls Derived1::Derived1(), which calls Base::Base()
Derived1 derived2(derived); // calls Derived1::Derived1(Derived1 const &), which calls Base::Base(Base const &)
derived = derived2; // calls Derived1::operator=(Derived1 const &), which calls Base::operator=(Base const &)
} // two calls to Derived1::~Derived1(), both of which call Base::~Base();

std::cout << "\nDerived2:\n\n";
{
Derived2 derived; // calls Derived2::Derived2(), which calls Base::Base()
Derived2 derived2(derived); // calls Derived2::Derived1(Derived2 const &), which calls Base::Base(Base const &)
derived = derived2; // calls Derived2::operator=(Derived2 const &), which calls Base::operator=(Base const &)
} // two calls to Derived2::~Derived2(), both of which call Base::~Base();

std::cout << "\nDynamically allocated Derived2:\n\n";
{
Base * base = new Derived2(); // calls Derived1::Derived1(), which calls Base::Base()
delete base; // Base::~Base() is not virtual, so this incorrectly calls Base::~Base() only, and not Derived1::~Derived1()
}
}

Enigma

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this