Skip to main content
gamedev.net
🔒 Locked

operator overloading?

Started by McZ Jan 27, 2004 at 3:58 AM 5 replies 450+ views
Original Post
McZ
McZ
well... I have created a Button class like this class cButton { HWND hWnd; public: cButton(void); virtual ~cButton(void); HWND getHWND() { return hWnd; } void Create(char *name, int startx, int starty, int width, int height); }; so now I would like to know if I can create an operator thing to replace the getHWND() function so I can send my cButton class directly to anything that wants the button hWnd?! e.g. cButton *MyButton = new cButton; MyButton->Create("OK", 10, 10, 50, 20); // replace this line HWND hButton = MyButton->getHWND(); // with this line HWND hButton = (HWND)MyButton; is it posible ? I want some kind of generic explanation so I can implement it for other things to. like my cVertex3f class so I can write like this cVertex3f v1; cout << "Vertex 1: " << v1 << endl; and the output would be something like this Vertex 1: (0.1, 5.43, 8.76)
Sneftel
Sneftel
Sure. Here''s a class that you can pass like an int:

class A
{
public:
int operator int() { return myInt; }
private:
int myInt;
};

Those are called "conversion operators".

Now, conversion operators are not often used, and with good reason. It''s simply too dangerous, in many cases, to have the unchecked ability to treat a class like some other type. For instance, in your case, HWND is actually a typedef/define for void*. That means that you can use your class anywhere a void* can be used, and the compiler won''t complain. This is not a good thing.

Leave the getHWND() in.


"Sneftel is correct, if rather vulgar." --Flarelocke
McZ
McZ
well.. I guess it would be best to leave the getHWND() as it is

but for the cVertex3f class I have the... would it be something like this?

class cVertex3f
{
float x,y,z;

public:

ostream operator ostream( ostream &o )
{
ostream myO;

myO << "(" << x << "," << y << "," << z << ")" << o;
return myO;
}
};

or?
Sneftel
Sneftel
No. For that, you should overload the << operator. Keep in mind that you''ll have to overload it outside the class, since its RHS is not of your class'' type.

Google for "insertion operator overloading" for more info.


"Sneftel is correct, if rather vulgar." --Flarelocke
Mayrel
Mayrel
The strict answer to your question is no. What you're wanting to do is convert a CButton * to a HWND. However, you don't get to define conversion operators for pointers to classes, only for the classes themselves. You could define an operator HWND () method which you'd use like (HWND)*MyButton (note the *).

Like Sneftel, I'd probably tend to keep the getHwnd around.

The second part is quite simple, and is what you're 'meant' to do.


std::ostream &operator << (std::ostream &os, const cVertex3f &v)
{
return os << "(" << v.x << ", " << v.y << ", " << v.z << ")";
}


Edit: You'll have to declare std::ostream &operator << (std::ostream &os, const cVertex3f &v) as a friend of cVertex3f if the coordinates are private members as in your sample.

CoV

[edited by - Mayrel on January 27, 2004 5:29:17 AM]
McZ
McZ
ahh thats why my overloaded operator << didn''t work

thanks alot

Jingo
Jingo
quote:
Original post by Sneftel
Sure. Here''s a class that you can pass like an int:

class A
{
public:
int operator int() { return myInt; }
private:
int myInt;
};

Those are called "conversion operators".


"Sneftel is correct, if rather vulgar." --Flarelocke


You dont put a return type on a conversion operator. Should just be:

 
operator int()
{
return myInt;
}



Topic Locked

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

Sign in to reply to this topic.