Results 1 to 7 of 7

Thread: [SOLVED] Allow a class to access data from its parent.

  1. #1
    Join Date
    Jul 2007
    Beans
    341

    Arrow [SOLVED] Allow a class to access data from its parent.

    Okay I got class A (parent class) which creates an instance of class B (child class) inside of it. I want the methods in class B to access the data of class A. Is this possible? I've been reading the article on Circular Dependency, but I want to do this without a mess of header files.

    Code:
    class B;
    
    class A
    {
    	public:
    		
    	int qqq;
    	B* child;
    	
    	A()
    	{
    		qqq = 32;
    		child = new B(this);
    	}
    };
    
    class B
    {
    	public:
    	
    	A* parent;
    	
    	B(A* p)
    	{ parent = p; }
    	
    	void print()
    	{
    		cout << parent->qqq << endl;
    	}
    };
    
    int main(void)
    {
    	A parent;
    	parent.child->print();
    }
    and here are the errors:

    Code:
    error: invalid use of incomplete type 'struct B'
    error: forward declaration of 'struct B'

  2. #2
    Join Date
    May 2006
    Location
    US (MA)
    Beans
    79
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Allow a class to access data from its parent.

    Hi!

    The usual approach to solve a circular dependency is to create a third (interface) class C which combines both A and B as pointers. This way they can be linked to eachother without knowing of each others existence.

    Something like this:
    Code:
    class C {
    public:
       C(A* a, B* b): a_(a), b_(b) 
       {} 
    private:
       A* a_;
       B* b_;
    };
    Hope this helps,
    Roel.
    Ubuntu User #12366
    Read my blog for my experiences with Ubuntu

  3. #3
    Join Date
    Aug 2006
    Location
    60°27'48"N 24°48'18"E
    Beans
    3,458

    Re: Allow a class to access data from its parent.

    I guess it is "friend classes" that the OP is looking for...
    LambdaGrok. | #ubuntu-programming on FreeNode

  4. #4
    Join Date
    Feb 2007
    Beans
    4,045
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Allow a class to access data from its parent.

    The problem is that A's constructor is using B's constructor before B's constructor has been declared. One way to get around that is to only declare A's constructor in the class declaration, and define it after B's constructor has been declared.

    Code:
    class B;
    class A
    {
        ...
        A(); // declare constructor
    };
    class B
    {
        ...  
    };
    A::A()   // define constructor
    {
        qqq = 32;
        child = new B(this);
    }
    EDIT: Oh and don't forget to make a destructor that deletes the child.
    Last edited by geirha; December 14th, 2008 at 12:54 PM.

  5. #5
    Join Date
    Jul 2008
    Location
    Dublin, Ireland
    Beans
    633
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Allow a class to access data from its parent.

    The error is unrelated to the title of the post. There is a limited amount of things you can do with a forward declared class (defining pointers, references or static member variables) and many things you cannot do until the full declaration of the class is presented to the compiler. (Read this post's comments)

    Code:
    class A;
    class B;
    
    class A // class declaration
    {
    public:
       A();
    private:
       B* child_;
    };
    
    class B
    {
    public:
       B( A* parent ) : parent_(parent) {}
    private:
       A* parent_;
    };
    
    A::A() : child( new B( this ) ) // ok B has been fully declared
    {}
    Last edited by dribeas; December 14th, 2008 at 08:31 PM. Reason: Added link to previous similar post.

  6. #6
    Join Date
    Jul 2007
    Beans
    341

    Re: Allow a class to access data from its parent.

    Okay thanks, geirha. I have been playing with this some more and it makes for an interesting demo on pointers.

    These two statements are equivalent:
    Code:
    par.child->parent->quad();	
    par.child->parent->child->parent->child->parent->child->parent->child->parent->child->parent->child->parent->quad();
    I wonder what kind of performance hit the second statement takes

    Code:
    class A;
    
    class B
    {
    	public:
    	
    	A* parent;
    	
    	B(A* );
    	
    	void print();
    	
    	//Hi refs nothing so it is fine here
    	void hi()
    	{ cout << "hi" << endl;}
    };
    
    class A
    {
    	public:
    		
    	int qqq;
    	double* ddd;
    	
    	B* child;
    	
    	A()
    	{
    		cout << "Class A constructor: " << this << endl;
    		
    		qqq = 32;
    		child = new B(this);
    		
    		//Create a gig of data in ram to prove that it is not duplicated
    		ddd = new double[(int)1.25e8];
    		for ( int x = 0; x < (int)1.25e8; x++ )
    			ddd[x] = x;
    	}
    
    	//quad refs nothing so it is fine here
    	void quad()
    	{
    		qqq *= 4;
    	}
    };
    
    B::B(A* p)
    { 
    	cout << "Class B constructor: " << this << endl;
    	parent = p; 
    	cout << "Parent = : " << parent << endl;
    }
    
    
    //Print References A therefore it needs to be after A
    void B::print()
    {
    	cout << parent->qqq << endl;
    }
    
    int main(void)
    {
    	A par;
    	par.child->print();
    	par.child->parent->quad();
    	//par A creates child B passing it A's address
    	//B refs A which has function quad
    	
    	par.child->parent->child->parent->child->parent->child->parent->child->parent->child->parent->child->parent->quad();		//Same function as above
    	
    	par.child->print();
    	par.child->hi();
    	
    	char c; cin >> c;	//Pause
    	
    }

  7. #7
    Join Date
    Jul 2007
    Beans
    341

    Re: Allow a class to access data from its parent.

    Quote Originally Posted by u-slayer View Post

    Code:
    par.child->parent->quad();	
    par.child->parent->child->parent->child->parent->child->parent->child->parent->child->parent->child->parent->quad();
    I wonder what kind of performance hit the second statement takes
    I added a for ( int x = 0; x < (int)1e9; x++) before each statement to test it.

    Above first statement = 4.868s
    Above second statement = 10.832s

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •