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. if parent and child classes declare the same static member variable ?

if parent and child classes declare the same static member variable ?

Scheduled Pinned Locked Moved C / C++ / MFC
question
4 Posts 3 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
    amistry_petlad
    wrote on last edited by
    #1

    then what will be the following is true for that instance 1)all instances of the parent class will reference one variable and each instance of the child class would reference its own separate variable 2)all instances of the parent class would reference one variable and all instance of the child class would reference another variable. 3)All instance of the parent and the child classes would reference the same variable. 4)Each instance of the parent and the child classes would reference its own separate variable. 5)None of the above I want to know answer with some detail, if anybody could have . Thanks in advance

    CPalliniC 1 Reply Last reply
    0
    • A amistry_petlad

      then what will be the following is true for that instance 1)all instances of the parent class will reference one variable and each instance of the child class would reference its own separate variable 2)all instances of the parent class would reference one variable and all instance of the child class would reference another variable. 3)All instance of the parent and the child classes would reference the same variable. 4)Each instance of the parent and the child classes would reference its own separate variable. 5)None of the above I want to know answer with some detail, if anybody could have . Thanks in advance

      CPalliniC Offline
      CPalliniC Offline
      CPallini
      wrote on last edited by
      #2

      Sometimes a little test is better than a thousand words: (assuming 'parent' is base class and 'child' is derived one)

      #include using namespace std;

      class A
      {
      public:
      static int alpha;

      };

      class AA: public A
      {
      public:
      static int alpha;
      };

      int A::alpha = 1;
      int AA::alpha = 5;

      void main()
      {

      int i;
      const int N=5;
      A a[N];
      AA aa[N];

      for (i=0; i<n;> {
      cout << "a[" << i << "].alpha = " << a[i].alpha << endl;
      cout << "aa[" << i << "].alpha = " << aa[i].alpha << endl;
      }
      }

      The output is:

      a[0].alpha = 1
      aa[0].alpha = 5
      a[1].alpha = 1
      aa[1].alpha = 5
      a[2].alpha = 1
      aa[2].alpha = 5
      a[3].alpha = 1
      aa[3].alpha = 5
      a[4].alpha = 1
      aa[4].alpha = 5

      Personally, I'll bet on point (2). ;)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
      [My articles]

      In testa che avete, signor di Ceprano?

      J 1 Reply Last reply
      0
      • CPalliniC CPallini

        Sometimes a little test is better than a thousand words: (assuming 'parent' is base class and 'child' is derived one)

        #include using namespace std;

        class A
        {
        public:
        static int alpha;

        };

        class AA: public A
        {
        public:
        static int alpha;
        };

        int A::alpha = 1;
        int AA::alpha = 5;

        void main()
        {

        int i;
        const int N=5;
        A a[N];
        AA aa[N];

        for (i=0; i<n;> {
        cout << "a[" << i << "].alpha = " << a[i].alpha << endl;
        cout << "aa[" << i << "].alpha = " << aa[i].alpha << endl;
        }
        }

        The output is:

        a[0].alpha = 1
        aa[0].alpha = 5
        a[1].alpha = 1
        aa[1].alpha = 5
        a[2].alpha = 1
        aa[2].alpha = 5
        a[3].alpha = 1
        aa[3].alpha = 5
        a[4].alpha = 1
        aa[4].alpha = 5

        Personally, I'll bet on point (2). ;)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
        [My articles]

        J Offline
        J Offline
        Joe Woodbury
        wrote on last edited by
        #3

        What is this? Actually writing code to see how C++ works? Why, that's revolutionary.

        Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

        CPalliniC 1 Reply Last reply
        0
        • J Joe Woodbury

          What is this? Actually writing code to see how C++ works? Why, that's revolutionary.

          Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

          CPalliniC Offline
          CPalliniC Offline
          CPallini
          wrote on last edited by
          #4

          Joe Woodbury wrote:

          Why, that's revolutionary.

          Yes, since about XVII century. :-D

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
          [My articles]

          In testa che avete, signor di Ceprano?

          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