Overload operator for int and char in c++ -


i want have class can accept assignment both , int , string literal. string literal, turns out, must of type char[]. have whole lot of data types taken assignment operator. compiler gets confused between these two. how interchangeable char can int, use quick ascii comparisons. i'd know if there's way compiler stop trying send string literals int version of overloaded operator. none of other types have problem, these 2 getting mixed up.

    mydatatype& operator = (int right);     mydatatype& operator = (const char &right); 

when try:

mydatatype test;  test = "hello world."; 

the compiler tries interpret string literal int , throws error: error: invalid conversion ‘const char*’ ‘int’ [-fpermissive]

thanks.

if string must of type char[], must have [], or *, in signature.

mydatatype& operator = (int right); mydatatype& operator = (const char *right); 

or

mydatatype& operator = (int right); template<size_t n> mydatatype& operator = (const char (&right)[n]); 

but, c++, simpler use string

mydatatype& operator = (int right); mydatatype& operator = (std::string right); 

Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -