473,799 Members | 2,950 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2377
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******** *********@newss vr23.news.prodi gy.com...
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*******@mari ani.ws> wrote in message
news:cf******** @dispatch.conce ntric.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 ("Miscellane ous
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******** *********@newss vr23.news.prodi gy.com...
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
2611
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 topics to include in a course on object-orientation that I'm going to conduct. (I will later summarize all the replies and discussion, for the
21
2532
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 constructors class MyClass { Foo foo; // foo and bar require default constructors Bar bar; public:
3
3458
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, or one-past the end of the object as long as it isn't dereferenced. I use "object" in the standard 'C' sense. Is there some special dispensation given to comparing two pointers
11
1982
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: xmlns="x-schema:xop-schema.xml" Operation: ---------- The software generates several C functions that implement the writing of the XML. To make things more concrete suppose
11
3846
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 experts. I would like to produce Javascript classes that can be "subclassed" with certain behaviors defined at subclass time. There are plenty of ways to do this through prototyping and other techniques, but these behaviors need to be static and...
4
3666
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 in advance knowing how many objects (employee1, employee2, etc) Dim player1 As New Persons.Players Dim player2 As New Persons.Players Dim player3 As New Persons.Players ....
4
3714
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 value-initialization. I think default-init calls default constructor for class objects and sets garbage values to PODs. Value-init also calls default constructor for class objects and sets 0s to POD types. This is what I've learned from the books (especially...
10
2052
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 nonstatic function members are also hoisted outside the class object. Virtual functions are supported in two steps: A table of pointers to virtual functions is generated for each class (this is called the virtual table). A single pointer to the...
14
1989
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 convert an object of this type to the following type?
0
9686
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10475
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10250
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10026
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9068
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7564
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6805
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3757
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.