C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Question about language compliance

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
DJ Franke
Guest





PostPosted: Thu Sep 29, 2005 11:49 pm    Post subject: Question about language compliance Reply with quote



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





PostPosted: Fri Sep 30, 2005 12:13 am    Post subject: Re: Question about language compliance Reply with quote



"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





PostPosted: Fri Sep 30, 2005 1:14 am    Post subject: Re: Question about language compliance Reply with quote



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





PostPosted: Fri Sep 30, 2005 1:41 am    Post subject: Re: Question about language compliance Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.