I have a simple function that acts as getter for a pointer to an object
wxAuiNotebook* Frame::GetNoteBook(unsigned short index)
{
NotebooksContainer->GetBook(index);
}
I forgot to write the return keyword in the above simple function and compiled it and it run successfully.
Then I discovered a return keyword is missing, so I launched GDB trying to know how this did work, I found the function actually returns the required pointer.
Experiment #1
I have tried much simpler example
class A {
private:
std::string* pstr;
public:
std::string* GetPstr()
{
std::cout << pstr << std::endl;
pstr;
}
void SetPstr(std::string* val)
{
pstr = val;
}
};
int main() {
A a;
std::string jfox("This is Jumping Fox");
a.SetPstr(&jfox);
std::cout << a.GetPstr() << std::endl;
return 0;
}
GCC compiler doesn't throw an error during compilation, but the addresses of the objects are totally different. I got the following output under G++ 4.3.3
0x7fff547ce790
0x6012a0
Note :The same simple example throws error under MSVC 2015.
To my knowledge, some languages - ex. Ruby - return the outcome of last statement of the function as the return value, but this doesn't apply to C++.
what's the explanation why the same pointer was returned in the original example and why different pointers in the simple example ?
I'm open to more experimentation.