[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

Initialization with this.

Morya

11/10/2008 6:03:00 AM

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
8 Answers

red floyd

11/10/2008 6:29:00 AM

0

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.

Morya

11/10/2008 6:35:00 AM

0

> 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.

zr

11/10/2008 7:28:00 AM

0

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

};

Marcel Müller

11/10/2008 7:30:00 AM

0

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

Kai-Uwe Bux

11/10/2008 7:34:00 AM

0

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

Morya

11/10/2008 8:00:00 AM

0

> > 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

Rich Rostrom

6/25/2014 7:13:00 PM

0

Alex Milman <alexmilman@msn.com> wrote:

> At the time when Constitution had been drafted slavery was considered a
> quite normal thing so why would it be mentioned at all?

It wasn't really "normal" anymore.

Jefferson wrote in 1785:

"And can the liberties of a nation be thought secure
when we have removed their only firm basis, a
conviction in the minds of the people that these
liberties are of the gift of God? That they are not to
be violated but with his wrath? Indeed I tremble for
my country when I reflect that God is just: that his
justice cannot sleep for ever: that considering
numbers, nature and natural means only, a revolution
of the wheel of fortune, an exchange of situation, is
among possible events: that it may become probable by
supernatural interference!"

In other words: how can we be secure in our liberty,
which we think to be a gift of God, when we openly
violate the liberties of others? Do we not thereby
incur God's just wrath? The slaves are physically
capable of revolution - and God being just may aid them!

Benjamin Franklin was anti-slavery; so were Alexander
Hamilton, John Jay, and John Adams. George Washington
freed his slaves by testament.
--
The real Velvet Revolution - and the would-be hijacker.

http://originalvelvetrevo...

Jerry Kraus

6/25/2014 7:38:00 PM

0

On Wednesday, June 25, 2014 2:12:59 PM UTC-5, Rich Rostrom wrote:
> Alex Milman <alexmilman@msn.com> wrote:
>
>

>
> Jefferson wrote in 1785:
>
>
>
> "And can the liberties of a nation be thought secure
>
> when we have removed their only firm basis, a
>
> conviction in the minds of the people that these
>
> liberties are of the gift of God? That they are not to
>
> be violated but with his wrath? Indeed I tremble for
>
> my country when I reflect that God is just: that his
>
> justice cannot sleep for ever: that considering
>
> numbers, nature and natural means only, a revolution
>
> of the wheel of fortune, an exchange of situation, is
>
> among possible events: that it may become probable by
>
> supernatural interference!"
>
>
>
> In other words: how can we be secure in our liberty,
>
> which we think to be a gift of God, when we openly
>
> violate the liberties of others? Do we not thereby
>
> incur God's just wrath? The slaves are physically
>
> capable of revolution - and God being just may aid them!
>
>


Strong words indeed -- from an atheist!!!

To quote Harry Truman in reference to Richard Nixon:

"He can lie out of both sides of his mouth at the same time, and, if he ever caught himself telling the truth he'd lie just to keep his hand in."

Jefferson's most notable dealing with Slaves was his pedophilic seduction of Sally Hemings. He is, as far as we know, the only founding father to have fathered children with his slaves! Verrrrry egalitarian.