473,325 Members | 2,805 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

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

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
Jul 22 '05 #1
5 2347
Glenn Serpas wrote:
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;
}
Jul 22 '05 #2
"Glenn Serpas" <no***@nowhere.net> wrote in message
news:C8*****************@newssvr23.news.prodigy.co m...
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.

--
David Hilsee
Jul 22 '05 #3

"Gianni Mariani" <gi*******@mariani.ws> wrote in message
news:cf********@dispatch.concentric.net...
<snip>
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.

--
David Hilsee
Jul 22 '05 #4
"Glenn Serpas" <no***@nowhere.net> wrote in message
news:C8*****************@newssvr23.news.prodigy.co m...
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.
--
Gary
Jul 22 '05 #5
Glenn Serpas wrote:
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.
Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Az Tech | last post by:
Hi people, (Sorry for the somewhat long post). I request some of the people on this group who have good experience using object-orientation in the field, to please give some good ideas for...
21
by: Jason Heyes | last post by:
I want to allow objects of my class to be read from an input stream. I am having trouble with the implementation. Here are the different approaches I have tried: // Version 1.0 - Default...
3
by: ozbear | last post by:
This is probably an obvious question. I know that pointer comparisons are only defined if the two pointers point somewhere "into" the storage allocated to the same object, or if they are NULL,...
11
by: jacob navia | last post by:
I am writing software to make a general storage facility of any kind of objects to/from disk. The intermeidate format used is XML, using the schema (modified a bit) of Microsoft:...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
4
by: Tomas | last post by:
A newbie question: How can I instantiate objects dynamically in VB.NET. E.g. I have the object 'Player' and I would like to instantiate it with the several instances (James, Gunner, etc.), without...
4
by: Jess | last post by:
Hello, I tried several books to find out the details of object initialization. Unfortunately, I'm still confused by two specific concepts, namely default-initialization and...
10
by: sumsin | last post by:
The C++ Object Model book says that 'Nonstatic data members are allocated directly within each class object. Static data members are stored outside the individual class object. Static and...
14
by: Szabolcs Borsanyi | last post by:
Deal all, The type typedef double ***tmp_tensor3; is meant to represent a three-dimensional array. For some reasons the standard array-of-array-of-array will not work in my case. Can I...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.