How to Instantiate object pointers that are members of other objects .

G

Glenn Serpas

I have Class A and Class B .. Class B has a private member that is a pointer
to a Class A object.

private:
B *mypointer ;


I instantiate the A object

A* myobject new = A();


How do I tell it to instantiate it's member mypointer ? I've tried several
options I've read about in my C++ book and on the web. The solutions I
found were to add the following to the contructions declaration in the
class header. However, it will not compile.

public:
A() { mypointer = new B() ; }


I've also tried adding instantitating the object in the contructor
implementation. This will compile, but will crash during execution.

A::A()
{
B* mypointer = new B() ;

}


I can only instantiate the mypointer if it is not a pointer (ie B
myBobject ;) I simply add it to the constructor implementation for class A.

A::A() : myBobject()
{

}


Hopefully someone reading this message will have an approach that will work
for me. Thanks.

Glenn
 
G

Gianni Mariani

Glenn said:
I have Class A and Class B .. Class B has a private member that is a pointer
to a Class A object.

private:
B *mypointer ;


I instantiate the A object

A* myobject new = A();


How do I tell it to instantiate it's member mypointer ? I've tried several
options I've read about in my C++ book and on the web. The solutions I
found were to add the following to the contructions declaration in the
class header. However, it will not compile.

public:
A() { mypointer = new B() ; }


I've also tried adding instantitating the object in the contructor
implementation. This will compile, but will crash during execution.

A::A()
{
B* mypointer = new B() ;

}


I can only instantiate the mypointer if it is not a pointer (ie B
myBobject ;) I simply add it to the constructor implementation for class A.

A::A() : myBobject()
{

}


Hopefully someone reading this message will have an approach that will work
for me. Thanks.


class A;
class B;


class A
{
public:
A( B * );

private:
B * ptrb;
};

class B
{
public:
B();

private:
A * ptra;
};


B::B()
: ptra( new A( this ) )
{
}


A::A( B * foo )
: ptrb( foo )
{
}


int main()
{
B * b = new B;
}
 
D

David Hilsee

Glenn Serpas said:
I have Class A and Class B .. Class B has a private member that is a pointer
to a Class A object.

private:
B *mypointer ;


I instantiate the A object

A* myobject new = A();

I'm sure you meant A* myobject = new A();
How do I tell it to instantiate it's member mypointer ? I've tried several
options I've read about in my C++ book and on the web. The solutions I
found were to add the following to the contructions declaration in the
class header. However, it will not compile.

public:
A() { mypointer = new B() ; }

That looks like an attempt at writing an inline definition, not a
declaration. It would conflict with what you have written below, because
that is also a definition.
I've also tried adding instantitating the object in the contructor
implementation. This will compile, but will crash during execution.

A::A()
{
B* mypointer = new B() ;

}

This code does not assign a value to the member, but instead creates and
initializes a local variable with the same name as the member. Perhaps you
meant to write

mypointer = new B() ;

HTH.
 
D

David Hilsee

class A;
class B;


class A
{
public:
A( B * );

private:
B * ptrb;
};

class B
{
public:
B();

private:
A * ptra;
};


B::B()
: ptra( new A( this ) )
{
}


A::A( B * foo )
: ptrb( foo )
{
}


int main()
{
B * b = new B;
}

To the OP: A good explanation of this code can be found in the FAQ
(http://www.parashift.com/c++-faq-lite/) section 38 ("Miscellaneous
technical issues") question 11 ("How can I create two classes that both know
about each other?") as well as questions 12 and 13.
 
G

Gary Labowitz

Glenn Serpas said:
I have Class A and Class B .. Class B has a private member that is a pointer
to a Class A object.

private:
B *mypointer ;


I instantiate the A object

A* myobject new = A();


How do I tell it to instantiate it's member mypointer ? I've tried several
options I've read about in my C++ book and on the web. The solutions I
found were to add the following to the contructions declaration in the
class header. However, it will not compile.

public:
A() { mypointer = new B() ; }


I've also tried adding instantitating the object in the contructor
implementation. This will compile, but will crash during execution.

A::A()
{
B* mypointer = new B() ;

}


I can only instantiate the mypointer if it is not a pointer (ie B
myBobject ;) I simply add it to the constructor implementation for class A.

A::A() : myBobject()
{

}


Hopefully someone reading this message will have an approach that will work
for me. Thanks.

There are a couple of issues. First, you are implying that whenever you have
an A, you will always have a B that A will point to using mypointer. If this
is true, there are two cases.
1. B may or may not yet exist, but when it does A can point to it. Also, A
can be changed to point to different B's during execution.
2. B always exists, and is created if it doesn't whenever an A is created.
This means the B is a part of A -- always. For this case, it might be best
to make B a member of A and have it created automatically whenever an A is
created. Otherwise, do as you were doing with the creation of a new B in the
constructor of A. I will note that this may have failed to compile because
the compiler didn't know what a B was at the point you used it in the
constructor of A. For it to know, B must have been declared before the code
of A that uses the B class. This can be done by putting the B class
definition ahead of A's code.

In any event, if the case is 1. (B can be created later) you will need a
function in A that can be called to set the mypointer member to the address
of a B that is passed as an actual parameter to the function.

It is also possible to turn this around the other way -- letting a new B
create an A and setting the mypointer member of that A to itself.

It all depends on how the two classes are to interoperate.

I hope this helps.
 
G

Glenn Serpas

Glenn said:
I have Class A and Class B .. Class B has a private member that is a
pointer to a Class A object.

private:
B *mypointer ;


I instantiate the A object

A* myobject new = A();


How do I tell it to instantiate it's member mypointer ? I've tried several
options I've read about in my C++ book and on the web. The solutions I
found were to add the following to the contructions declaration in the
class header. However, it will not compile.

public:
A() { mypointer = new B() ; }


I've also tried adding instantitating the object in the contructor
implementation. This will compile, but will crash during execution.

A::A()
{
B* mypointer = new B() ;

}


I can only instantiate the mypointer if it is not a pointer (ie B
myBobject ;) I simply add it to the constructor implementation for class
A.

A::A() : myBobject()
{

}


Hopefully someone reading this message will have an approach that will
work for me. Thanks.

Glenn

Thanks for the information. One of the solutions Gianni provided was what I
needed. In addition. thanks for the FAQ site. I've been looking at.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top