Yes, explicit keyword is here long time ago. Get a C++ book from more details.
At the moment, C++ decides that binding a non-const ref to a
temporary is wrong. Thats the problem.
Kobi.
On Sat, 29 Jan 2005 23:04:52 +0200 (EET), Omer Zak <[EMAIL PROTECTED]> 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 -g testcompile.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 defined life 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
> My opinions, as expressed in this E-mail message, are mine alone.
> They do not represent the official policy of any organization with which
> I may be affiliated in any way.
> WARNING TO SPAMMERS: at http://www.zak.co.il/spamwarning.html
>
>
> =================================================================
> 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]
>
>
=================================================================
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]