Usage of pointer to object under construction

D

Dave

Hello all,

In the code below, I use a pointer to an object under construction. Is the
usage below legal? I have come across similar code at work. It compiles,
but I'm not sure it's really legal...

Thanks,
Dave

struct D;

struct B
{
B(D *);
};

struct D: B
{
D(): B(this) {}
};
 
V

Victor Bazarov

Dave said:
In the code below, I use a pointer to an object under construction. Is the
usage below legal? I have come across similar code at work. It compiles,
but I'm not sure it's really legal...

Thanks,
Dave

struct D;

struct B
{
B(D *);
};

struct D: B
{
D(): B(this) {}
};


There is no _usage_ here. To show _usage_ you need to post the _body_ of
the B's constructor.

Victor
 
D

Dave

Victor Bazarov said:
There is no _usage_ here. To show _usage_ you need to post the _body_ of
the B's constructor.

Victor

Your response implies the code, as much of it as was shown, is legal. There
may or may not be something illegal going on in B::B(), but there also may
or may
not being something illegal going on in
SomeClassInAnotherProgram::SomeClassInAnotherProgram().
It wasn't really the point, hence the omission. Of course, I cannot invoke
member
functions, for just one thing, via the pointer because those member
functions may
refer to object state not yet itself constructed.

But let's suppose for the sake of argument that B::B() is as shown below.
Perhaps
my question should have been: Is it legal to __mention__ a pointer to an
object
under construction?

struct D;

struct B
{
B(D *p): ptr_to_superobject(p) {}
D *ptr_to_superobject;
};

struct D: B
{
D(): B(this) {}
};
 
V

Victor Bazarov

Dave said:
Your response implies the code, as much of it as was shown, is legal. There
may or may not be something illegal going on in B::B(), but there also may
or may
not being something illegal going on in
SomeClassInAnotherProgram::SomeClassInAnotherProgram().
It wasn't really the point, hence the omission. Of course, I cannot invoke
member
functions, for just one thing, via the pointer because those member
functions may
refer to object state not yet itself constructed.

But let's suppose for the sake of argument that B::B() is as shown below.
Perhaps
my question should have been: Is it legal to __mention__ a pointer to an
object
under construction?

By the time the constructor's initialiser list is being processed (base
class objects and members are being initialised), the storage for the
constructed object has been allocated and 'this' has a value that points
to that storage (and, of course, is a valid pointer). If all you want to
do is to store it elsewhere, it's fine because it's not going to change
for the lifetime of the object.
struct D;

struct B
{
B(D *p): ptr_to_superobject(p) {}
D *ptr_to_superobject;
};

struct D: B
{
D(): B(this) {}
};

Yes, that's fine.

Beware, though, that storing a pointer to a non-const object which is
really constructed as 'const' may be dangerous. But something tells me
that it's not really the case here :)

Victor
 
D

Dave

Victor Bazarov said:
By the time the constructor's initialiser list is being processed (base
class objects and members are being initialised), the storage for the
constructed object has been allocated and 'this' has a value that points
to that storage (and, of course, is a valid pointer). If all you want to
do is to store it elsewhere, it's fine because it's not going to change
for the lifetime of the object.


Yes, that's fine.

Beware, though, that storing a pointer to a non-const object which is
really constructed as 'const' may be dangerous. But something tells me
that it's not really the case here :)

Victor

Yes, storage has been allocated and, realistically, this will *probably* be
fine in the real world. But my concern lies with the fact that I'm passing
around pointers to an object that does not yet exist. Until the constructor
finishes, the object does not exist, period. What does the Standard say
about this?
 
V

Victor Bazarov

Dave said:
Yes, storage has been allocated and, realistically, this will *probably* be
fine in the real world. But my concern lies with the fact that I'm passing
around pointers to an object that does not yet exist. Until the constructor
finishes, the object does not exist, period. What does the Standard say
about this?

The Standard says that the member initialisers are evaluated in the scope
of the constructor. 'this' designates the object _under_construction_.
Calling member functions, applying 'typeid' operator or 'dynamic_cast' to
the "this" pointer cause undefined behaviour _if_ done before all base
class initialisers have completed. Also, conversion from X to B where B
is a base class of X can only be done once all subobjects of class X that
derive from B have been constructed and have not begun destructing. That
means that any other operation involving 'this' is _OK_. Of course that
includes a simple _copying_ of it.

If you need more information, search groups.google.com for discussions on
the use of 'this' in the initialiser lists. I am really not up to
repeating it all here.

V
 
P

puzzlecracker

the snippet looks legal to me.... pointers only need a declaration -
and you have that, for it is a static size_t they occupy.
 
D

Default User

puzzlecracker said:
the snippet looks legal to me.... pointers only need a declaration -
and you have that, for it is a static size_t they occupy.

Please don't top-post.

Also, Dave and Victor, could you guy please TRIM your quotes a bit?
This sort of screen after screen of up to five deep quotes is exactly
the sort of thing that fuels the top-posters.




Brian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top