C++ inheritance and copy constructor

Hello everybody ๐Ÿ™‚

While working on upcoming 1.05 patch we encountered a small problem, which actually is quite interesting – that’s why we want to share it with you :).

We needed to put some data into STL containers – in many cases this gives good speedup and makes our job much simpler – containers can automatically manage data that is inside them – taking care of memory allocation and freeing.
So, we started changing some game classes to the form which allows them to be inside STL containers – we needed copy constructors for classes which had pointer data that needed to be allocated via new instruction.
Everything run smoothly to the point when we had to cope withย  copy constructor in classes with inheritanceย  – how to explicitly call copy constructor of parent class? Many of you might think that it’s called automatically via children copy constructor, but, it’s not. Let’s look at the sourcecode:

class Parent
Parent() {}
Parent(const Parent& p) {} // copy constructor

class Children
Children() {}
Children(const Children& c) {}

Our problem was to call Parent copy constructor when Children copy constructor was invoked. After spending some time looking for solution on the Internet, we found it:

Children(const Children& c) : Parent(c) {}

At first it looks a bit strange. We call constructor of Parent class giving it the Children object. But, when we look at it more carefully, it will be clear for us. Children object is inheriting from Parent. This means that it contains Parent information + Children information – it’s Parent object inside Children. When we write: Parent(Children) we invoke copy constructor of Parent class with up-hierarchy casting – we cast Children object up, making it Parent (as we said earlier, it contains Parent information). Copy constructor of Parent takes needed information from casted object and… voila, it works ๐Ÿ™‚
That would be all for today,
have a nice weekend ๐Ÿ™‚

This entry was posted in C++, Game aspects, General and tagged , , . Bookmark the permalink.

6 Responses to C++ inheritance and copy constructor

  1. Inner Circle says:

    And how does that affect the game? Speed Optimization?

    Sorry, but i find this article hard to understand. It would be next time to write articles in a way everyone can understand…

    anyway, thanks for the Article

    • conio says:

      Hi, thank you for your comment:)
      Well, this article, and this blog in general, is not only about the game, but also about the tools we use to create it. So, we want to share our programming experiences with community, so that you can see how actually game development looks like. How this thing affect game? Well, copying constructor is something that is needed in some special cases. We wanted to use ‘special’ set of tools, called containers, which provide in some cases nice speedup. But, to be allowed to use them we need our classes to have copy constructors. This article actually tells about some special circumstances, when we have a class which uses inheritance. Don’t worry, next articles will be more game related, so not only programmers will be able to understand them ๐Ÿ™‚ Keep in touch with us!:)

  2. Raht says:

    C++ is so high and pro, but in my Python world calling parent constructor with child object as argument is an everyday practice ๐Ÿ˜‰

    Just wanted to let you know that there are some gamers (me) able to understand this post ๐Ÿ˜€

  3. Pingback: Alexander

  4. Pingback: harvey

  5. Pingback: Derrick

Leave a Reply