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 

object creation and member initialization list

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
shambhu
Guest





PostPosted: Thu Jul 06, 2006 3:32 pm    Post subject: object creation and member initialization list Reply with quote



hi ,
could any one tell me the flow of the following program .

class ABC
{
int &r ;
ABC(int a=10):r(a)
{
}
} ;

int main()
{
ABC obj;
}

My question is when is object created . if the object is created at ABC
obj ; then reference rule is violated. any answers


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Frederick Gotham
Guest





PostPosted: Fri Jul 07, 2006 3:05 am    Post subject: Re: object creation and member initialization list Reply with quote



shambhu posted:

Quote:
hi ,
could any one tell me the flow of the following program .

class ABC
{
int &r ;
ABC(int a=10):r(a)
{


You're binding a reference to local automatic object. As soon as the
constructor returns, "r" becomes invalid because the object to which it
refers has been destroyed.


Quote:
}
} ;

int main()
{
ABC obj;
}

My question is when is object created . if the object is created at ABC
obj ; then reference rule is violated. any answers


The body of "main" is entered.
A local automatic object called "obj" is constructed.
ABC::ABC(int) is invoked.
obj.r is bound to the local object in the constructor.
The local object is destroyed and the contructor returns.
"main" has exhausted all statements and now returns.



--

Frederick Gotham

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
flagos
Guest





PostPosted: Fri Jul 07, 2006 3:07 am    Post subject: Re: object creation and member initialization list Reply with quote



shambhu:

Try:

class ABC
{
public:
int &r ;
ABC(int a=10):r(a)
{
}
int get(){ return r; }
} ;

int main()
{
ABC obj;

int val = obj.get();
//what is the value of val now? why?

return 0;
}

You are constructing a reference to a stack variable that is passed by
value. When the stack unwinds, you have a reference to some undefined
place.

Hope this help.

shambhu wrote:
Quote:
hi ,
could any one tell me the flow of the following program .

class ABC
{
int &r ;
ABC(int a=10):r(a)
{
}
} ;

int main()
{
ABC obj;
}

My question is when is object created . if the object is created at ABC
obj ; then reference rule is violated. any answers


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Francis Glassborow
Guest





PostPosted: Fri Jul 07, 2006 3:08 am    Post subject: Re: object creation and member initialization list Reply with quote

In article <1152176381.344654.137480 (AT) j8g2000cwa (DOT) googlegroups.com>,
shambhu <eshambhu (AT) gmail (DOT) com> writes
Quote:
hi ,
could any one tell me the flow of the following program .

class ABC
{
int &r ;
ABC(int a=10):r(a)
{
}
} ;

int main()
{
ABC obj;
}

My question is when is object created . if the object is created at ABC
obj ; then reference rule is violated. any answers

As your code is not compilable the rules violated are syntactic and
semantic rules of C++. In this case you have tried to use a private
constructor.

However, I am a little concerned that even Comeau allows this to
compile:

struct ABC
{
int &r ;
ABC(int a=10):r(a)
{
}
} ;

int main()
{
ABC obj;
obj.r = 1;
}

However the language is covered because the resulting code has undefined
behaviour (I think you will find that a tool such as Gimpel's excellent
PCLint++ will issue a diagnostic for binding a parameter to a reference
member.


--
Francis Glassborow ACCU
Author of 'You Can Do It!' and "You Can Program in C++"
see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Victor Bazarov
Guest





PostPosted: Fri Jul 07, 2006 3:09 am    Post subject: Re: object creation and member initialization list Reply with quote

shambhu wrote:
Quote:
hi ,
could any one tell me the flow of the following program .

class ABC
{
int &r ;
ABC(int a=10):r(a)
{
}
} ;

int main()
{
ABC obj;
}

My question is when is object created .

If that's a question, then it's created when 'main' begins executing.

Quote:
if the object is created at
ABC obj ; then reference rule is violated. any answers

Yes. Right after 'obj' has been initialised, its member 'r' is invalid.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
raj.amal@gmail.com
Guest





PostPosted: Fri Jul 07, 2006 3:20 am    Post subject: Re: object creation and member initialization list Reply with quote

Can u be clear in what u are meaning as reference rule ?


shambhu wrote:
Quote:
hi ,
could any one tell me the flow of the following program .

class ABC
{
int &r ;
ABC(int a=10):r(a)
{
}
} ;

int main()
{
ABC obj;
}

My question is when is object created . if the object is created at ABC
obj ; then reference rule is violated. any answers

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
LR
Guest





PostPosted: Fri Jul 07, 2006 7:10 pm    Post subject: Re: object creation and member initialization list Reply with quote

Francis Glassborow wrote:
Quote:
In article <1152176381.344654.137480 (AT) j8g2000cwa (DOT) googlegroups.com>,
shambhu <eshambhu (AT) gmail (DOT) com> writes

hi ,
could any one tell me the flow of the following program .

class ABC
{
int &r ;
ABC(int a=10):r(a)
{
}
} ;

int main()
{
ABC obj;
}

My question is when is object created . if the object is created at ABC
obj ; then reference rule is violated. any answers


As your code is not compilable the rules violated are syntactic and
semantic rules of C++. In this case you have tried to use a private
constructor.

However, I am a little concerned that even Comeau allows this to
compile:

struct ABC
{
int &r ;
ABC(int a=10):r(a)
{
}
} ;

int main()
{
ABC obj;
obj.r = 1;
}

However the language is covered because the resulting code has undefined
behaviour (I think you will find that a tool such as Gimpel's excellent
PCLint++ will issue a diagnostic for binding a parameter to a reference
member.



It would if r was a pointer as:
class X {
int *p;
public:
X(int a=10) : p(&a) {}
};
Yields: "Assigning address of auto variable 'a' to outer scope symbol
'X::p'"

But it doesn't do it for references, although the fact that r is a
reference in ABC will be flagged.

Gimpel's website now has a feature similar to Comeau's that allows you
to try code online.

LR

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
James Hopkin
Guest





PostPosted: Sat Jul 08, 2006 4:54 am    Post subject: PC-Lint Reply with quote

Quote:

Gimpel's website now has a feature similar to Comeau's that allows you
to try code online.


Thanks for that!

Having had a play around with it, it does produce lots of useful
information, but Comeau is lot more accurate in terms of the Standard.

Just going by the interactive try-out page, it appears not to correctly
handle non-dependent names within templates, and it struggled with some
sizeof expressions I threw at it.

Still, I'm going to pipe my code through there as well as Comeau before
posting here from now on. By the way, www.tinyurl.com/hkwtn takes you
straight to the 'blank slate' page.

James


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) 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.