Skip to main content
gamedev.net
🔒 Locked

Constructor Param == Conversion?

Started by Kest 2 replies 400+ views
Original Post
Kest
Kest
If I supply a parameter type to an object constructor, does that cause the object to allow calling the '=' operator on the constructor parameter type? An example:
class Rocket
{
public:
   Rocket() { SetupOtherJunk(); }
   Rocket(int missle_count) { SetupOtherJunk(); FireMissles(missle_count); }
   void FireMissles(int count) { ... }

private:
   void SetupOtherJunk();
};

// Later in function code..

Rocket new_rocket;
..other code..
new_rocket = 5;

Does this function code result in the following code to be executed..?
new_rocket.SetupOtherJunk();
..other code..
new_rocket.SetupOtherJunk();
new_rocket.FireMissles( 5 );
This doesn't make any sense at all to me. I don't know for sure what code is executed, but my IDE is allowing me to use the = operator on an integer with my class. And I have no such operation defined, other than the constructor parameter. Is there any way to prevent this from happening? I cought myself before something bad happened, but I could imagine this type of thing causing me future problems. Thanks for information.
juanpaco
juanpaco
Assuming you're doing c++ here...

That is totally whacked. That ought to not compile. You haven't overloaded the = operator, so I don't see how that can compile either. Are you sure that when you're using the = operator that you're not using it with a Rocket pointer? If it's a pointer, that makes sense, as you're assigning it a bad address.

Of course, I could be wrong too. Do you have more code to give a better idea of the context? From what I can tell, you really should be getting a compiler error there.
mattd
mattd
Read this.

You can stop this from occuring by using the explicit keyword.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.