Jump to content
  • Advertisement
Sign in to follow this  
Turold

Weird class template issue

This topic is 3622 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

I have a simple template class for handle management. Every handle is represented in mem as two shorts, so that full handle fits in a register and is equal in size to a normal ptr. It works fine, but for some reason I can write statements like: "handle=NULL", where "handle" is a handle object by value... ... and it compiles! Why on earth can I assign an INT to a class OBJECT? More! Above statement executes constructor! I had to patch it by implementing overloaded "=" operator which prevented executing constructor. Why does compiler allow this to pass silently and what's exactly going on?

Share this post


Link to post
Share on other sites
Advertisement
I think you need to show some more code, but you can in fact assign an integer value to a pointer type (although other than NULL there is really no reason to use any other value). As far as the constructor being executed, its not executing after it is assigned null, that is impossible, construction occurs somewhere prior to that.

Share this post


Link to post
Share on other sites
It's a little hard to tell whether this is actually the problem based on what you posted, but remember that a constructor that takes a single argument also serves to support implicit conversions from compatible types. As such, you might try marking the constructor as explicit, and see if the code you mentioned then generates a compiler error.

We could probably stand to see more code though (for example, I'm not sure if the example you provided is an assignment, or an initialization).

Share this post


Link to post
Share on other sites
One possibility is that you're really doing something like this:

HandleType handle = NULL;

Which will work if you have a non-explicit constructor that takes a single argument of a type implicitly convertible from NULL's type (an integer or void pointer).

To give an absolute answer you'd need to show some code.

Share this post


Link to post
Share on other sites
"explicit" fixed it! Now it generates an error as I wish. You guys ROCK! :D

It executed constructor, since I have one which takes ptr to object to be referenced by a handle. I didn't know about those "implicit" conversions. But still, automatic casting an int to a struct pointer is something I didn't expect from C++.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!