Type conversion error: cannot convert from 'char' to '...*'.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Arepi
    New Member
    • Sep 2008
    • 62

    Type conversion error: cannot convert from 'char' to '...*'.

    Hi,

    The following class decide which type is in the current instance and itialize depends on this information.

    The problem:
    When make an instance with any pointer type, a compilation error occures: cannot convert from 'char' to '...*'.

    I thought might be a solvation that dont compile the char initialization part if a pointer type has come, but I doubt that could solve with conditional compilation.

    Is anyway a solution for this problem?

    Thanks
    Arepi

    Code:

    Code:
    #include <iostream>
    #include <typeinfo>
    #include <string>
    
    using namespace std;
    
    template<class type>
    class IdInputTypes{
    public:
    	IdInputTypes();
    protected:
    	type Variable;
    	const type_info& ClassType1;
    	string ClassType1Name;
    };
    
    template<class type>
    IdInputTypes<type>::IdInputTypes() : ClassType1(typeid(type)) {
    	ClassType1Name = ClassType1.name();
    	if(ClassType1Name.find("*") != ClassType1Name.npos){
    		Variable=NULL;
    		cout << "The type of this object is pointer and initialized with NULL" << endl;
    	}
    	else if(ClassType1Name=="string" || ClassType1Name=="char"){
    		Variable=(char)"";
    		cout << "The type of this object is " << ClassType1Name << "and initialized with '' '' "<< endl;
    	}
    	else{
    		Variable=0;
    		cout << "The type of this object is a number type and initialized with 0" << endl;
    	}
    }
    
    void main(void){
    	IdInputTypes<double *> *idp;
    	idp=new IdInputTypes<double *>;
    	delete idp; 
    	cin.get();
    }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Your approach is incorrect for C++.

    In addtion, your are using RTTI which is very slow and requires that you hard-code type names in your code. This forces a re-code when new types are added. It is generally a design flaw to use RTTI. RTTI is part of C++ in order to provide the ability to make emergency fixes to old code and not for general development use.

    In C you would have to know the type of the class in order to do a conversion but in C++ you just let the class convert itself by having a conversion operator method.

    That is, in C++ the class knows how to convert itself to another type. Conversion constructors allow the class to convert from some type to the class type.

    Comment

    • Arepi
      New Member
      • Sep 2008
      • 62

      #3
      Thanks.
      Anyway my goal was to make an "universal constructor" which initialize the data members depands on type. As we dont know which type will come as it is a template, we have to decide somehow, thats why I used RTTI.My problem was a collision bitween initialization cases as when it compiled the type already determined. Anyway I realized there is a solution. It means more coding but at least works: Simple I have to make different construtors with different input parameters and use allways the one which match to the given specialization of the template.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        A "universal constructor" is also known as a virtual constructor and is part of the Factory design pattern.

        There are several kinds of factory classes you can write and one of them creates an object based on a type you specify. For example, ite factory creates an object based on the type you specify and returns a pointer to its base object:


        Code:
        Factory* fact = new AutomobileFactory;
        Automobile* obj = fact.Create(FORD);
        delete f;
        For this to work, the object created by the fact object must be derived from the Automobile class and th two classes must be designed to use polymorphism. After the object is created, the factory is deleted since it's no longer needed.

        You might research the Factory design pattern.

        Comment

        Working...