473,320 Members | 1,961 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,320 software developers and data experts.

Usage of pointer to object under construction

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

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

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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jim Campbell | last post by:
Suppose I wanted to a class to contain a pointer, but I didn't want to make the copy constructor or assignment operator reinitialize this pointer. (Think of a class that contains an mmapped pointer...
11
by: Squid Seven | last post by:
I create a pointer to an item: CardSession *cardSession; Then, later, I use new to create an instance of the item and assign it to that pointer: cardSession = new CardSession(); In...
48
by: Michael Sig Birkmose | last post by:
Hi everyone! Does anyone know, if it is possible to meassure the maximum stack usage of a C program throughout it's entire execution? -- Michael Birkmose
51
by: Kuku | last post by:
What is the difference between a reference and a pointer?
4
by: craig | last post by:
During construction of an object "parent", if you create a subobject that stores a pointer to the parent (through the "this" pointer), will that pointer be valid when the subobject is later called?...
42
by: xdevel | last post by:
Hi, if I have: int a=100, b = 200, c = 300; int *a = {&a, &b, &c}; than say that: int **b is equal to int *a is correct????
33
by: Ney André de Mello Zunino | last post by:
Hello. I have written a simple reference-counting smart pointer class template called RefCountPtr<T>. It works in conjunction with another class, ReferenceCountable, which is responsible for the...
3
by: Nindi73 | last post by:
Hi, I am in need of a deep copy smart pointer (Boost doesn't provide one) which doesnt require the contained types to have a virtual copy constructor. I wrote a smart pointer class that I think...
11
by: copx | last post by:
Unforuntately, I know next to nothing about ASM and compiler construction, and while I was aware of the syntactic differences between pointers and arrays, I was not aware of this: ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.