Access data in a parent class.

This is a discussion on Access data in a parent class. within the C++ Programming forums, part of the General Programming Boards category; How can I access varibles in a parent class? Say I have CHILD_CLASS inside of PARENT_CLASS. An I want a ...

  1. #1
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342

    Question Access data in a parent class.

    How can I access varibles in a parent class?

    Say I have CHILD_CLASS inside of PARENT_CLASS.
    An I want a function in my CHILD_CLASS to access a varible in PARENT_CLASS.

    How would I do that without saving that varible in a global to accessed externally?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >How can I access varibles in a parent class?
    The "correct" object-oriented way would be to provide a protected member function for accessing the variable that the child class can inherit.

    >Say I have CHILD_CLASS inside of PARENT_CLASS.
    Are you talking about an inheritance hierarchy or nesting? If it's the former then see my answer above. If it's the latter, make the "child" class a friend of the "parent" class and reconsider your terminology.
    My best code is written with the delete key.

  3. #3
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    Yes, I am refering to "inheritance hierarchy".

    >> The "correct" object-oriented way would be to provide
    >> a protected member function for accessing the
    >> variable that the child class can inherit.

    I don't understand.

  4. #4
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Code:
    class parent
    {
    private:
    	int d;
    
    protected:
    	virtual int get_d( void ) // prob doesn't need to virtual
    	{
    		return d;
    	}
    
    public:
    	parent() : d( 42 ){}
    };
    
    
    
    class child : public parent
    {
    private:
    	int x;
    
    public:
    	child() : x(0){}
    
    	void whatever()
    	{
    		cout<< "Here's parent::d - " << get_d();
    	}
    };
    
    int main( void )
    {
    	child df;
    
    	df.whatever();
    }
    Last edited by twomers; 09-17-2006 at 12:52 PM.

  5. #5
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    But what I had the exact same class in a class?

  6. #6
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,904
    Then read Prelude's reply. All you have to do is implement a protected function in the parent class which returns the value of the variable you desire. Then call this function from the child class and you're done.

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >But what I had the exact same class in a class?
    Why don't you show us what you have? I'm getting mixed signals.
    My best code is written with the delete key.

  8. #8
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    I think he means something like:

    Code:
    class thing
    {
    	thing *variable;
    };
    going from his previous thread. SHow us what you got though.

  9. #9
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    Code:
    class minimax
    {
       public:
          minimax *ply;
          char board[9];
          int depth;
          void look_ahead(); 
    };
    
    void minimax::look_ahead()
    {
          }
    
    minimax my_ai;
    This is what I have so far.

    I trying to make my child minimax class (ply) to make it's depth varible the same as it's parent's plus 1.

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I think he means something like:
    Then it's not a problem. You can access the private members of an object of the same class, whether it's a member itself or the argument to a function:
    Code:
    class thing {
        thing *p;
        int i;
    public:
        void foo1()
        {
            p->i = 321; // Just fine
        }
    
        void foo2 ( const thing& t )
        {
            t.p->i = 123; // Also fine
        }
    };
    My best code is written with the delete key.

  11. #11
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    >> I trying to make my child minimax class (ply) to make it's depth varible the same as it's parent's plus 1.

    You gonna call the child the same as the parent? (I know this is don in real life, but I don't think it's ok in programming)

  12. #12
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    Code:
    class minimax
    {
       public:
          minimax();
          minimax *ply;
          char board[20];
          int depth;
          void override_depth(int); 
          void look_ahead(); 
    };
    
    minimax::minimax(){
       depth = ply.depth + 1;   }
    
    void minimax::override_depth(int dna){
       depth = dna;   }
    
    void minimax::look_ahead()
    {
          }
    Okay, this script has my parent access my child. But I need the child to access the parent, not the other way round.

    >> You gonna call the child the same as the parent? (I know
    >> this is don in real life, but I don't think it's ok in programming)

    If this is the case, then an external varible will be the only thing I can do, right?

    Also, should I not be trying to make my own minimax class, and just download one off some site?

  13. #13
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Quote Originally Posted by twomers
    >> I trying to make my child minimax class (ply) to make it's depth varible the same as it's parent's plus 1.

    You gonna call the child the same as the parent? (I know this is don in real life, but I don't think it's ok in programming)
    There is no child or parent, there is one class...

  14. #14
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    > Okay, this script has my parent access my child.

    I think we have a confusion of terms here. It looks like you don't mean parent, as in base class; you mean the owner of a pointer to an instance of the same class.

    Can you just do:
    Code:
    ply->depth = this->depth+1;
    ?
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  15. #15
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    Hmm, that locks my program up.

    Are you sure that ply->depth is the "parent"s varible rather than "child"s?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gcc: Template class child can't directly access parent fields
    By SevenThunders in forum C++ Programming
    Replies: 11
    Last Post: 03-17-2009, 07:05 AM
  2. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 07:51 PM

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