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. C++ related issue

C++ related issue

Scheduled Pinned Locked Moved C / C++ / MFC
c++oophelpquestion
6 Posts 5 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.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    Friends, Suppose we pass an object of a certain class as a reference to two other classes. It means that the two classes have a "same" copy of an object. This is a type of "composition" Now insted of "composition", i want to get the same effect using "inheritance" OR i want my derived classes to inherit from "same" base class. Same base class means that the same base subobject. If one class make any change in the data of its parent, then the change should get reflected in the second class as its parent is same as that of first class. How is it possible ???

    D J 2 Replies Last reply
    0
    • L Lost User

      Friends, Suppose we pass an object of a certain class as a reference to two other classes. It means that the two classes have a "same" copy of an object. This is a type of "composition" Now insted of "composition", i want to get the same effect using "inheritance" OR i want my derived classes to inherit from "same" base class. Same base class means that the same base subobject. If one class make any change in the data of its parent, then the change should get reflected in the second class as its parent is same as that of first class. How is it possible ???

      D Offline
      D Offline
      dog_spawn
      wrote on last edited by
      #2

      What do you actually want to do? Are you a student quoting from your exercise or something? You should rephrase your question so it actually makes sense.

      L 1 Reply Last reply
      0
      • D dog_spawn

        What do you actually want to do? Are you a student quoting from your exercise or something? You should rephrase your question so it actually makes sense.

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Consider the following code:

        class Parent
        {
        public:
        SetValue(int a)
        {
        m_value = a;
        }

        private:

        int m\_value
        

        };

        class Brother:public Parent
        {
        brother()
        {
        SetValue(12)
        {
        };

        class Sister:public Parent
        {
        Sister()
        {
        SetValue(8)
        }
        };

        As can be seen above, the two classes "Brother" and "Sister" are derived from the same base class "Parent" Both the children classes have their own copy of "Parent". If the class "Brother" makes any change in the data member of "Parent", then the change will not effect the "Parent" of "Sister" as "Sister" has its own "Parent" What i want is that, both the children classes should share the "same" parent. And if "Brother" changes the data present in its "Parent", then the data of the "Parent" of "Sister" should also gets changed automatically as both "Brother" and "Sister" will have same subobject "Parent". How can i achieve this goal.

        D S 2 Replies Last reply
        0
        • L Lost User

          Consider the following code:

          class Parent
          {
          public:
          SetValue(int a)
          {
          m_value = a;
          }

          private:

          int m\_value
          

          };

          class Brother:public Parent
          {
          brother()
          {
          SetValue(12)
          {
          };

          class Sister:public Parent
          {
          Sister()
          {
          SetValue(8)
          }
          };

          As can be seen above, the two classes "Brother" and "Sister" are derived from the same base class "Parent" Both the children classes have their own copy of "Parent". If the class "Brother" makes any change in the data member of "Parent", then the change will not effect the "Parent" of "Sister" as "Sister" has its own "Parent" What i want is that, both the children classes should share the "same" parent. And if "Brother" changes the data present in its "Parent", then the data of the "Parent" of "Sister" should also gets changed automatically as both "Brother" and "Sister" will have same subobject "Parent". How can i achieve this goal.

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          Jamal Jamshed wrote: How can i achieve this goal. **static** int m_value;

          1 Reply Last reply
          0
          • L Lost User

            Friends, Suppose we pass an object of a certain class as a reference to two other classes. It means that the two classes have a "same" copy of an object. This is a type of "composition" Now insted of "composition", i want to get the same effect using "inheritance" OR i want my derived classes to inherit from "same" base class. Same base class means that the same base subobject. If one class make any change in the data of its parent, then the change should get reflected in the second class as its parent is same as that of first class. How is it possible ???

            J Offline
            J Offline
            John R Shaw
            wrote on last edited by
            #5
            1. Create a base class that all related classes are derived from. 2) Make any member variables that you wish to be common to all derived classes static members. 3) If you are going to destroy (free) any of the members in the destuctor then you need to add a static reference counter member variable that is incremented in the constructor and decremented in the destructor.

            // In header
            class MyClass
            {
            static int m_nRefCount;
            static int m_nMem1;
            static whatever* m_pWhatEver;
            ...................
            public: // constructors / destructors / accessors / manipulators / ect...
            ...................
            };

            // In module / code file

            int MyClass::m_nRefCount = 0;
            whatever MyClass::m_pWhatEver = NULL;

            MyClass::MyClass()
            {
            ++m_nRefCount;
            }

            MyClass::~MyClass()
            {
            if( --m_nRefCount < 1 )
            {
            if( m_pWhatEver is valid )
            delete m_pWhatEver;
            }
            }

            :-DHave a wonderful day. INTP

            1 Reply Last reply
            0
            • L Lost User

              Consider the following code:

              class Parent
              {
              public:
              SetValue(int a)
              {
              m_value = a;
              }

              private:

              int m\_value
              

              };

              class Brother:public Parent
              {
              brother()
              {
              SetValue(12)
              {
              };

              class Sister:public Parent
              {
              Sister()
              {
              SetValue(8)
              }
              };

              As can be seen above, the two classes "Brother" and "Sister" are derived from the same base class "Parent" Both the children classes have their own copy of "Parent". If the class "Brother" makes any change in the data member of "Parent", then the change will not effect the "Parent" of "Sister" as "Sister" has its own "Parent" What i want is that, both the children classes should share the "same" parent. And if "Brother" changes the data present in its "Parent", then the data of the "Parent" of "Sister" should also gets changed automatically as both "Brother" and "Sister" will have same subobject "Parent". How can i achieve this goal.

              S Offline
              S Offline
              Scott H Settlemier
              wrote on last edited by
              #6

              The magic words you are looking for are "virtual inheritance[^]."

              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