| View previous topic :: View next topic |
| Author |
Message |
DJ Franke Guest
|
Posted: Thu Sep 29, 2005 11:49 pm Post subject: Question about language compliance |
|
|
Hi !
The following code compiles on the Borland5 C++ compiler (simplified
version):
class SomeClass{
public:
SomeClass() {
Son = NULL;
}
SomeClass(const SomeClass& sc) {
Son = sc.Son;
}
SomeClass& find() { return *Son; }
SomeClass* Son;
};
SomeClass Useless = Useless.find();
In the case where I encountered it, the find function did do quite some
things and caused a very understandable runtime error, since Useless
was not intialised at that stage.
I looked at the code in the debugger and the standard constructor is
never called; the find function is called first and then the copy
constructor, which means that Useless is known and can be used before
it is initialised.
I am not very good an the language internals, so my question is wether
this is a bug in the compiler, or wether there is a good reason that
this is allowed to happen.
Thanks
|
|
| Back to top |
|
 |
Ali Çehreli Guest
|
Posted: Fri Sep 30, 2005 12:13 am Post subject: Re: Question about language compliance |
|
|
"DJ Franke" <longreef (AT) gmail (DOT) com> wrote
| Quote: | The following code compiles on the Borland5 C++ compiler (simplified
version):
[...]
SomeClass Useless = Useless.find();
In the case where I encountered it, the find function did do quite some
things and caused a very understandable runtime error, since Useless
was not intialised at that stage.
|
Your problem can be expressed in a simpler way:
SomeClass Useless = Useless;
In order to construct Useless, first the right hand side must be evaluated.
Right hand side uses the very object that you are in the process of
constructing. That object is not yet an object; it is not constructed.
The solution: don't do it. :)
Ali
|
|
| Back to top |
|
 |
DJ Franke Guest
|
Posted: Fri Sep 30, 2005 1:14 am Post subject: Re: Question about language compliance |
|
|
Hi !
Thanks for your reply.
I know that I shouldn't do it and why it doesn't do any good, I just
stumbled over it due to a typo and now wonder wether it complies with
the standard (and if then why) or wether the compiler should not allow
it.
|
|
| Back to top |
|
 |
Ali Çehreli Guest
|
Posted: Fri Sep 30, 2005 1:41 am Post subject: Re: Question about language compliance |
|
|
Summary:
Class object = use(object);
"DJ Franke" <longreef (AT) gmail (DOT) com> wrote
| Quote: | I just
stumbled over it due to a typo and now wonder wether it complies with
the standard (and if then why) or wether the compiler should not allow
it.
|
I stumbled on it a few weeks ago myself.
I think the compiler can give warnings in most (all?) of such cases, because
there is no good of using a non-constructed object to construct the same
object. A quality of implementation issue I guess... :/
Ali
|
|
| Back to top |
|
 |
|