return type

Started by
2 comments, last by xeddiex 18 years ago
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?
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.
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();
xee..
Yep, just as Xeee said, it's a void pointer. This generic pointer can point to any data type.

- xeddiex
one..

This topic is closed to new replies.

Advertisement