Thanks for the effort, John.
Quote:
Originally Posted by johnsfine
The compilation error meant exactly what it said. The parameter you used for DerivedMethod (std::string) did not match any parameter you declared for DerivedMethod.
|
How daft of me :doh: That's all I have to say!
Quote:
Originally Posted by johnsfine
I'm sure you're used to the fact that you can normally pass a value when a & is expected and you can normally pass a non const when a const is expected. So it is confusing that in this case you can't.
|
I need to study more on
const std :: string&.
Quote:
Originally Posted by johnsfine
I'm used to getting a compiler warning for that mistake. But I haven't tested to see why you don't get it in this case.
|
I tested the below program once again, with Wall and Wextra, it doesn't give me any sorts of
warnings!
Code:
#include <iostream>
#include <vector>
using namespace std;
template <class MyDummyClass, typename ReturnType, typename Parameter>
class SingularCallBack
{
public:
typedef ReturnType (MyDummyClass ::*Method)(Parameter);
SingularCallBack(MyDummyClass* _class_instance, Method _method)
{
class_instance = _class_instance;
method = _method;
};
ReturnType execute(Parameter parameter)
{
return (class_instance->*method)(parameter);
};
private:
MyDummyClass* class_instance;
Method method;
};
class BaseClass
{
public:
virtual bool DerivedMethod (const std::string str)
{
cout << str;
return true;
}
};
class AClass : public BaseClass
{
public:
AClass(unsigned int _id): id(_id){}
bool DerivedMethod( const std::string& str)
{
std::cout << "Derived Method AClass[" << id << "]: " << str << std::endl;
return true;
}
private:
unsigned int id;
};
int main()
{
std::vector < SingularCallBack < BaseClass, bool, std::string > > callback_list;
AClass a(1);
callback_list.push_back (SingularCallBack < BaseClass, bool, std::string > (&a, &BaseClass :: DerivedMethod));
for (unsigned int i = 0; i < callback_list.size(); ++i)
{
callback_list[i].execute("abc");
}
return 0;
}
Quote:
Originally Posted by johnsfine
The derived class's function has a different signature than the base class's virtual function, so the derived function masks the base function rather than overriding it.
|
I never knew that we are allowed to change the signatures of the virtual functions in the derived classes.
Quote:
Originally Posted by johnsfine
A related tricky topic is buried in the meaning of
BaseClass::DerivedMethod when used in the context of a derived object...That strange difference between BaseClass::DerivedMethod and &BaseClass::DerivedMethod has confused a lot of C++ programmers.
|
I am really thankful to you for all the effort you put in that explanation, but the situation is still not crystal clear for me. I request you to tell me what keywords should
I put in Google to get more on the difference between BaseClass::DerivedMethod and &BaseClass::DerivedMethod.
I have the
thinking in C++ book where the VTABLES are explained in detail, are
the VTABLES which I need to study in order to understand the above differences?