Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Is the Parent Instance Same For All Childs?

Is the Parent Instance Same For All Childs?

Scheduled Pinned Locked Moved C / C++ / MFC
question
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AmbiguousName
    wrote on last edited by
    #1

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

    This world is going to explode due to international politics, SOON.

    Richard Andrew x64R S _ 3 Replies Last reply
    0
    • A AmbiguousName

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

      This world is going to explode due to international politics, SOON.

      Richard Andrew x64R Offline
      Richard Andrew x64R Offline
      Richard Andrew x64
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • A AmbiguousName

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

        This world is going to explode due to international politics, SOON.

        S Offline
        S Offline
        SaqibRasheed
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • A AmbiguousName

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

          This world is going to explode due to international politics, SOON.

          _ Offline
          _ Offline
          _Superman_
          wrote on last edited by
          #4

          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 for int base; and int childOne; When an object of ChildTwo is created, memory is allocated for int base; and int childTwo; Here you can see that memory for int base; has been allocated twice, once for the instance of ChildOne and once for the instance of ChildTwo. So base has separate memory and hence value for each instance.

          «_Superman_»  _I love work. It gives me something to do between weekends.

          _Microsoft MVP (Visual C++) (October 2009 - September 2013)

          Polymorphism in C

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups