C++ template class pointer polymorphism

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #16
    Try this.

    There is no casting. There is no template. The class is set up for int and float.
    I convert input int to a string. I convert input float to a string.

    When the user uses the object as int, it converts itself to an int. The same object will convert itself to a float if you use it as a float.

    Let me know what you think.

    Code:
    #include <iostream>
    #include <vector>
    #include <sstream>
    #include <string.h>
    
    using namespace std;
    
    #define _CRT_SECURE_NO_WARNINGS
    
    class GenericData
    {
    	string data;
    	
    
    	string Convert(int arg);
    	string Convert(float arg);
    
    public:
    	GenericData(int arg);
    	GenericData(float arg);
    	operator int();
    	operator float();
    };
    
    GenericData::GenericData(int arg)
    {
    	data = this->Convert(arg);
    }
    
    
    GenericData::GenericData(float arg)
    {
    			data = this->Convert(arg);
    }
    
    string	GenericData::Convert(int arg)
    	{
    		ostringstream str;
    		str << arg;
    		return str.str();
    	}
    string	GenericData::Convert(float arg)
    {
    	ostringstream str;
    	str << arg;
    	return str.str();
    }
    
    //Conversion operators
    
    GenericData::operator int()
    {
    	stringstream str(this->data);
    
    	int temp = 0;
    
    	str >> temp;
    
    	return temp;
    }
    
    GenericData::operator float()
    {
    	stringstream str(this->data);
    
    	float temp = 0;
    
    	str >> temp;
    
    	return temp;
    }
    
    int main()
    { 
    	GenericData obj1(5);     //an int
    	GenericData obj2(3.14159f);  //a double 
    
    	int value (obj1);
    
    	float valuef(obj2);
    
    	cout << value << endl;
    
    	
    
    	
    }

    Comment

    • 51423benam
      New Member
      • Apr 2018
      • 31

      #17
      Ah, I wanted to do your approach with the proxy, but I have a problem. Your example itself is nice, but my problem still exists. I need to call functions on that template object (or, in this case, the proxy) which return data types differ, depending on the template argument of the object. How can I solve this using the proxy pattern? By the way, the return values aren't primitive data types such as int or float which you can convert easily, it's more like std::shared_ptr <std::deque<std ::unique_ptr<Da taBlock<T>>>> (:

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #18
        The customary way to do that is to keep the data in the proxy object is a format that you can convert as needed to something else.

        If your function has a 51423benam argument type all you need provide is an operator(51423b nam) member function. When the object is used to call a function with a 51423bnam argument type, the conversion operator will be implicitly called. The object data will be converted to 51423benam type an the call will proceed.

        Your conversion operators are not limited to defined types.

        Your example would need:

        Code:
        operator(std::shared_ptr<std::deque<std::unique_ptr<DataBlo ck<T>>>>)
        {
          //convert to std::shared_ptr<std::deque<std::unique_ptr<DataBlo ck<T>>>>  here
        }

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #19
          BTW: Sorry for the delay. I was out-of-state for a week at a seminar.

          Comment

          Working...