Skip to main content
gamedev.net
🔒 Locked

Inverse casting operators?

Started by ms291052 5 replies 350+ views
Original Post
ms291052
ms291052
Is it possible to create a class, and that kind of do inverse casting? What I''m thinking is something along these lines: I have a (semi-)global pointer to an array of pointers to my THING class, and a lot of functions that take ID numbers to THINGs (stored in the class), look them up in the array, and use them. What I''d like to do is be able to allow these functions to take either a DWORD (ID) or a THING* but that would mean somehow writing an operator to cast a DWORD to a THING* by taking the DWORD and looking it up in the array. Is it possible to make a standard type cast into your class and define how it does so?
Cedric
Cedric
Please don''t write an implicit cast.

I''m sorry, but it looks like you''ll have to write overloaded versions of your function. The one that accepts a DWORD calls the one that accepts THINGS* by making the look-up explicitly.

Cédric
SiCrane
SiCrane
If you have a lot of functions that need to take both DWORD and THING * arguments, you might try using a proxy object instead of overloading. Something like:

THING * LookUp(int index);

class Proxy {
public:
Proxy(THING * thing) : thing_(thing) {}
Proxy(int index) : thing_(LookUp(index)) {}
operator THING *() { return thing_; }
private:
THING * thing_;
};

void SomeFuncUsingTHING(THING * thing);
void SomeOtherFuncUsingTHING(THING * thing);

void SomeFunc(Proxy p) {
SomeFuncUsingTHING(p);
SomeOtherFuncUsingTHING(p);
}
biovenger
biovenger
Hi again.

If you want my view from a pure OO view then here it goes:

First of all, change the ID''s from DWORD to another self-defined type. Perhaps a THING_ID class? Then, change all functions to take a THING_ID value instead of a DWORD. Now here is a syntax for object conversion:


// class A wants to be able to be converted from an int to a class A

class A
{
private:
int val;
public:
A() { val = 0; } // Default constructor


// Conversion constructor

A(const int &i) { val = i; }
};


But otherwise, you can just write an implicit conversion function (such as above) for DWORD. This is however unclean and shouldn''t be used. Can result in lots of problems.
-----------------------------Final Frontier Trader
PlayGGY
PlayGGY
quote:
Original post by biovenger
Hi again.

If you want my view from a pure OO view then here it goes:

First of all, change the ID''s from DWORD to another self-defined type. Perhaps a THING_ID class? Then, change all functions to take a THING_ID value instead of a DWORD. Now here is a syntax for object conversion:


// class A wants to be able to be converted from an int to a class A

class A
{
private:
int val;
public:
A() { val = 0; } // Default constructor


// Conversion constructor

A(const int &i) { val = i; }
};


But otherwise, you can just write an implicit conversion function (such as above) for DWORD. This is however unclean and shouldn''t be used. Can result in lots of problems.



I think that is overkill. Why not overload the function?
And the rockets' red glare, the bombs bursting in air,gave proof through the fight that our flag was still there.Oh say, does that star-spangled banner yet waveover the land of the free and the home of the brave?
biovenger
biovenger
well it depends. Sometimes overloading won''t be the correct answer. It all depends on his implementation. It''s ok to overload if you CAN pass the THING objects by pointer or reference, but if you only have the ID''s then a good thing would be to use some kind of conversion or a lookup table of THING pointers. The above method really isn''t overkill since it doesn''t take long to implement, plus that it results in really neat code.
-----------------------------Final Frontier Trader
ms291052
ms291052
Lol, hey again, biovenger. I thought you''d had enough of my stupid THINGs. Anyway, that code was exactly what I needed. Thanks!

Topic Locked

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

Sign in to reply to this topic.