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

implicit copy assignment operator

P: n/a
Dear C++ Afficionados,

I know the compiler will generate a copy assignment operator (amongst
other fn's) if you don't provide one in your class declaration.

But the parameter types can vary, and the compiler decides between
const, reference, and (with xlC) volatile.

Is there any way to dump the object file and see what the compiler
generated? I couldn't see any switches for this. It would be helpful
to see I think.

Thanks much. Sincerely, George

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


P: n/a
Better to just define your own. That way you know what you're getting.

Jul 23 '05 #2

P: n/a

George wrote:
Dear C++ Afficionados,

I know the compiler will generate a copy assignment operator (amongst
other fn's) if you don't provide one in your class declaration.

But the parameter types can vary, and the compiler decides between
const, reference, and (with xlC) volatile.

Is there any way to dump the object file and see what the compiler
generated? I couldn't see any switches for this. It would be helpful to see I think.

Thanks much. Sincerely, George


Jul 23 '05 #3

P: n/a
George wrote:
I know [that] the compiler will generate a copy assignment operator
(among other functions)
if you don't provide one in your class declaration.
No. For class X, yhe compiler will supply

1. an assignment operator,

X& X::operator=(const X&);

2. a *default* constructor

X::X(void);

3. ans a copy constructor

X::X(const X&);

by default.
But the parameter types can vary,
If you need an assignment operator or default constructor
which accepts any arguments other that those above,
you must define them yourself.
and the compiler decides between
const, reference, and (with xlC) volatile.

Is there any way to dump the object file
and see what the compiler generated?


The typical compiler
never actually creates callable functions for them
but inlines them automatically.
Jul 23 '05 #4

P: n/a
I did,

T& operator=(const T&)

But the compiler wouldn't take it. So after several attempts, it was
happy without the const and the &.

There are 5 possible signatures, (at least with xlC). I suppose the
best solution is to define all 5.

-g

Jul 23 '05 #5

P: n/a
In message <11**********************@c13g2000cwb.googlegroups .com>,
George <ge**********@excite.com> writes
I did,

T& operator=(const T&)

But the compiler wouldn't take it.
?? It should take whatever you give it.

Post actual code, and the compiler's error messages.
So after several attempts, it was
happy without the const and the &.
Which would imply taking a copy before you perform the assignment.
There are 5 possible signatures, (at least with xlC). I suppose the
best solution is to define all 5.

No. The best solution is to say what you mean.

--
Richard Herring
Jul 23 '05 #6

P: n/a
Oops, my bad. "An rvalue cannot be converted to a reference to a
non-const type."

I'd overlooked that compiler message.

Thanks for all the help. Sincerely,

George

---------------------------------------------------------------------
// This fails to compile, messages are:
//
// The call does not match any parameter list for "operator="
// "P::operator=(P&)" is not a viable candidate.
// A parameter of type "P&" cannot e initalized with an rvalue of type
"P"
// An rvalue cannot be converted to a reference to a non-const type
//
// Compile with xlC -c <thisfile>

class P{
public:

explicit P(int i);
P(P& p);
P& operator=(P&);
void swishswish();
private:
int data;
};
inline P::P(int i) : data(i){ }
inline P::P(P& p):data(p.data) { }
inline P& P::operator=(P& p){ data = p.data; return *this;}
void P::swishswish(){ data = 89; }

int main()
{
P iP(2);
iP = static_cast<P>(3);
iP.swishswish();

return 0;
}

Jul 23 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.