By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,912 Members | 1,247 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,912 IT Pros & Developers. It's quick & easy.

small operator question

P: n/a
Kev

As its not possible, as far as I know, to have more than one parameter in
an overloaded equality operator, is it possible to do something like this?

MyStruct A(1, 2, 3), B(4, 5, 6);

A = B; works;

A = MyStruct(4, 5, 6); does not

Thx

Jul 23 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
* Kev:

As its not possible, as far as I know, to have more than one parameter in
an overloaded equality operator,
It seems you mean assignment operator.

is it possible to do something like this?

MyStruct A(1, 2, 3), B(4, 5, 6);

A = B; works;
A C++ comment can look like this:

A = B; // works

A = MyStruct(4, 5, 6); does not


In general you cannot detect that an argument is a temporary, so no, you
can not make the last construction not work except for special cases --
and why would you?

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #2

P: n/a
"A = MyStruct(4, 5, 6); " should work, if you defined MyStruct's
constructor explicitly

Aries Sun

Jul 23 '05 #3

P: n/a
It worked on VC7.1 even when I made the operator= take a non-const
reference:

#include <algorithm>

struct MyStruct
{
int a;
int b;
int c;
MyStruct( int x, int y, int z )
: a(x), b(y), c(z)
{
}

MyStruct& operator=( MyStruct& rhs )
{
std::swap( a, rhs.a );
std::swap( b, rhs.b );
std::swap( c, rhs.c );
return *this;
}
};

int main()
{
MyStruct A(1,2,3), B(4,5,6);
A = B;
A = MyStruct( 4, 5, 6 );
return 0;
}

Jul 23 '05 #4

P: n/a
Kev
"Earl Purple" <ea*********@yahoo.com> wrote in
news:11**********************@g43g2000cwa.googlegr oups.com:
It worked on VC7.1 even when I made the operator= take a non-const
reference:


I did not have my contructor set up properly. Thanks everyone!
Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.