Thread: Accessing members of parent class

  1. #1
    return 0;
    Join Date
    Jan 2005
    Location
    Netherlands
    Posts
    89

    Accessing members of parent class

    If I have two different classes that are both derived from the same parent class, is it possible for one class to access the parent members of the other class? Example:

    Code:
    #include <iostream>
    using namespace std;
    
    class Parent
    {
        protected:
            int value;
    };
    
    
    
    class Child_A : public Parent
    {
        public:
            void Foobar(Parent &other) { cout << other.value << endl; }
    };
    
    
    
    class Child_B : public Parent
    {
        
    };
    
    
    
    int main()
    {
        Child_A a;
        Child_B b;
    
        a.Foobar(b);
        return 0;
    }
    This gives me "int Parent::value is protected". Is it possible in another way?

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    >> void Foobar(Parent &other) { cout << other.value << endl; }

    change to:

    Code:
    void Foobar() { cout << value << endl; }
    the protected values are now variables in the Child_A class, Loosly speaking, so there's no need to pass anything into it. Also, Cild_B isnot a parent type, so:

    Code:
    int main()
    {
        Child_A a;
        Child_B b;
    
        a.Foobar(b);
        return 0;
    }
    that won't work, unless b is declared as:

    Code:
    Parent b;
    Last edited by twomers; 06-29-2006 at 10:23 AM.

  3. #3
    return 0;
    Join Date
    Jan 2005
    Location
    Netherlands
    Posts
    89
    But I want to access Child_B's value from within a method of Child_A (both are derived from Parent)

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    access value public or via public function inside Parent

    public static member

    Kuphryn

  5. #5
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    then use:

    Code:
    class Child_A : public Child_B
    {
    };
    i guess, or use a public getter in Child_B to get Child_A's variables.

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Pass in a const reference to Child_B and access the value.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You cannot do what you are trying to do. Child_A has the same access to Child_B's data as any other class that isn't related. You have to provide public accessor methods that anybody can use, or make Child_A a friend of Parent or pass a Child_B and make Child_A a friend of Child_B.

  8. #8
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Bad design in any case.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linking error with static template class members
    By cunnus88 in forum C++ Programming
    Replies: 6
    Last Post: 04-02-2009, 12:31 PM
  2. Base-class pointer, accessing object from derived class
    By Korhedron in forum C++ Programming
    Replies: 15
    Last Post: 09-28-2008, 05:30 AM
  3. Private Static class members
    By earth_angel in forum C++ Programming
    Replies: 13
    Last Post: 08-29-2005, 06:37 AM
  4. Protected Inheritance
    By golfinguy4 in forum C++ Programming
    Replies: 8
    Last Post: 12-27-2002, 10:56 AM
  5. Accessing private data members
    By maloy in forum C++ Programming
    Replies: 11
    Last Post: 10-04-2002, 02:48 PM