What exactly does xyz (as an example) in var(xyz) do? i have been unable to find anything that might tell me and was curious as to what it did. Any help would be appriciated.
Many thanks,
Fussion
What exactly does xyz (as an example) in var(xyz) do? i have been unable to find anything that might tell me and was curious as to what it did. Any help would be appriciated.
Many thanks,
Fussion
>>What exactly does xyz (as an example) in var(xyz) do?
In what context?
*Cela*
As in
int xyz = 200;
int var(xyz);
?
If so, it does the exact same thing as
int var = xyz;
But only for built-in types
User for user defined types (classes, structs) assigning a variable like this calls the copy constructor, as opposed to the default constructor, and assignment operator.
As an example, try running this:
For built in types, it doesn't matter which way you assign. For user defined types, prefer the first ( var(anothervar) ) as you only invoke a single function call.Code:class contact { public: contact() {std::cout << "default constructor"; } contact(const contact &rhs) : m_number(rhs.m_number), m_name(rhs.m_name) { std::cout << "copy constructor"; } contact(const std::string &name, int number) : m_number(number), m_name(name) { std::cout << "overloaded construtor"; } private: int m_number; std::string m_name; };
Last edited by Eibro; 01-19-2003 at 06:14 PM.
That's not true at all!Originally posted by Eibro
But only for built-in types
User for user defined types (classes, structs) assigning a variable like this calls the copy constructor, as opposed to the default constructor, and assignment operator.
...
For built in types, it doesn't matter which way you assign. For user defined types, prefer the first ( var(anothervar) ) as you only invoke a single function call.
The = in a declaration with initialization is purely cosmetic. The copy constructor is called, not the default and assignment.
Both explicitly specifying the copy constructor and using = do exactly the same thing.
Hmm I wonder; is this a compiler optimization or does it state somewhere in the standard that the result is a call to the copy constructor?
Anyway, good to know. Thanks for clearing that up.
According to the standard the copy constructor is called.
type variable = something;
is initialisation.
variable = something;
is assignment.
Copy constructor called in initialisation and assignment operator in assignment. Learn to spot the difference between the two.
Free the weed!! Class B to class C is not good enough!!
And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi
A copy constructor is calledOriginally posted by Stoned_Coder
type variable = something;
is initialisation.
variable = something;
is assignment.
Copy constructor called in initialisation and assignment operator in assignment. Learn to spot the difference between the two.
1: Pass by value
2: Return by value
3: Creation of one object using another object // Initialization
Well having said that, I just can't understand why a default constructor is called in this case
Imagine that X is a class
X(); // Creates an object and this object dies at the end of this statement
X ob = X(); // calls the default constructor only
It does not call the copy constructor nor the assignment operator.
Have a wonderful day.... and keep smiling... you look terrific that way
signing off...
shiv... as i know him
Yes it does. It calls the default constructor for the X() part and then the copy constructor for ob's initialization.Originally posted by shiv_tech_quest
Well having said that, I just can't understand why a default constructor is called in this case
Imagine that X is a class
X(); // Creates an object and this object dies at the end of this statement
X ob = X(); // calls the default constructor only
It does not call the copy constructor nor the assignment operator.
Hi Poly,
I had tried this few years ago, when learning copy / default constructor, can't recollect the compiler. It didn't behave this way. So was always wondering why it didn't work the way I had predicted (default first followed by copy), but just tried the same now on MSVC++6 and worked correctly.... hmmmmmmm what more can i say
Have a wonderful day.... and keep smiling... you look terrific that way
signing off...
shiv... as i know him