| View previous topic :: View next topic |
| Author |
Message |
shambhu Guest
|
Posted: Thu Jul 06, 2006 3:32 pm Post subject: object creation and member initialization list |
|
|
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
|
Posted: Fri Jul 07, 2006 3:05 am Post subject: Re: object creation and member initialization list |
|
|
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
|
Posted: Fri Jul 07, 2006 3:07 am Post subject: Re: object creation and member initialization list |
|
|
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
|
Posted: Fri Jul 07, 2006 3:08 am Post subject: Re: object creation and member initialization list |
|
|
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
|
Posted: Fri Jul 07, 2006 3:09 am Post subject: Re: object creation and member initialization list |
|
|
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
|
Posted: Fri Jul 07, 2006 3:20 am Post subject: Re: object creation and member initialization list |
|
|
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
|
Posted: Fri Jul 07, 2006 7:10 pm Post subject: Re: object creation and member initialization list |
|
|
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
|
Posted: Sat Jul 08, 2006 4:54 am Post subject: PC-Lint |
|
|
| 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 |
|
 |
|