Check for the constructor in SingularCallBack class, i might think, it's a constructor problem that is not allowing you to construct the vector...
Check for the constructor in SingularCallBack class, i might think, it's a constructor problem that is not allowing you to construct the vector...
Thanks for all the effort, Elysia, now I please make me understand why can't the values in the derived class be reflected in the base class:
I think the problem is that I should call the the baseclass constructor from the derived class constructor, like this:
AClass () : BaseClass ()
Code:#include <iostream> #include <vector> #include <string> using namespace std; template <class MyDummyClass, typename ReturnType, typename Parameter> class SingularCallBack { public: typedef ReturnType (MyDummyClass ::*Method)(const Parameter&); SingularCallBack(MyDummyClass* _class_instance, Method _method, Parameter h) { class_instance = _class_instance; method = _method; hh = h; }; ReturnType execute(Parameter parameter) { return (class_instance->*method)(parameter); }; private: MyDummyClass* class_instance; Method method; Parameter hh; }; template <typename MyDummyClass, typename ReturnType, typename Parameter> class BaseClass { public: std :: vector<SingularCallBack<MyDummyClass, ReturnType, Parameter> > myvec; BaseClass () { if (myvec.empty () == false) { myvec[0].execute (55); } else { cout << "\nNothing to print\n"; cout << "\ny" << y; } } }; template <typename ReturnType, typename Parameter> class AClass : public BaseClass<AClass<ReturnType, Parameter>, ReturnType, Parameter> { public: BaseClass<AClass<ReturnType, Parameter>, ReturnType, Parameter> k; AClass () { k.myvec.push_back( SingularCallBack<AClass, ReturnType, Parameter>(this, &AClass::f, 55) ); } ReturnType f (const Parameter& foo) { cout << "\nasdsads\n"; return ReturnType(); } }; int main() { AClass<int, int> test; return 0; }
Last edited by AnishaKaul; 03-11-2011 at 12:59 AM.
This code should generate errors..... Starting from the main...
AClass constructor will be called...
As AClass is derived from the BaseClass so It'll call BaseCall constructor.. In BaseClass constructor you have some conditional statements that will lead you to the errors...
>>BaseClass<AClass<ReturnType, Parameter>, ReturnType, Parameter> k;
Why are you creating an instance of the base class inside the derived class when you're inheriting from that class? Remove that line.
Replace
k.myvec.push_back( SingularCallBack<AClass, ReturnType, Parameter>(this, &AClass::f, 55) );
with
this->myvec.push_back( SingularCallBack<AClass, ReturnType, Parameter>(this, &AClass::f, 55) );
Try that and see what happens.
It's difficult for me to understand what you're after, really.
I did that Elysia, before posting here, it resulted in the same problem as stated in my last post, that's why I replaced it with the BaseClass's object.
Nothing, I just want to fill that vector with the derived class's object and the parameter, through the derived class, and call the functions thus stored in the vector from the base class.It's difficult for me to understand what you're after, really.
Is there a thinking flaw, I think it's just like the function pointers in C.
and thanks again.
How do you know this? How do you use this?
Typically, it's not a good idea because in C++ we can use functors. Which really boils down to using a template parameter to say that we accept any type who supports an overloaded () operator.Is there a thinking flaw, I think it's just like the function pointers in C.
So it can be any class, any function pointer, etc.
Still, there is merit in learning how templates work properly. It's a good exercise to get practice in templates, so I would suggest completing it, if only for experience.
See this: The green one too executes and func. gets called. but still the red statement gets executed.
Code:#include <iostream> #include <vector> #include <string> using namespace std; template <class MyDummyClass, typename ReturnType, typename Parameter> class SingularCallBack { public: typedef ReturnType (MyDummyClass ::*Method)(const Parameter&); SingularCallBack(MyDummyClass* _class_instance, Method _method, Parameter h) { class_instance = _class_instance; method = _method; hh = h; }; ReturnType execute(Parameter parameter) { return (class_instance->*method)(parameter); }; private: MyDummyClass* class_instance; Method method; Parameter hh; }; template <typename MyDummyClass, typename ReturnType, typename Parameter> class BaseClass { public: std :: vector<SingularCallBack<MyDummyClass, ReturnType, Parameter> > myvec; BaseClass () { if (myvec.empty () == false) { myvec[0].execute (55); } else { cout << "\nNothing to print\n"; } } }; template <typename ReturnType, typename Parameter> class AClass : public BaseClass<AClass<ReturnType, Parameter>, ReturnType, Parameter> { public: AClass () { this->myvec.push_back( SingularCallBack<AClass, ReturnType, Parameter>(this, &AClass::f, int(55)) ); this->myvec[0].execute (55); } ReturnType f (const Parameter& foo) { cout << "\nasdsads\n"; return ReturnType(); } }; int main() { AClass<int, int> test; return 0; }That's exactly what I am trying to do, I am using templates to get random types and call the derived class functions from the base class.Typically, it's not a good idea because in C++ we can use functors. Which really boils down to using a template parameter to say that we accept any type who supports an overloaded () operator.
So it can be any class, any function pointer, etc.
Still, there is merit in learning how templates work properly. It's a good exercise to get practice in templates, so I would suggest completing it, if only for experience.
Now I realize that I was getting confused by the sequence of the events, the base class is indeed getting called first, but my problem is NOT yet solved, I'll get back to you soon.
Thanks again.
What's the current code you are working from? (all of the example)
Soma