|
-
April 27th, 2012, 06:48 AM
#1
Assignment Operator
Hi,
What is the advantage of conversion function over assignment operator overloading.
and why there is no return type in the conversion function though it returns an object
-
April 27th, 2012, 08:31 AM
#2
Re: Assignment Operator
Conversion functions (usually called cast functions) are for a completely different purpose than the assignment operator. The assignment operator assigns an object to another object of the same type. The cast function converts it to another type. And cast functions certainly do have return types, and they do not have to return objects.
Code:
struct foo {
foo & operator = (const foo & other){ bar = other.bar; } //assignment
operator float(void){ return (float)bar; } //cast
private:
int bar;
};
foo a;
foo b;
a = b; //assignment
float f = (float)a; //cast
-
April 27th, 2012, 09:31 AM
#3
Re: Assignment Operator
 Originally Posted by Rajesh1978
and why there is no return type in the conversion function though it returns an object
 Originally Posted by ninja9578
And cast functions certainly do have return types, and they do not have to return objects.
Well lets try to not confuse the OP.
A cast operator does not declare the return type in its signature, because it is already included in the name. It is just a syntax quirk:
This will cast to char*. It WILL return an "object", which will be of the type of the cast (here, it will return a char*). Note that the term "object" is used in a broad sense here, and can be a class, a pointer, a reference, etc...
----
It is actually usually considered bad design to provide cast operators, as it can lead to unexpected call. Explicit conversion is usually regarded as a superior alternative.
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
April 27th, 2012, 01:13 PM
#4
Re: Assignment Operator
Correct me if I'm wrong, can't you declare the cast operator with "explicit" like you do with constructors to avoid those unexpected calls?
-
April 27th, 2012, 01:38 PM
#5
Re: Assignment Operator
 Originally Posted by ninja9578
Correct me if I'm wrong, can't you declare the cast operator with "explicit" like you do with constructors to avoid those unexpected calls?
Only with C++11. So (virtually) no one is using it yet.
The stl uses it for streams, with explicit casts to bool.
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
April 30th, 2012, 04:58 PM
#6
Re: Assignment Operator
 Originally Posted by ninja9578
The assignment operator assigns an object to another object of the same type.
That's not correct. An assignment operator can take a different type of RHS than the type of the LHS, i.e. the assignment operator can be overloaded. When both types are the same, we are actually talking about the copy assignment operator. However, since other overloads of the assignment operator are used so rarely, most people just talk about the assignment operator when they mean the copy assignment operator.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
May 1st, 2012, 01:56 PM
#7
Re: Assignment Operator
I see, I assumed you could use it with different parameters that just a const reference to it's own type, I've just never seen it. This seems dangerous for the same reason cast operators are, possible unexpected calls to it.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|