c++, inheritance and constructors

Started by
4 comments, last by Boldt 18 years, 6 months ago
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
Advertisement
Yes, it does that by itself, if you meant that the A in class B calls A's constructor?

:)


Each time a class is "spawned" it calls the constructor, same goes for destructors...
"Game Maker For Life, probably never professional thou." =)
Yeah, but I create an instance of B, so I'm basically wondering if the parts of A also will be constructed

Niels
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() { ... }

}

----
thank u
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
Thanks for the answers - they were most helpful

Niels

This topic is closed to new replies.

Advertisement