Thread: Template vectors

  1. #16
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Check for the constructor in SingularCallBack class, i might think, it's a constructor problem that is not allowing you to construct the vector...

  2. #17
    Noob AnishaKaul's Avatar
    Join Date
    Jan 2010
    Location
    Gurgaon, India
    Posts
    115
    Quote Originally Posted by Elysia View Post
    Templates are bastards.
    You may try this:
    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.

  3. #18
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    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...

  4. #19
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    >>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.
    Last edited by Elysia; 03-11-2011 at 05:41 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #20
    Noob AnishaKaul's Avatar
    Join Date
    Jan 2010
    Location
    Gurgaon, India
    Posts
    115
    Quote Originally Posted by Elysia View Post
    >>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.
    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.

    It's difficult for me to understand what you're after, really.
    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.

    Is there a thinking flaw, I think it's just like the function pointers in C.

    and thanks again.

  6. #21
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by AnishaKaul View Post
    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.
    How do you know this? How do you use this?

    Is there a thinking flaw, I think it's just like the function pointers in C.
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #22
    Noob AnishaKaul's Avatar
    Join Date
    Jan 2010
    Location
    Gurgaon, India
    Posts
    115
    Quote Originally Posted by Elysia View Post
    How do you know this? How do you use this?
    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;
    }
    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.
    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.

  8. #23
    Noob AnishaKaul's Avatar
    Join Date
    Jan 2010
    Location
    Gurgaon, India
    Posts
    115
    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.

  9. #24
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You probably figured this out already, but in inheritance, the base class constructor is always called first. The reverse is true for the destructor.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #25
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    What's the current code you are working from? (all of the example)

    Soma

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assignment issues
    By Elysia in forum C++ Programming
    Replies: 6
    Last Post: 01-13-2010, 12:55 PM
  2. Specialising a member function with a template template parameter
    By the4thamigo_uk in forum C++ Programming
    Replies: 10
    Last Post: 10-12-2007, 04:37 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM