Omer Zak wrote:
Explicit means that C++ should not use this constructor as an implicit cast operator. If that were not the case, I could pass an int where a function expects a "class a". Sometimes you want that. Sometimes you don't. In this case, I don't.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++?
Take the program as is. It's a compilable program (my problem non-withstanding). It won't link because some functions are not defined, but that's not the problem here.a &operator= ( a &that );
};
How are the variables in this class declared (if there are any variables
at all)?
Well, this is a reduced version of my full blown program, obviously. Let's just say that this optimization you mention will not happen there at any rate. In any case, I'm wondering WHY temporary implicit variables should be considered const. It's clear that the compiler does consider them like that.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.
If I want constant expressions, I usually just define them "static const" - in your example: "static const complex ANGLE45( 0.707, 0.707 );" This both tells the compiler what I meant, makes sure readonly static area is used, and is clearer to the reader what I meant (remeber the "no magic values" golden rule).
Consider what you would have wished to happen if you had usedI also checked how auto_ptr does that. It seems it's using a proxy class to pass the body around. Damn complicated, if you ask me.
complex(0.707,0.707) instead of your own a.
Shachar
-- Shachar Shemesh Lingnu Open Source Consulting ltd. http://www.lingnu.com/
================================================================= 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]