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. virtual member?

virtual member?

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
6 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.
  • S Offline
    S Offline
    Stan the man
    wrote on last edited by
    #1

    Hi. I am not sure how to explain this but I want to declare a member in a base class. IN a class derived from this, I want to be able to be able to override the member with another class that is derived from that original member. Ex: class CBase { virtual CTest m_test; } class CBase1 : public CBase { virtual CTest1 m_test; } CTest1 is derived from CTest. I want to be able to almost use the same name in each class and just add more fuunctionality in the CBase1 class, but CBase will still be able to call fuctions from m_test. Then in CBase1, it can call additional functions available in CTest1... This is like a virtual function but I want to do it for a member variable or something. I am not sure if I explained it right. Anyone can give me an idea? Thanks. Stan the man

    C M M 3 Replies Last reply
    0
    • S Stan the man

      Hi. I am not sure how to explain this but I want to declare a member in a base class. IN a class derived from this, I want to be able to be able to override the member with another class that is derived from that original member. Ex: class CBase { virtual CTest m_test; } class CBase1 : public CBase { virtual CTest1 m_test; } CTest1 is derived from CTest. I want to be able to almost use the same name in each class and just add more fuunctionality in the CBase1 class, but CBase will still be able to call fuctions from m_test. Then in CBase1, it can call additional functions available in CTest1... This is like a virtual function but I want to do it for a member variable or something. I am not sure if I explained it right. Anyone can give me an idea? Thanks. Stan the man

      C Offline
      C Offline
      Chris Losinger
      wrote on last edited by
      #2

      there's no such thing as a virtual member. but, can you make CTest a base class of CTest1 ? then you can override members in CTest to add that additional functionality.

      image processing toolkits | batch image processing

      S 1 Reply Last reply
      0
      • S Stan the man

        Hi. I am not sure how to explain this but I want to declare a member in a base class. IN a class derived from this, I want to be able to be able to override the member with another class that is derived from that original member. Ex: class CBase { virtual CTest m_test; } class CBase1 : public CBase { virtual CTest1 m_test; } CTest1 is derived from CTest. I want to be able to almost use the same name in each class and just add more fuunctionality in the CBase1 class, but CBase will still be able to call fuctions from m_test. Then in CBase1, it can call additional functions available in CTest1... This is like a virtual function but I want to do it for a member variable or something. I am not sure if I explained it right. Anyone can give me an idea? Thanks. Stan the man

        M Offline
        M Offline
        Maximilien
        wrote on last edited by
        #3

        :| I'm not certain I understand what you want to achieve, but my gut feeling is that you cannot do that. maybe if you create a base class for CTest and CTest1 and keep a pointer to that class instead of CTest and CTest1 ?

        // not tested, and probably will not work as-is
        class CTestBase {};
        class CTest : public CTestBase {};
        class CTest1 : public CTestBase {};

        // and have

        class CBase
        {
        CBase() { m_pTest = new CTest;};
        CTestBase* m_pTest;
        };

        class Cbase1 : public CBase
        {
        CBase1() { m_pTest = new CTest1;};
        CTestBase* m_pTest;
        };

        but you will probably need to use something like CBase::m_pTest and CBase1::m_pTest to resolve potential name conflicts.

        Maximilien Lincourt Your Head A Splode - Strong Bad

        S 1 Reply Last reply
        0
        • C Chris Losinger

          there's no such thing as a virtual member. but, can you make CTest a base class of CTest1 ? then you can override members in CTest to add that additional functionality.

          image processing toolkits | batch image processing

          S Offline
          S Offline
          Stan the man
          wrote on last edited by
          #4

          Hi Chris. Yes, that is not a problem. The one thing is that if the (virtual) member/class is already declared in another base class. Then this base class I derive another class. But for this class, I need a slightly different functionality of that "virtual" member/class. But I do not want to disturb the function of the original (virtual) member/ class in that other base class. You know my meaning? I want to "replace" that class with another class which has identical functions but with some changes... Thanks. Stan the man

          1 Reply Last reply
          0
          • M Maximilien

            :| I'm not certain I understand what you want to achieve, but my gut feeling is that you cannot do that. maybe if you create a base class for CTest and CTest1 and keep a pointer to that class instead of CTest and CTest1 ?

            // not tested, and probably will not work as-is
            class CTestBase {};
            class CTest : public CTestBase {};
            class CTest1 : public CTestBase {};

            // and have

            class CBase
            {
            CBase() { m_pTest = new CTest;};
            CTestBase* m_pTest;
            };

            class Cbase1 : public CBase
            {
            CBase1() { m_pTest = new CTest1;};
            CTestBase* m_pTest;
            };

            but you will probably need to use something like CBase::m_pTest and CBase1::m_pTest to resolve potential name conflicts.

            Maximilien Lincourt Your Head A Splode - Strong Bad

            S Offline
            S Offline
            Stan the man
            wrote on last edited by
            #5

            HI Max. Thanks for the comment. I think I can use something like this. Don't know why I did not think of it. Thanks. Stan the man

            1 Reply Last reply
            0
            • S Stan the man

              Hi. I am not sure how to explain this but I want to declare a member in a base class. IN a class derived from this, I want to be able to be able to override the member with another class that is derived from that original member. Ex: class CBase { virtual CTest m_test; } class CBase1 : public CBase { virtual CTest1 m_test; } CTest1 is derived from CTest. I want to be able to almost use the same name in each class and just add more fuunctionality in the CBase1 class, but CBase will still be able to call fuctions from m_test. Then in CBase1, it can call additional functions available in CTest1... This is like a virtual function but I want to do it for a member variable or something. I am not sure if I explained it right. Anyone can give me an idea? Thanks. Stan the man

              M Offline
              M Offline
              Matthew Faithfull
              wrote on last edited by
              #6

              I think there's a bit of an OO design issue going on here. Perhaps you need some data hiding... class CBase { public: virtual int AccessorFunctionThatDealsWithTest(); private: CTest m_test; }; class CDerived1 : public CBase { public: int AccessorFunctionThatDealsWithTest();//override of CBase virtual function private: CSomeOtherTest m_test; }; If you do this then it matters not if CTest and CSomeOtherTest are related, they can be but the 'user' of CBase:CDerived1 hierarchy doesn't have to know or care because the AccessorFunctionThatDealsWithTest() interface is the same even though its implementation might be entirely different. Now that makes me :-D

              Nothing is exactly what it seems but everything with seems can be unpicked.

              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