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

inheritance

Scheduled Pinned Locked Moved C / C++ / MFC
helpcomoopquestion
7 Posts 2 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.
  • K Offline
    K Offline
    khomeyni
    wrote on last edited by
    #1

    in the name of god hello anyone who is reading i have a problem please help me: in the code below is there any way which the class DerivedContainer in every where treat with DrivedMember as m_instance without static_cast? as i have no well access to net if it is possible please send me the answer to sajadsadeghy@gmail.com or if not also thanks. #include "stdafx.h" class BaseMember { public: int testBase; BaseMember() { } }; class DerivedMemer:public BaseMember { public: DerivedMemer() { } void SomeFunction() { } int testDerived; }; class BaseContainer { public: BaseMember* m_instance; BaseContainer() { m_instance=new BaseMember(); } }; class DerivedContainer:public BaseContainer { public: DerivedContainer() { m_instance=new DerivedMemer(); } void TestFunction() { DerivedMemer* pIs=static_cast(this->m_instance); pIs->testDerived=100; } }; int _tmain(int argc, _TCHAR* argv[]) { DerivedContainer der; der.TestFunction(); return 0; } thanks in advance . valhamdolelah

    D 1 Reply Last reply
    0
    • K khomeyni

      in the name of god hello anyone who is reading i have a problem please help me: in the code below is there any way which the class DerivedContainer in every where treat with DrivedMember as m_instance without static_cast? as i have no well access to net if it is possible please send me the answer to sajadsadeghy@gmail.com or if not also thanks. #include "stdafx.h" class BaseMember { public: int testBase; BaseMember() { } }; class DerivedMemer:public BaseMember { public: DerivedMemer() { } void SomeFunction() { } int testDerived; }; class BaseContainer { public: BaseMember* m_instance; BaseContainer() { m_instance=new BaseMember(); } }; class DerivedContainer:public BaseContainer { public: DerivedContainer() { m_instance=new DerivedMemer(); } void TestFunction() { DerivedMemer* pIs=static_cast(this->m_instance); pIs->testDerived=100; } }; int _tmain(int argc, _TCHAR* argv[]) { DerivedContainer der; der.TestFunction(); return 0; } thanks in advance . valhamdolelah

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

      khomeyni wrote:

      DerivedMemer* pIs=static_cast(this->m_instance);

      This does not compile. Try:

      DerivedMemer *pIs = static_cast<DerivedMemer *>(this->m_instance);

      You could also use:

      DerivedMemer *pIs = (DerivedMemer *) this->m_instance;

      "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

      K 1 Reply Last reply
      0
      • D David Crow

        khomeyni wrote:

        DerivedMemer* pIs=static_cast(this->m_instance);

        This does not compile. Try:

        DerivedMemer *pIs = static_cast<DerivedMemer *>(this->m_instance);

        You could also use:

        DerivedMemer *pIs = (DerivedMemer *) this->m_instance;

        "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

        K Offline
        K Offline
        khomeyni
        wrote on last edited by
        #3

        in the name of god hello and thanks for your guide but my question is that every time we need the m_instance of class DrivedContainer we must use TestFunction as in the main belowthat it is not good way to do this in every initializing; is there any way to avoid writing TestFunction and casting the m_instance to DrivedMember class object? void TestFunction() { DerivedMemer *pIs = static_cast(this->m_instance); pIs->testDerived=100; } int main(int argc, _TCHAR* argv[]) { DerivedContainer der; der.TestFunction(); return 0; } thanks valhamdolelah

        D 1 Reply Last reply
        0
        • K khomeyni

          in the name of god hello and thanks for your guide but my question is that every time we need the m_instance of class DrivedContainer we must use TestFunction as in the main belowthat it is not good way to do this in every initializing; is there any way to avoid writing TestFunction and casting the m_instance to DrivedMember class object? void TestFunction() { DerivedMemer *pIs = static_cast(this->m_instance); pIs->testDerived=100; } int main(int argc, _TCHAR* argv[]) { DerivedContainer der; der.TestFunction(); return 0; } thanks valhamdolelah

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

          khomeyni wrote:

          ...every time we need the m_instance of class DrivedContainer...

          From where? It can be accessed from another method in DerivedContainer, and in main(). How are you wanting to get access to m_instance?

          "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

          "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

          K 1 Reply Last reply
          0
          • D David Crow

            khomeyni wrote:

            ...every time we need the m_instance of class DrivedContainer...

            From where? It can be accessed from another method in DerivedContainer, and in main(). How are you wanting to get access to m_instance?

            "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

            "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

            K Offline
            K Offline
            khomeyni
            wrote on last edited by
            #5

            in main i used : DerivedContainer der; der.TestFunction(); i mean that every time i create a new object of DrivedContainer i must cast m_instance of class DrivedContainer which has been inherited from BaseContainer to DrivedContainer. class BaseContainer { public: BaseMember* m_instance; BaseContainer() { m_instance=new BaseMember(); } }; int main(int argc, _TCHAR* argv[]) { DerivedContainer der; der.TestFunction(); } you said there is another way for this. please say what i must to do? valhamdolelah

            D 1 Reply Last reply
            0
            • K khomeyni

              in main i used : DerivedContainer der; der.TestFunction(); i mean that every time i create a new object of DrivedContainer i must cast m_instance of class DrivedContainer which has been inherited from BaseContainer to DrivedContainer. class BaseContainer { public: BaseMember* m_instance; BaseContainer() { m_instance=new BaseMember(); } }; int main(int argc, _TCHAR* argv[]) { DerivedContainer der; der.TestFunction(); } you said there is another way for this. please say what i must to do? valhamdolelah

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

              khomeyni wrote:

              you said there is another way for this. please say what i must to do?

              I've not sure if it is the correct way, but one option would be:

              class BaseContainer
              {
              public:
              BaseMember *m_instance;

              BaseContainer()
              {
                  m\_instance = new BaseMember();
              }
              
              BaseContainer( DerivedMemer \*d )
              {
                  m\_instance = d;
              }
              

              };

              class DerivedContainer : public BaseContainer
              {
              public:
              DerivedContainer() : BaseContainer( new DerivedMemer )
              {
              }
              };

              void main( void )
              {
              DerivedContainer der;
              BaseContainer bas;
              }

              This is a part of C++ that I've never had to bother/worry about.

              "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

              "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

              K 1 Reply Last reply
              0
              • D David Crow

                khomeyni wrote:

                you said there is another way for this. please say what i must to do?

                I've not sure if it is the correct way, but one option would be:

                class BaseContainer
                {
                public:
                BaseMember *m_instance;

                BaseContainer()
                {
                    m\_instance = new BaseMember();
                }
                
                BaseContainer( DerivedMemer \*d )
                {
                    m\_instance = d;
                }
                

                };

                class DerivedContainer : public BaseContainer
                {
                public:
                DerivedContainer() : BaseContainer( new DerivedMemer )
                {
                }
                };

                void main( void )
                {
                DerivedContainer der;
                BaseContainer bas;
                }

                This is a part of C++ that I've never had to bother/worry about.

                "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

                "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                K Offline
                K Offline
                khomeyni
                wrote on last edited by
                #7

                in the name of god hello and thanks for your guide but suppose that i want to create a function in DerivedContainer and use m_instance there then it is false for it and yet we must cast for each object. example: class DerivedContainer : public BaseContainer { public: DerivedContainer() : BaseContainer( new DerivedMemer ) { } mynew_fun(){//here we need type_cast yet. DerivedMember *nD=static_cast (this->m_instance); } };

                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