C++没有操作员定义就会失败

发布于 2024-12-11 05:34:07 字数 573 浏览 0 评论 0原文

我目前正在将 C++ 应用程序移植到稍微受限的环境中。该应用程序使用 STL、字符串和流类。我正在重写这些的简化版本,它们将在我的环境中很好地发挥作用。

我担心的是,即使没有所有必要的运算符定义,我的应用程序也在编译。例如,对于我定义的字符串类:

string operator+ (const string& lhs, const string& rhs);

这已经足够了。但是,我注意到经常有 mystring +“某个常量字符串”的情况,而这在我的代码中没有定义。当我明确添加它时,它被使用:

string operator+ (const string& lhs, const char* rhs);

在那之前发生了什么?在我添加第二个函数之前它编译成功。当然,编译器无法推断如何将 c 样式字符串连接到我的字符串类。

我的程序现在出现奇怪的行为,我想知道这是否是由于其他运算符未定义所致。如果程序需要,是否有任何方法可以强制编译器要求此类运算符定义?

PS 我的字符串类位于唯一的命名空间中,与 std:: 无关

I'm currently porting a C++ application to a slightly restricted environment. The application uses the STL, string and stream classes. I'm rewriting simplified versions of these that will play nicely in my environment.

What I'm concerned about is that my application is compiling even without all the necessary operator definitions. For example, for my string classes I defined:

string operator+ (const string& lhs, const string& rhs);

and this was enough. However, I noticed there were often cases that had mystring + "some constant string" and this isn't defined in my code anywhere. When I explicitly added it it was used:

string operator+ (const string& lhs, const char* rhs);

What was going on before that? It compiled successfully before I added the second function. Surely the compiler wouldn't be able to infer how to concatenate c-style strings to my string class.

I'm getting strange behaviour in my program now and I'm wondering if it's due to other operators left undefined. Is there any way to enforce the compiler to require such operator definitions if it's needed by the program?

P.S. My string class is in a unique namespace and unrelated to std::

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

紫瑟鸿黎 2024-12-18 05:34:08

如果没有看到代码的其余部分,就不可能确定,但​​在这种情况下,猜起来可能也不太难。几乎可以肯定,您的 string 具有一个以 char const * 作为参数的构造函数,因此编译器正在使用该构造函数来转换 char const * 转换为 string,然后使用 string operator+ (const string& lhs, const string& rhs); 将其连接起来。

允许这种情况发生是使用全局变量而不是成员函数重载这些运算符的主要原因之一(如果不是的话)。作为成员函数,它们可以转换操作数,但不能转换左操作数。

It's impossible to be certain without seeing the rest of your code, but in this case it's probably not too hard to guess either. You almost certainly have a constructor for your string that takes a char const * as its parameter, so what's happening is that the compiler is using that ctor to convert the char const * to a string, then using string operator+ (const string& lhs, const string& rhs); to concatenate that.

Allowing this to happen is one of (if not the) primary reason for overloading these operators with globals instead of member functions. As member functions they can convert the right operand, but not the left.

橘虞初梦 2024-12-18 05:34:08

当您传递 const char* 时,可能会构造一个字符串对象并将其传递给operator+。如果您在调试器中单步执行代码,您可能能够验证构造函数是否被调用。

When you passed a const char*, a string object was probably constructed and passed to operator+. If you step through the code in a debugger you will probably be able to verify the constructor is being called.

且行且努力 2024-12-18 05:34:08

您的类中可能有一个构造函数,它将 const char * 作为输入参数,很可能该构造函数用于隐式转换和您看到的奇怪行为。

声明您的构造函数将 const char * 作为显式,这将禁用它在您不希望使用它的隐式转换中的使用。

You probably, have a constructor in your class which takes const char * as an input parameter, Most likely this constructor is used for implicit coversions and the weird behavior you see.

Declare your constructor which takes const char * as explicit, this would disable its use for implicit conversions where you do not intend it to be used.

面犯桃花 2024-12-18 05:34:08

您的“字符串”类是否有一个采用 const char* 的单参数构造函数?该单参数构造函数是否标记为“显式”?

如果不明确,最可能的答案是编译器正在通过转换构造函数从 char* 构造一个临时字符串对象,然后使用该临时字符串调用两个参数加法运算符。

Does your 'string' class have a one argument constructor that takes a const char*? Is that single argument constructor marked as 'explicit'?

If it is not explicit, the the most likely answer is that the compiler is constructing a temporary string object from the char* via your converting constructor, and then using that temporary string to call your two argument addition operator.

万劫不复 2024-12-18 05:34:08

除非您指定如何转换,否则编译器无法知道如何将字符串转换为 std::stringchar*

在类声明中查找转换构造函数或强制转换运算符。

class MyString
{
    MyString(char*);
    MyString(std::string);

    operator std::string ();
    operator char*();
};

这些将被隐式调用。

您可以为构造函数指定 explicit 关键字来防止这种情况发生。

如果这不能解决问题,则必须在某个地方重载运算符,最好使用调试器找到它们单步执行代码。

The compiler can't know how to convert your string to a std::string or char* unless you specify how.

Look for conversion constructors or cast operators in your class declaration.

class MyString
{
    MyString(char*);
    MyString(std::string);

    operator std::string ();
    operator char*();
};

These will be called implicitly.

You can specify the explicit keyword for your constructor to prevent this from happening.

If this doesn't solve the problem, you must have the operators overloaded somewhere, best find them stepping through the code with a debugger.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文