By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,963 Members | 924 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,963 IT Pros & Developers. It's quick & easy.

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

P: 62

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?



Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <typeinfo>
  3. #include <string>
  5. using namespace std;
  7. template<class type>
  8. class IdInputTypes{
  9. public:
  10.     IdInputTypes();
  11. protected:
  12.     type Variable;
  13.     const type_info& ClassType1;
  14.     string ClassType1Name;
  15. };
  17. template<class type>
  18. IdInputTypes<type>::IdInputTypes() : ClassType1(typeid(type)) {
  19.     ClassType1Name =;
  20.     if(ClassType1Name.find("*") != ClassType1Name.npos){
  21.         Variable=NULL;
  22.         cout << "The type of this object is pointer and initialized with NULL" << endl;
  23.     }
  24.     else if(ClassType1Name=="string" || ClassType1Name=="char"){
  25.         Variable=(char)"";
  26.         cout << "The type of this object is " << ClassType1Name << "and initialized with '' '' "<< endl;
  27.     }
  28.     else{
  29.         Variable=0;
  30.         cout << "The type of this object is a number type and initialized with 0" << endl;
  31.     }
  32. }
  34. void main(void){
  35.     IdInputTypes<double *> *idp;
  36.     idp=new IdInputTypes<double *>;
  37.     delete idp; 
  38.     cin.get();
  39. }
Oct 2 '10 #1
Share this Question
Share on Google+
3 Replies

Expert Mod 5K+
P: 9,197
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.
Oct 2 '10 #2

P: 62
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.
Oct 2 '10 #3

Expert Mod 5K+
P: 9,197
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:

Expand|Select|Wrap|Line Numbers
  1. Factory* fact = new AutomobileFactory;
  2. Automobile* obj = fact.Create(FORD);
  3. 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.
Oct 2 '10 #4

Post your reply

Sign in to post your reply or Sign up for a free account.