By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,446 Members | 3,090 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,446 IT Pros & Developers. It's quick & easy.

Usage of pointer to object under construction

P: n/a
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) {}
};
Jul 22 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Dave wrote:
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
Jul 22 '05 #2

P: n/a

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:ZG*******************@newsread1.mlpsca01.us.t o.verio.net...
Dave wrote:
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


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::SomeClassInAnotherProgr am().
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) {}
};

Jul 22 '05 #3

P: n/a
Dave wrote:
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:ZG*******************@newsread1.mlpsca01.us.t o.verio.net...
Dave wrote:
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

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::SomeClassInAnotherProgr am().
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
Jul 22 '05 #4

P: n/a

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:vj******************@newsread1.mlpsca01.us.to .verio.net...
Dave wrote:
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:ZG*******************@newsread1.mlpsca01.us.t o.verio.net...
Dave wrote:

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_ ofthe 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::SomeClassInAnotherProgr am().
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


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?
Jul 22 '05 #5

P: n/a
Dave wrote:
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:vj******************@newsread1.mlpsca01.us.to .verio.net...
Dave wrote:
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:ZG*******************@newsread1.mlpsca01.u s.to.verio.net...
Dave wrote:
>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
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::SomeClassInAnotherPr ogram().
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

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
Jul 22 '05 #6

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

Victor Bazarov wrote:
Dave wrote:
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:ZG*******************@newsread1.mlpsca01.us.t o.verio.net...
Dave wrote:

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_ ofthe 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::SomeClassInAnotherProgr am().
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


Jul 22 '05 #7

P: n/a

puzzlecracker wrote:
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

Jul 22 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.