Overloading the assignment operator, right now!
Sean Kelly
sean at f4.ca
Fri Sep 22 15:05:38 PDT 2006
Bruno Medeiros wrote:
>
> One must be wary of what Oskar mentioned: that the assignment operator
> is not the only assignment, there is also the implicit assignment of a
> function call's argument passing. I think this can be easily solved by
> having the function call use the copy operator, if it is overloaded, for
> argument creation. Hum.., now that this is mentioned, how does it work
> in C++? Does C++ do it that way?
Pretty much. For example:
class C
{
public:
C(int i) : x(i) {}
C(C const& o) : x(0) {}
int x;
};
void f1(C val) { printf("%d\n", val.x); }
void f2(C* val) { printf("%d\n", val->x); }
void f3(C& val) { printf("%d\n", val.x); }
C c(1); f1(c); f2(&c); f3(c);
All of the above functions pass their parameters by value (just as in D)
but only the f1 has C as a parameter. f2 and f3 accept a pointer and a
reference to C, respectively. So calling f1 will create a copy of C via
C's copy ctor, while f2 and f3 will copy the address of C and end up
referencing the original object. So executing the above should print:
0
1
1
Sean
More information about the Digitalmars-d
mailing list