polymorphism problem

This is a discussion on polymorphism problem within the C++ Programming forums, part of the General Programming Boards category; I have a class A, which you can derive your own class from and use it to do various things. ...

  1. #1
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483

    polymorphism problem

    I have a class A, which you can derive your own class from and use it to do various things. With the class there are a few functions, that will need access to the members of class A and the members of the derived class.

    The problem is how do I make the function so it can access the members of the Base and Derived class, when I don't know what the derived class's name might be.

    something like this i guess:

    Code:
    class A
    {
    public:
    int a;
    };
    class B : public A
    {
    public:
    int b;
    }
    
    
    //Im not sure what the parameters should be for the functions
    void FunctionFoo(A aa)
    {
    //Ill need access to the members of the derived class ( B ), but I dont know what the class name is.
    cout << aa.b << endl;
    }
    My Website
    010000110010101100101011
    Add Color To Your Code!

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    Step one if A is meant to be a base class is to give it a virtual destructor.

    To access data from the derived class, use a virtual function. For example, you could create a virtual function in class A: virtual int GetDerivedData(). Then, class B can override that function to return its b value. Other derived classes can return their own particular value.

    Your FunctionFoo should take A as a reference so that if you pass it an instance of the derived class, the derived class's overrides will be called instead. This is the essence of polymorphism.

  3. #3
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    Yeah, I just slapped that example together without too much tough.

    Thanks though.

    But what if you are not sure how many members are in the derived class? How would you know how many virtual functions to make.
    My Website
    010000110010101100101011
    Add Color To Your Code!

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    That is not what polymorphism is used for. The goal of polymorphism is for you to have a code like FunctionFoo that works on an A reference or pointer, and only knows about the A interface. Then, you create derived classes that implement that interface in different ways. You can change how they are implemented, or add new derived classes with new implementations, but FunctionFoo will always work because all it does is call methods from A's interface.

    So when writing code for the base class, you don't want to know how any of the derived classes will be setup, you want to make the interface that makes sense for all of them. For example, if you want an output method that outputs all the data members, you might wonder how you can know what the derived class data members are so they can be output. The answer is to make that output method virtual in the base class, and each derived class implements it properly for their own data members, while the base class never knows what those derived members are.

  5. #5
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    Well what Im really trying to do is wrap win32 in my own classes. So far everything's been going ok.

    But now that im making a dialog class, I want the dialog to have full acess to all the members of the main application class, i was thinking of passing a pointer to of the class to the dialog's contructor, but thats not gonna work.

    So now im thinking of letting the user to make the pointer to his derived class in the dialog class, and make the constructor ( or a create method) virual so it can be overwriten to accep the derived class.

    is this the right approach?
    My Website
    010000110010101100101011
    Add Color To Your Code!

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    I'm not sure I understand your scenario. What would the "derived class" derive from? Why can't you pass a pointer of the main application class to the dialog class?

    I think you might consider doing research on some design patterns. I'm sure there is some good information available, considering the whole dialog-application relationship is a common one.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. Polymorphism Problem
    By ltanusaputra in forum C++ Programming
    Replies: 3
    Last Post: 04-25-2007, 11:36 AM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. Polymorphism - Can't Articulate Problem
    By Tonto in forum C++ Programming
    Replies: 17
    Last Post: 11-13-2006, 04:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21