"Omer Zak" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
> Replying to myself, because I forgot one more point:
> From what I remember about C++, you need also a copy constructor in this
> case,

In this case a copy constructor is not invoked.

> because you strive to copy a value to a variable (and in this
> special case, the value is a constant instance of a class).
>
> On Sat, 29 Jan 2005, Omer Zak wrote:
>
> >
> >
> > On Sat, 29 Jan 2005, Shachar Shemesh wrote:
> >
> > > Hi all,
> > >
> > > Here is a small program for your viewing pleasure:
> > >
> > > > class a {
> > > > public:
> > > > explicit a(int param);
> >
> > What is the meaning of 'explicit' declaration?
> > Is this a C++ keyword which was added since I learned C++?
> >
> >
> > > >
> > > > a &operator= ( a &that );
> > > > };
> >
> > How are the variables in this class declared (if there are any variables
> > at all)?
> >
> >
> > > >
> > > > int main()
> > > > {
> > > > a var1(3);
> > > >
> > > > var1=a(5);
> > > >
> > > > return 0;
> > > > }
> > >
> > > Somewhat surprisingly, this does not compile:
> > > g++ -Wall -gtestcompile.cc -o testcompile
> > > testcompile.cc: In function `int main()':
> > > testcompile.cc:12: error: no match for 'operator=' in 'var1 = a(5)'
> > > testcompile.cc:5: error: candidates are: a& a::operator=(a&)
> > > make: *** [testcompile] Error 1
> > >
> > > There are two things that can make it compile. One is to add a "const"
> > > at the "operator=" definition, and the other is to use an explicit
> > > variable (i.e. - not a temporary one).
> > >
> > > The reason for this failure seems to be that g++ treats temporary
> > > variables as consts. I see neither reason nor logic for this decision,
> > > however. Why can't I modify temporary variables if I so wish? Don't they
> > > have a well definedlife span (until the end of the statement) for a 
> > > reason?
> >
> > My guess is that the language allows the temporary a(5) to be compiled as
> > a constant and stored in read-only part of the program.
> >
> > Consider what you would have wished to happen if you had used
> > complex(0.707,0.707) instead of your own a.
>
>                                              --- Omer
[snip]


-- 
 Alex Vinokur
     email: alex DOT vinokur AT gmail DOT com
     http://mathforum.org/library/view/10978.html
     http://sourceforge.net/users/alexvn





=================================================================
To unsubscribe, send mail to [EMAIL PROTECTED] with
the word "unsubscribe" in the message body, e.g., run the command
echo unsubscribe | mail [EMAIL PROTECTED]

Reply via email to