Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Kranar

Assignment operator in C++

This topic is 5225 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Okay, I have a class and have overrided the assignment operator, but it seems to not do anything when I initialize an instance of my class, only after it''s been initialized. For example...
myClass x;
myClass y;
y = x;
My assignment operator is being used in this case...
myClass x;
myClass y = x;
My assignment operator isn''t being used. What is being used in this case and how can I override it?

Share this post


Link to post
Share on other sites
Advertisement
In that particular case the copy-constructor is called.
It takes the following form:

X(const X& x);

SO for myClass it would be:
myClass(const myClass& ref);


Edit: I wanted to add that the following would do the same as myClass y = x;
myClass y(x);

Some prefer the latter, as it is more clear that a constructor is being called for y.


[edited by - Direct4D on June 1, 2004 3:22:39 PM]

Share this post


Link to post
Share on other sites
The copy constructor is being used in the second case. The general rule is when you override the assignment operator that you should override the copy constructor. Here''s the format I follow for my classes:


class MyClass {
int mydata;
char* mystring;

void clone(const MyClass& o) {
if( this != &o ) {
mydata = o.mydata;
strcpy(mystring, o.mystring);
}
}
public:
MyClass() { ... }
MyClass(const MyClass& o) { clone(o); }
MyClass& operator=(const MyClass& o) { clone(o); return *this; }
};


Regards,
Jeff


[ CodeDread ]

Share this post


Link to post
Share on other sites
What was said above is correct. I''d just like to add that you should avoid this syntax:
myClass y = x;
as you noticed by now, it is confusing to many. Prefer myClass y(x).

Share this post


Link to post
Share on other sites
I haven''t really read the post but if your trying to avoid implicit conversions then use the keyword "explicit" before a constructor declaration to enforce "()" on construction.

Share this post


Link to post
Share on other sites
quote:
Original post by snk_kid
I haven''t really read the post but if your trying to avoid implicit conversions then use the keyword "explicit" before a constructor declaration to enforce "()" on construction.


I suggest you read the post next time. Your response has nothing to do with his problem, it just contains some random bit of knowledge.


[ CodeDread ]

Share this post


Link to post
Share on other sites
quote:
Original post by rypyr
quote:
Original post by snk_kid
I haven''t really read the post but if your trying to avoid implicit conversions then use the keyword "explicit" before a constructor declaration to enforce "()" on construction.


I suggest you read the post next time. Your response has nothing to do with his problem, it just contains some random bit of knowledge.


[ CodeDread ]


Well actually i was just saying how from what fallenang3l was talking how to enforce it, so its not completely random

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!