简体   繁体   中英

Overload operator for int and char in c++

I want to have a class that can accept assignment from both and int and a string literal. The string literal, it turns out, must be of type char[]. I actually have a whole lot of data types that are all successfully taken by the assignment operator. But the compiler gets confused between these two. I get how interchangeable the char can be with an int, I often use that for quick ascii comparisons. What I'd like to know is if there's a way to get the compiler to stop trying to send string literals to the int version of the overloaded operator. None of the other types have this problem, it is only these two that are getting mixed up.

    myDataType& operator = (int Right);
    myDataType& operator = (const char &Right);

When I try:

myDataType Test;

Test = "Hello World.";

The compiler tries to interpret the string literal as an int and throws this error: error: invalid conversion from 'const char*' to 'int' [-fpermissive]

Thanks.

If the string must be of type char[] , then you must have a [] , or * , in the 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, as this is C++, probably just simpler to actually use a string

myDataType& operator = (int Right);
myDataType& operator = (std::string Right);

My guess is that the compiler is trying to actually use the constructor as your operator is not of the good type. You should probably give us the full error message, not just the last one.

A string litteral is a const char * and not a const char & .

I could reproduce it with the following code:struct Foo{

struct Foo{
    Foo(){}
    Foo(int test){}
    Foo& operator = (int Right){printf(" =(int)");}
    Foo& operator = (const char Right){printf("=(const char &)");}
};

int main(int, char**)
{
    Foo foo;
    foo = "ttt";
    return 0;
}

which gave me the following output :

main.cpp: In function 'int main(int, char**)':
main.cpp:13:9: error: ambiguous overload for 'operator=' (operand types are 'Foo' and 'const char [4]')
     foo = "ttt";
         ^
main.cpp:13:9: note: candidates are:
main.cpp:6:10: note: Foo& Foo::operator=(int) <near match>
     Foo& operator = (int Right){printf(" =(int)");}
          ^
main.cpp:6:10: note:   no known conversion for argument 1 from 'const char [4]' to 'int'
main.cpp:7:10: note: Foo& Foo::operator=(const char&) <near match>
     Foo& operator = (const char &Right){printf("=(const char *)");}

However, the following code is fine :

struct Foo{
    Foo(){}
    Foo(int test){}
    Foo& operator = (int Right){printf(" =(int)");}
    Foo& operator = (const char *Right){printf("=(const char *)");}
};

If you want the compiler not to convert arguments implicitly, use the explicit keyword like this :

struct Foo{
    explicit Foo(int test){}
};

This will tell the compiler that this constructor will accept int , and only int as a parameter type. So pointers won't be using this constructor for example. For more details about it check this SO question and explicit keyword from cppreference.com

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM