Jump to content
  • Advertisement
Sign in to follow this  
Boldt

c++, inheritance and constructors

This topic is 4697 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
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...

Share this post


Link to post
Share on other sites
Yeah, but I create an instance of B, so I'm basically wondering if the parts of A also will be constructed

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!