C++ 中 int 和 char 的重载运算符
Overload operator for int and char in c++
我想要一个 class 可以接受来自 int 和字符串文字的赋值。事实证明,字符串文字必须是 char[] 类型。我实际上有很多数据类型都被赋值运算符成功采用。但是编译器会混淆这两者。我知道 char 可以与 int 互换,我经常用它来进行快速的 ascii 比较。我想知道的是,是否有办法让编译器停止尝试将字符串文字发送到重载运算符的 int 版本。 None 其他类型有这个问题,只有这两个搞混了。
myDataType& operator = (int Right);
myDataType& operator = (const char &Right);
当我尝试时:
myDataType Test;
Test = "Hello World.";
编译器尝试将字符串文字解释为 int 并抛出此错误:错误:从‘const char*’到‘int’的无效转换[-fpermissive]
谢谢。
如果字符串必须是 char[]
类型,那么您必须在签名中包含 []
或 *
。
myDataType& operator = (int Right);
myDataType& operator = (const char *Right);
或
myDataType& operator = (int Right);
template<size_t N>
myDataType& operator = (const char (&Right)[N]);
但是,因为这是 C++,实际使用 string
可能更简单
myDataType& operator = (int Right);
myDataType& operator = (std::string Right);
我的猜测是编译器试图实际使用构造函数,因为您的运算符类型不正确。您应该给我们完整的错误消息,而不仅仅是最后一条。
字符串文字是 const char *
而不是 const char &
。
我可以用下面的 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;
}
这给了我以下输出:
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 *)");}
但是,下面的代码没问题:
struct Foo{
Foo(){}
Foo(int test){}
Foo& operator = (int Right){printf(" =(int)");}
Foo& operator = (const char *Right){printf("=(const char *)");}
};
如果您希望编译器不隐式转换参数,请使用 explicit 关键字,如下所示:
struct Foo{
explicit Foo(int test){}
};
这将告诉编译器这个构造函数将接受int
,并且只接受int
作为参数类型。所以指针不会使用这个构造函数。有关它的更多详细信息,请查看 cppreference.com
中的 SO question and explicit keyword
我想要一个 class 可以接受来自 int 和字符串文字的赋值。事实证明,字符串文字必须是 char[] 类型。我实际上有很多数据类型都被赋值运算符成功采用。但是编译器会混淆这两者。我知道 char 可以与 int 互换,我经常用它来进行快速的 ascii 比较。我想知道的是,是否有办法让编译器停止尝试将字符串文字发送到重载运算符的 int 版本。 None 其他类型有这个问题,只有这两个搞混了。
myDataType& operator = (int Right);
myDataType& operator = (const char &Right);
当我尝试时:
myDataType Test;
Test = "Hello World.";
编译器尝试将字符串文字解释为 int 并抛出此错误:错误:从‘const char*’到‘int’的无效转换[-fpermissive]
谢谢。
如果字符串必须是 char[]
类型,那么您必须在签名中包含 []
或 *
。
myDataType& operator = (int Right);
myDataType& operator = (const char *Right);
或
myDataType& operator = (int Right);
template<size_t N>
myDataType& operator = (const char (&Right)[N]);
但是,因为这是 C++,实际使用 string
myDataType& operator = (int Right);
myDataType& operator = (std::string Right);
我的猜测是编译器试图实际使用构造函数,因为您的运算符类型不正确。您应该给我们完整的错误消息,而不仅仅是最后一条。
字符串文字是 const char *
而不是 const char &
。
我可以用下面的 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;
}
这给了我以下输出:
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 *)");}
但是,下面的代码没问题:
struct Foo{
Foo(){}
Foo(int test){}
Foo& operator = (int Right){printf(" =(int)");}
Foo& operator = (const char *Right){printf("=(const char *)");}
};
如果您希望编译器不隐式转换参数,请使用 explicit 关键字,如下所示:
struct Foo{
explicit Foo(int test){}
};
这将告诉编译器这个构造函数将接受int
,并且只接受int
作为参数类型。所以指针不会使用这个构造函数。有关它的更多详细信息,请查看 cppreference.com