Topic: Confused by the standard Was: Reference initialization question -- is it safe?
Author: "Bo-Staffan Lankinen" <bSoP_AsMteSfUfCaKnS_lankinen@hotmail.com>
Date: 26 Mar 2001 23:06:15 -0500 Raw View
> >class Derived : private Variable, public Base {
> > Derived(...) : Variable(...), Base(...) { }
> >};
> >
> >Because we derive from Variable first, it is initialized first.
Therefore
>I always knew that - I always did so!
> >we can pass it -- either a reference to the Variable or a pointer to the
>It's exactly what the question is about - can we pass the reference?
>The standard explicitly forbids this in 3.8.
> >Variable -- down to the base class constructor. This constructor can use
> >the Variable, and safely store the reference/pointer.
>
> Intuitively, yes. According to standard, no. I'm very shocked.
If I understand the standard correctly, 3.8 describes the behavior after the
storage is allocated but before the constructor or after the destructor is
executed, ie.
struct x {int i;};
struct y : public x {};
extern y obj;
const x& base=obj; //The implicit conversion of y->x of un-constructed but
allocated obj, results in undefined behavior.
y obj;
void foo() {
y* pobj=new y;
pobj->~y();
const x& base=*pobj; //The implicit conversion of y->x of un-constructed
but allocated pobj, results in undefined behavior.
}
If you want to know the restrictions DURING the execution of the constructor
then refer to 12.6.2 and 12.7. As I read it, it's indeed well defined to
pass a pointer/reference to Variable when invoking the constructor of Base.
Bo-Staffan
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: jk@steel.orel.ru (Eugene Karpachov)
Date: 23 Mar 01 02:36:22 GMT Raw View
21 Mar 2001 07:04:41 -0500 Siemel Naran wrote:
>"Eugene Karpachov" <jk@steel.orel.ru> wrote in message
>
>> It still unclear to me - can I use "Dietmar Kuehl trick" about inheritance
>as
>> I always did? Isn't it directly forbidden by the standard? Is calling base
>> member function from constructor body directly forbidden by the standard?
>
>Yes, Dietmar Kuehl's trick about inheritance works, as always. To
>summarize, it is
>
>class Derived : private Variable, public Base {
> Derived(...) : Variable(...), Base(...) { }
>};
>
>Because we derive from Variable first, it is initialized first. Therefore
I always knew that - I always did so!
>we can pass it -- either a reference to the Variable or a pointer to the
It's exactly what the question is about - can we pass the reference?
The standard explicitly forbids this in 3.8.
>Variable -- down to the base class constructor. This constructor can use
>the Variable, and safely store the reference/pointer.
Intuitively, yes. According to standard, no. I'm very shocked.
>Calling a base class member function from the derived class initialization
>list or constructor body is allowed. But it is not in question here, if I'm
>not mistaken.
Yes, it is in question - calling base class member function from the derived
class constructor can be considered as implicit conversion of "this" to
pointer to base class, which is explicitly forbidden by the standard. Again,
intuitively we can do it, but according to standard we cannot. Am I
misunderstanding 3.8? Is there any open issue, or defect report, or something
about this?
--
jk
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]