Pointers and Inheritance

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • muffins
    New Member
    • Nov 2008
    • 6

    Pointers and Inheritance

    Hi,

    I am trying to get a pointer created in a derived class to be accesible and used in the base class. Is this actually possible and how would one call that pointer?

    Thankyou for your help in advance :)

    - Muffins
  • boxfish
    Recognized Expert Contributor
    • Mar 2008
    • 469

    #2
    How is the pointer created in the base class? If it's just a member of the base class that's inherited by the derived class, you should be able to use it just like any other inherited member. You're just refering to any sort of dynamic array or variable, right?

    Comment

    • Ganon11
      Recognized Expert Specialist
      • Oct 2006
      • 3651

      #3
      If the pointer is made and used in the derived class, it cannot be viewed in the base class. Consider the following simple outline:

      Code:
      class Person {
         // ... stuff here.
         // for example,
         public:
            string name;
            int age;
      };
      
      class Student : public person {
         // ... stuff here.
         // for example
         public:
            double GPA;
      };
      A Student object has a name, age, and GPA, but a Person object does not have a GPA, because a Person is NOT a Student. A Student IS-A Person, not vice versa.

      Now, why do you need to use a derived class pointer in a base class? It would seem prudent to make this pointer in the base class so that both would have access to it.

      Comment

      • muffins
        New Member
        • Nov 2008
        • 6

        #4
        Thank you both for your reply. It is actually a requirement of an assigment I am doing that the pointer has to be made in the sub class and then used in the base class, or at least reference is made to it in the base class. Hope I am making some sense.

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by muffins
          Thank you both for your reply. It is actually a requirement of an assigment I am doing that the pointer has to be made in the sub class and then used in the base class, or at least reference is made to it in the base class. Hope I am making some sense.
          You could make an object of the base class ask for that pointer through a small virtual method. The first virtual method in the base class can simply return NULL, the overridden function implementation( s) in the derived class(es) should return the actual value of such a pointer.

          kind regards,

          Jos

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            Use the Visitor design pattern for this.

            You visit your base class with a pointer to a visitor. The derived class overrides the base class visit method and calls a visitor method using the derived class pointer.

            Done every day.

            Comment

            Working...