473,405 Members | 2,334 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,405 software developers and data experts.

Initialization with this.

Hi,

I had a scenario where classes had two way composition (Already
present in a huge code. Wasn't introduced by me) something like this:

A {
public:
A ()
: b(*this)
{
}
private:
B b;
};

B {
public B (A aobj) : a(aobj) {}
private:
A a;
};

I believe this code is not valid (undefined behavior ??) as A is not
fully constructed when it is passed to B. I googled around but didn't
find a direct reference of such situation. Any hints? pointers ?

~Moh
Nov 10 '08 #1
6 1378
Morya wrote:
Hi,

I had a scenario where classes had two way composition (Already
present in a huge code. Wasn't introduced by me) something like this:

A {
public:
A ()
: b(*this)
{
}
private:
B b;
};

B {
public B (A aobj) : a(aobj) {}
private:
A a;
};

I believe this code is not valid (undefined behavior ??) as A is not
fully constructed when it is passed to B. I googled around but didn't
find a direct reference of such situation. Any hints? pointers ?
To heck with UB. How the hell does it even compile? B is not defined
when it is declared as a member variable of A.
Nov 10 '08 #2
To heck with UB. *How the hell does it even compile? *B is not defined
when it is declared as a member variable of A.
Sorry for misleading code. you could use pointer and forward declare a
class. something like this:

class B;

class A {
..
...
private:
B* b;
};

The main point here about the state of the object which is passed with
the this pointer.
I would accept code any code you suggest that would convey the same
idea. The code that i presented was a place holder.
Nov 10 '08 #3
zr
On Nov 10, 8:03*am, Morya <mda...@gmail.comwrote:
Hi,

I had a scenario where classes had two way composition (Already
present in a huge code. Wasn't introduced by me) something like this:

A {
public:
A ()
: b(*this)
{}

private:
B b;

};

B {
public B (A aobj) : a(aobj) {}
private:
A a;

};

I believe this code is not valid *(undefined behavior ??) as A *is not
fully constructed when it is passed to B. I googled around but didn't
find a direct reference of such situation. Any hints? pointers ?

~Moh
I don't know what is the expected behavior of the above code, if any.
But if you are looking for a solution to your problem, you might
consider changing class B's member a to be of type A& or A*. That way
a would refer/point to the right object. Of course, this solution may
not be practical if B must have its private copy of the A stored in a.

Here's the reference option (similarly you could write the pointer
option):

A {
public:
A ()
: b(*this)
{}

private:
B b;

};

B {
public B (A& aobj) : a(aobj) {}
private:
A& a; // a is now a reference

};
Nov 10 '08 #4
Hi!

Morya schrieb:
>To heck with UB. How the hell does it even compile? B is not defined
when it is declared as a member variable of A.

Sorry for misleading code. you could use pointer and forward declare a
class. something like this:

class B;

class A {
..
..
private:
B* b;
};

The main point here about the state of the object which is passed with
the this pointer.
I would accept code any code you suggest that would convey the same
idea. The code that i presented was a place holder.
It depends on what you are doing with the A instance.
Storing Pointers and references to *this is valid at any time in the
constructor. Calling member functions may be or may be not.
Effectively everything you invoke from a constructor becomes part of the
constructor. This applies to your own member functions in the same way
as for any other piece of code. The constructor is responsible to do
this in a well defined way. At least all base classes and members of A
are initialized at this point (unless you forgot to initialize som POD
types).
Marcel
Nov 10 '08 #5
Morya wrote:
>To heck with UB. *How the hell does it even compile? *B is not defined
when it is declared as a member variable of A.

Sorry for misleading code. you could use pointer and forward declare a
class. something like this:

class B;

class A {
..
..
private:
B* b;
};
Now you left out the part about the initialization of the member b. If you
want

A ()
: b ( new B (*this) )
{}

then B would have to be complete again. For only initializing a pointer,
though, it is hard to see how an A object could be used.

The main point here about the state of the object which is passed with
the this pointer.
The first point is to come up with a compilable example. Only then can the
question about undefined behavior be asked meaningfully.
I would accept code any code you suggest that would convey the same
idea. The code that i presented was a place holder.
I would prefer if you could provide code. Everything we can do amounts to
guesswork.
Best

Kai-Uwe Bux
Nov 10 '08 #6
The main point here about the state of the object which is passed with
the this pointer.

The first point is to come up with a compilable example. Only then can the
question about undefined behavior be asked meaningfully.
Agreed. Was trying to build one out of the huge hierarchy already
present. Essentially, ended up butchering the concept!! My Apologies.
>
I would accept code any code you suggest that would convey the same
idea. The code that i presented was a place holder.

I would prefer if you could provide code. Everything we can do amounts to
guesswork.
Agreed.
>
Best

Kai-Uwe Bux
The current case was about initialization of a pointer, which would be
used latter. To summarize the code would look like:

class A;
class B {
public:
B(A* aa): a(aa) { }
private:
A* a;
};

class A {
public:
A(): b(new B(this)) {}
private:
B* b;
};

As pointed out by Marcel Müller, this should be okay. Although lesson
learnt is that it might not be safe to dereference it.

Thanks,
Mo
Nov 10 '08 #7

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

Similar topics

1
by: Qin Chen | last post by:
I will present very long code, hope someone will read it all, and teach me something like tom_usenet. This question comes to me when i read <<Think in C++>> 2nd, chapter 10 , name control,...
6
by: Alexander Stippler | last post by:
Hi, I wonder about the behaviour of como and icc on some very simple program. I thought initializing members of classes, which are of class type, would be 'direct initialized' (as the standard...
10
by: JKop | last post by:
What's the difference between them? Take the following: #include <iostream> struct Blah { int k;
4
by: Jacek Dziedzic | last post by:
Hello! Suppose I have a class Foo that defines a default c'tor that initializes some data using an initialization list: Foo::Foo() : member1(0), member2(0), member3(NULL), member4(20) // and...
6
by: anongroupaccount | last post by:
class CustomType { public: CustomType(){_i = 0;} CustomType(int i) : _i(i) {} private: int _i; }; class MyClass
8
by: lovecreatesbea... | last post by:
K&R 2, sec 2.4 says: If the variable in question is not automatic, the initialization is done once only, conceptually before the program starts executing, ... . "Non-automatic variables are...
23
by: Jess | last post by:
Hello, I understand the default-initialization happens if we don't initialize an object explicitly. I think for an object of a class type, the value is determined by the constructor, and for...
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...
20
by: JohnQ | last post by:
The way I understand the startup of a C++ program is: A.) The stuff that happens before the entry point. B.) The stuff that happens between the entry point and the calling of main(). C.)...
11
by: subramanian100in | last post by:
Suppose we have a class named Test. Test obj; // assuming default ctor is available Test direct_init(obj); // direct initialization happens here Test copy_init = obj; // copy initialization...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
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...
0
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,...
0
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...

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.