Is the Parent Instance Same For All Childs?
-
Hi all. Suppose a
BaseClass
has two childs. Now when the childrens' constructors are called, the base class' constructor is also called. Here is the scenario, in code.class BaseClass
{};class ChildOne : public BaseClass
{
public ChildOne(){} // base class constructor also called here
};Class ChildTwo : public BaseClass
{
public ChildTwo(){} // base class constructor also called here
};Do the childs share the same parent instance? I wanted to know a little more deep information about
BaseClass
instance and the two child instances. ThanksThis world is going to explode due to international politics, SOON.
-
Hi all. Suppose a
BaseClass
has two childs. Now when the childrens' constructors are called, the base class' constructor is also called. Here is the scenario, in code.class BaseClass
{};class ChildOne : public BaseClass
{
public ChildOne(){} // base class constructor also called here
};Class ChildTwo : public BaseClass
{
public ChildTwo(){} // base class constructor also called here
};Do the childs share the same parent instance? I wanted to know a little more deep information about
BaseClass
instance and the two child instances. ThanksThis world is going to explode due to international politics, SOON.
No, if you create two separate instances of the child class, then you also get two separate instances of the base class. But, of course, any static members are shared between all instances of the base and/or the child class.
The difficult we do right away... ...the impossible takes slightly longer.
-
Hi all. Suppose a
BaseClass
has two childs. Now when the childrens' constructors are called, the base class' constructor is also called. Here is the scenario, in code.class BaseClass
{};class ChildOne : public BaseClass
{
public ChildOne(){} // base class constructor also called here
};Class ChildTwo : public BaseClass
{
public ChildTwo(){} // base class constructor also called here
};Do the childs share the same parent instance? I wanted to know a little more deep information about
BaseClass
instance and the two child instances. ThanksThis world is going to explode due to international politics, SOON.
If you mean that base class have same values whenever an object of its child classes are made then yes same class have 'same instance'. But, it is false in the sense that each time an object is created, its new instance is created in memory. So, when you are creating the instance of a child class, its respective base class' instance is also created in memory. And if you are creating two instances of two child classes derived from same base class, then two different instances of base class are also getting created in memory against both child objects.
-
Hi all. Suppose a
BaseClass
has two childs. Now when the childrens' constructors are called, the base class' constructor is also called. Here is the scenario, in code.class BaseClass
{};class ChildOne : public BaseClass
{
public ChildOne(){} // base class constructor also called here
};Class ChildTwo : public BaseClass
{
public ChildTwo(){} // base class constructor also called here
};Do the childs share the same parent instance? I wanted to know a little more deep information about
BaseClass
instance and the two child instances. ThanksThis world is going to explode due to international politics, SOON.
An instance of a class is created when memory is allocated for the class. In the code that you've shown, there is no instance created and no memory allocated. Refer to the code snippet below -
class BaseClass
{
int base;
};class ChildOne : public BaseClass
{
int childOne;
public ChildOne(){} // base class constructor also called here
};Class ChildTwo : public BaseClass
{
int childTwo;
public ChildTwo(){} // base class constructor also called here
};When an object of
ChildOne
is created, memory is allocated forint base;
andint childOne;
When an object ofChildTwo
is created, memory is allocated forint base;
andint childTwo;
Here you can see that memory forint base;
has been allocated twice, once for the instance ofChildOne
and once for the instance ofChildTwo
. Sobase
has separate memory and hence value for each instance.«_Superman_» _I love work. It gives me something to do between weekends.