Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > this used in initialiser

Reply
Thread Tools

this used in initialiser

 
 
Fraser Ross
Guest
Posts: n/a
 
      06-25-2009
class base {
public:
base(void *) {}
};

class der : base {
public:
der() :base(this) {}
};

Does this have any defined value when used in a base class initialiser?
Only 1 out of 3 compilers have given a warning.

Fraser.


 
Reply With Quote
 
 
 
 
Fraser Ross
Guest
Posts: n/a
 
      06-25-2009
I think it points to an uninitialised sub-object. It needs to be used
with some care.

Fraser.


 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      06-25-2009
Fraser Ross wrote:
> class base {
> public:
> base(void *) {}
> };
>
> class der : base {
> public:
> der() :base(this) {}
> };
>
> Does this have any defined value when used in a base class initialiser?


Absolutely.

It points to the memory where the respective object resides (or will
reside once initialised).

> Only 1 out of 3 compilers have given a warning.


You are not prohibited from using 'this' in the initialiser list. Just
be careful, the object hasn't been fully initialised yet.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      06-26-2009
On Jun 25, 7:25 pm, "Fraser Ross" <z...@zzzzzz.com> wrote:
> class base {
> public:
> base(void *) {}
> };


> class der : base {
> public:
> der() :base(this) {}
> };


> Does this have any defined value when used in a base class
> initialiser?


Yes, but it might not be what is wanted. The implicit
conversion to void* is very dangerous here; not only can base
not use the pointer in its constructor, it must convert it to
der* in order to use it later.

--
James Kanze (GABI Software) email:
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      06-26-2009
On Jun 25, 9:29 pm, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
> Fraser Ross wrote:
> > class base {
> > public:
> > base(void *) {}
> > };


> > class der : base {
> > public:
> > der() :base(this) {}
> > };


> > Does this have any defined value when used in a base class
> > initialiser?


> Absolutely.


> It points to the memory where the respective object resides
> (or will reside once initialised).


It points to the memory where the der object will reside. The
only way the base class can correctly use it is by reconverting
it to a der*.

--
James Kanze (GABI Software) email:
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
Fraser Ross
Guest
Posts: n/a
 
      06-26-2009

>"James Kanze"
>On Jun 25, 7:25 pm, "Fraser Ross"
>> class base {
>> public:
>> base(void *) {}
>> };


>> class der : base {
>> public:
>> der() :base(this) {}
>> };


>> Does this have any defined value when used in a base class
>> initialiser?


>Yes, but it might not be what is wanted. The implicit
>conversion to void* is very dangerous here; not only can base
>not use the pointer in its constructor, it must convert it to
>der* in order to use it later.



It could also be converted later to base*.

I think my real code might violate 12.7/3. Where it says "might use
path E* -> D*-> A*" is it implying that conversions from E* to C* and D*
are equally good because they are directly inherited base classes? If I
remove C, B and X from the example and force the conversions of E* ->
D*-> A* the Comeau compiler gives no diagnostics but the standard is
saying its undefined behaviour.

struct A { };

struct D : virtual A {
D(A*);
};

struct E : D {
E() : D(this) {}// undefined: upcast from E* to A* might use path E* !
D* ! A*
};

Fraser.


 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      06-26-2009
On Jun 26, 2:20 pm, "Fraser Ross" <z...@zzzzzz.com> wrote:
> >"James Kanze"
> >On Jun 25, 7:25 pm, "Fraser Ross"
> >> class base {
> >> public:
> >> base(void *) {}
> >> };
> >> class der : base {
> >> public:
> >> der() :base(this) {}
> >> };
> >> Does this have any defined value when used in a base class
> >> initialiser?

> >Yes, but it might not be what is wanted. The implicit
> >conversion to void* is very dangerous here; not only can base
> >not use the pointer in its constructor, it must convert it to
> >der* in order to use it later.


> It could also be converted later to base*.


If you don't mind undefined behavior. The only legal use of a
void* is converting it back to the original type. Here, the
original type is der*, not base*.

> I think my real code might violate 12.7/3. Where it says
> "might use path E* -> D*-> A*" is it implying that conversions
> from E* to C* and D* are equally good because they are
> directly inherited base classes? If I remove C, B and X from
> the example and force the conversions of E* -> D*-> A* the
> Comeau compiler gives no diagnostics but the standard is
> saying its undefined behaviour.


You'll have to show use your real code. My comments concerning
undefined behavior mainly concerned the passage through a void*.
The examples in 12.7/3 don't have any void*. §12.7/2 does
guarantee that you could pass your this to a base*, but it
doesn't say anything about a void*.

> struct A { };


> struct D : virtual A {
> D(A*);
> };


> struct E : D {
> E() : D(this) {}// undefined: upcast from E* to A* might use path E* !
> D* ! A*
> };


I'm not sure vis-a-vis the standard, but I do know that this
fails with certain compilers.

Curiously, you can call member functions of A in the same
context (and calling such functions requires the same
conversion). And the member function can return its this
pointer.

--
James Kanze (GABI Software) email:
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
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
Colon initialiser? alan.larkin@gmail.com C Programming 11 01-25-2007 02:41 AM
Reference + initialiser oddity. Glen Able C++ 3 01-29-2005 01:21 AM
Why no non-integral static const initialiser's within class definition? Mike Hewson C++ 14 01-07-2005 08:43 AM
Dereferencing this in constructors initialiser list Martin Zimmermann C++ 2 04-14-2004 02:07 PM
calling member functions from an initialiser list John Harrison C++ 2 06-25-2003 06:25 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