Reputation: 183
main:
#include "multiparse.h"
int main()
{
parse_string obj;
obj = "1234";
//int a = obj;
//obj = 1234;
return 0;
}
header:
class parse_string
{
char* str;
long str_sz;
double val;
bool isnumber;
public:
template<class typename>
parse_string& operator=(typenamet input)
{
//printf("%d == %d\n",typeid(input).name(),typeid(const char*).name());
if(typeid(input)==typeid(char*)||typeid(input)==typeid(const char*))
{
str_sz=strlen(input)+1;
if(str==0)
{
str = (char*)malloc(sizeof(char)*str_sz);
}
else
{
str = (char*)realloc(str,sizeof(char)*str_sz);
}
memset(str,0,str_sz);
strcpy(str,input);
this->str_to_num();
isnumber=0;
printf("A\n");
}
else
{
printf("B\n");
val = (double)input;
this->num_to_str();
isnumber=1;
}
}
};
g++ error: multiparse.h error: invalid cast from type 'const char*' to type 'double' at --> 'val = (double)input;'
This code is not executed in my case it will just printf 'A' and not 'B', but g++ doesn't compile this code. I can't figure it out.
Upvotes: 0
Views: 50
Reputation: 183
I found a solution for my requirements, but I think it's not the best way:
parse_string& operator=(char* input)
{
str_sz=strlen(input)+1;
if(str==0)
{
str = (char*)malloc(sizeof(char)*str_sz);
}
else
{
str = (char*)realloc(str,sizeof(char)*str_sz);
}
memset(str,0,str_sz);
strcpy(str,input);
this->str_to_num();
isnumber=0;
return *this;
}
parse_string& operator=(const char* input)
{
str_sz=strlen(input)+1;
if(str==0)
{
str = (char*)malloc(sizeof(char)*str_sz);
}
else
{
str = (char*)realloc(str,sizeof(char)*str_sz);
}
memset(str,0,str_sz);
strcpy(str,input);
this->str_to_num();
isnumber=0;
return *this;
}
parse_string& operator=(char input)
{
val = (double)input;
this->num_to_str();
isnumber=1;
return *this;
}
parse_string& operator=(int input)
{
val = (double)input;
this->num_to_str();
isnumber=1;
return *this;
}
parse_string& operator=(long input)
{
val = (double)input;
this->num_to_str();
isnumber=1;
return *this;
}
parse_string& operator=(unsigned char input)
{
val = (double)input;
this->num_to_str();
isnumber=1;
return *this;
}
parse_string& operator=(unsigned int input)
{
val = (double)input;
this->num_to_str();
isnumber=1;
return *this;
}
parse_string& operator=(unsigned long input)
{
val = (double)input;
this->num_to_str();
isnumber=1;
return *this;
}
parse_string& operator=(double input)
{
val = input;
this->num_to_str();
isnumber=1;
return *this;
}
Upvotes: 0
Reputation: 522
Even though, the code is not executed, it's still part of a *.cpp
file (as it was #include
d. So, it becomes apart of a *.obj
/*.o
file for this source.
For this to happen, compiler needs to generate machine code
for everything in the *.cpp
file (templates work a bit different, but it's not about them now).
In other words, to get a .exe
/.lib
/.dll
file, which consists of .obj
files, you need the the files which are to become said .obj
files to be compiled properly (transcoded to machine code
).
Upvotes: 2