-2

I have a class foo and bar. foo has a resource. When bar inherits from foo will it too have a deep copy of the memory? Or do I have to call the constructor of foo from bars default-constructor?

class foo
{
    char *item;
    public:
        foo() : item(new char[5]) {}
};

class bar : public foo {};
Me myself and I
  • 3,990
  • 1
  • 23
  • 47
  • The base class constructor will get called automatically and bar will have it's own 5 element array. Each instance of a bar is also a foo, so they each have their own arrays. Inheritance isn't "sharing" inheritance means it is itself and all the things it inherited. Put in some print statements in the constructor and you'll see it happen! – devshorts May 04 '13 at 12:07
  • What do you mean "deep copy"? Inheritance doesn't "copy" anything. – JBentley May 04 '13 at 12:26
  • In fact when you create an instance of `bar`, it can't share (or copy) "the" instance of `foo` - there may not be an instance of `foo` or there may be many. What is "shared" (at compile time) is a description or specification of a `foo` - a `bar` is like a `foo` except for these additions. –  May 04 '13 at 12:29

2 Answers2

5

The derived class has:

All the members of base class + the additional members in derived class

The order of calling of constructors is well defined, the base class constructor is always called before the derived constructor. Depending on how you use the member initialization list, either the default base class constructor or the parameterized version gets called.

But it's always Base class constructor followed by Derived class constructor. So not sure what your second Q is.


And just to be precise, Please remove the char * member and use std::string.

class foo
{
    //char *item;     ---------------->  Erroneous, difficult to handle
    std::string item;

    ....
Community
  • 1
  • 1
Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • The question was somewhat hard to understand, but I believe he may have deliberately chosen a `char*` for the example because it is a pointer, and he is confused about that aspect of it. That's my guess based on his use of the phrase "deep copy", anyway. – JBentley May 04 '13 at 12:28
  • @JBentley: Perhaps, or perhaps OP used `char *` and then got confused about its handling. I think it's best to mention the `std::string` anyhow. – Alok Save May 04 '13 at 12:30
1

You can see the memory layout as two sub-structs foo and bar gathered in one named bar.

Your bar class will look like that in memory:

*******
foo members:
-char* item (sizeof(char*)
*******
bar members:
*******

When you will create a new bar, you will reserve memory for these two structures. In your case this will reserve enough memory for the char* which is probably 4 bytes in case of a x86 compiler.

In a normal usecase, bar's constructor will contains everything related to bar's member. bar's constructor can also call "manually" foo's constructor within the initialization list or a raw call. Otherwise the compiler will call the foo's default constructor for you.

Jiwan
  • 731
  • 4
  • 11