Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Initialization with this.

Reply
Thread Tools

Initialization with this.

 
 
Morya
Guest
Posts: n/a
 
      11-10-2008
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
 
Reply With Quote
 
 
 
 
red floyd
Guest
Posts: n/a
 
      11-10-2008
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.
 
Reply With Quote
 
 
 
 
Morya
Guest
Posts: n/a
 
      11-10-2008
> 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.
 
Reply With Quote
 
zr
Guest
Posts: n/a
 
      11-10-2008
On Nov 10, 8:03*am, Morya <mda...@gmail.com> 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 ?
>
> ~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

};
 
Reply With Quote
 
Marcel Müller
Guest
Posts: n/a
 
      11-10-2008
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
 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      11-10-2008
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
 
Reply With Quote
 
Morya
Guest
Posts: n/a
 
      11-10-2008
> > 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
what's the difference between value-initialization and default-initialization? Jess C++ 23 05-04-2007 03:03 AM
array initialization in initialization list. toton C++ 5 09-28-2006 05:13 PM
Initialization of non-integral type in initialization list anongroupaccount@googlemail.com C++ 6 12-11-2005 09:51 PM
Initialization via ctor vs. initialization via assignment Matthias Kaeppler C++ 2 07-18-2005 04:25 PM
Default Initialization Vs. Value Initialization JKop C++ 10 09-22-2004 07:26 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57