Thread: c++ inheritance

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    27

    c++ inheritance

    I need help with inheritance

    Code:
    class MemoryViewer : public CWnd
    {
    public:
    myclass *myobj;
    MemoryViewer(myclass *_myobj);
    }
    
    MemoryViewer::MemoryViewer(myclass *_myobj)
    {
      myobj = _myobj;
    }
    
    class MyMemoryViewer : public MemoryViewer {
     public:
      MyMemoryViewer();
      virtual void readData(int);
    };
    
    MyMemoryViewer::MyMemoryViewer()
    : MemoryViewer(myobject)
    {
      setAddressSize(0);
    }
    
    class MemoryViewerDlg
    {
      MyMemoryViewer my_viewer;
     public:	
      myclass *myobject ;
      MemoryViewerDlg(myclass *_myobject, CWnd* pParent = NULL);  
    public:
      virtual void read(int)=0;
    }
    Now I want to be able to do:

    myclass *myobject = new myclass;
    MemoryViewerDlg *dlg = new MemoryViewerDlg(myobject);

    and then I want to access myobject in the MyMemoryViewer read function?

    How can I pass the arm object all the way to that function?

  2. #2
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    I don't understand what your problem is, you have the myobject as a member variable, so why not access it in the read function? Just make sure you set it correctly in the code for your constructor.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    27
    I want to pass it along to MyMemoryViewer!!!
    The myobject is NOT global!

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >and then I want to access myobject in the MyMemoryViewer read function?
    In your constructor you would do this:
    Code:
      MemoryViewerDlg(myclass *_myobject, CWnd* pParent = NULL)
      {
        myobject = _myobject;
      }
    And then in read() you have access to myobject.
    Last edited by swoopy; 10-24-2005 at 02:15 PM.

  5. #5
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    use an initialiser list

    Code:
    class MemoryViewerDlg
    {
      MyMemoryViewer my_viewer;
     public:	
      myclass *myobject ;
      MemoryViewerDlg(myclass *_myobject, CWnd* pParent = NULL)
      : my_viewer(_myobject)
      {
      }
    };
    and don't use _varname. they are generally reserved for compiler keywords.
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 06-08-2009, 03:03 PM
  2. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 09:04 AM
  3. inheritance and performance
    By kuhnmi in forum C++ Programming
    Replies: 5
    Last Post: 08-04-2004, 12:46 PM
  4. Inheritance and Polymorphism
    By bench386 in forum C++ Programming
    Replies: 2
    Last Post: 03-18-2004, 10:19 PM
  5. Inheritance vs Composition
    By Panopticon in forum C++ Programming
    Replies: 11
    Last Post: 01-20-2003, 04:41 AM