Jump to content
  • Advertisement
Sign in to follow this  
Aerts

return type

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

There is a C++ GDI function named SelectObject that returns a HGDIOBJ object. However I saw it could also work with (ie can also return) types HBITMAP, HPEN and HBRUSH. How is this possible?

Share this post


Link to post
Share on other sites
Advertisement
One class can have more than one function with the same name, as long as the parameters to the function are different. Not just named different, but actually different types. For instance, you can imagine a string class with these functions:

class MyString
{
private:
// ...
public:
//append a string to the end of this one.
void Append(MyString &s);

//Convert this int to a string, then append it to the end.
void Append(int i);

//Convert this float to a string, then append it to the end.
void Append(float f);

// ...
};


The C++ compiler is smart enough to tell which one you want when you call it. If I call Append with an int, it's going to use the second function.

Share this post


Link to post
Share on other sites
this is possible because actually both HGDIBITMAP, HBITMAP, HPEN, HBRUSH and almost all Win32 handles(HWND, etc) are just defined as void pointers.

SelectObject that you're talking about is part of Windows GDI, whose API is in plain C.

BeanDog is talking about something else, it's function overloading, where you can have many functions with the same name, but different parameters, like this:

double SquareRoot(const double d);
float SquareRoot(const float d);



however, this is not the case for the function SelectObject because
1. because it's written in C.
2. even if it was in C++, function overloading is not possible using return type only, so the following is not valid:

int Function();
float Function();

Share this post


Link to post
Share on other sites
Yep, just as Xeee said, it's a void pointer. This generic pointer can point to any data type.

- xeddiex

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.

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!