Sign in to follow this  

return type

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

This topic is 4282 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this