正在调用重载运算符->编译时就解决了?

发布于 2024-09-03 02:09:30 字数 553 浏览 5 评论 0原文

当我尝试编译代码时: (注意:func和func2不是错字)

struct S
{
    void func2() {}
};

class O
{
public:
    inline S* operator->() const;
private:
    S* ses;
};

inline S* O::operator->() const
{
    return ses;
}

int main()
{
    O object;
    object->func();
    return 0;
}

编译报错:

D:\code>g++ operatorp.cpp -S -o operatorp.exe
operatorp.cpp: In function `int main()':
operatorp.cpp:27: error: 'struct S' has no member named 'func'

好像调用了“operator->”的重载函数是在编译时完成的吗?我添加了“-S”选项仅用于编译。

when I tried to compile the code:
(note: func and func2 is not typo)

struct S
{
    void func2() {}
};

class O
{
public:
    inline S* operator->() const;
private:
    S* ses;
};

inline S* O::operator->() const
{
    return ses;
}

int main()
{
    O object;
    object->func();
    return 0;
}

there is a compile error reported:

D:\code>g++ operatorp.cpp -S -o operatorp.exe
operatorp.cpp: In function `int main()':
operatorp.cpp:27: error: 'struct S' has no member named 'func'

it seems that invoke the overloaded function of "operator->" is done during compile time? I'd added "-S" option for compile only.

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

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

发布评论

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

评论(5

〗斷ホ乔殘χμё〖 2024-09-10 02:09:30

是的,它被视为普通的函数调用,只是由重载运算符调用。编译器在编译时检查所有内容是否有效。 C++ 与动态语言不同,动态语言会等到运行时才确定 p->mynonexistantfunction() 是否是函数的有效名称,如果满足以下条件,编译器将在编译时拒绝该代码:函数名不存在。

在本例中,它看起来像是一个拼写错误,S 有一个函数 func2(),但您的代码调用 func()

Yes, it's treated like an ordinary function call, it's just called by an overloaded operator. The compiler checks everything is valid at compile time. C++ is not like dynamic languages where it waits until runtime to work out if p->mynonexistantfunction() is a valid name for a function or not, the compiler will reject the code at compile time if the function name does not exist.

In this case it looks like a typo, S has a function func2() but your code calls func().

娇妻 2024-09-10 02:09:30

在 struct S 中,您声明了 func2(),但在 main 中,您尝试调用 func()。

尝试
int main()
{
对象;
对象->func2();
返回0;
}

In struct S you declared func2(), but in main,you try to call func().

try
int main()
{
O object;
object->func2();
return 0;
}

夏天碎花小短裙 2024-09-10 02:09:30

是的,编译器像任何其他函数一样通过其结果检查运算符。

例如,在这种情况下,如果您有,

S* foo() { ... }

(foo())->func();

结果将是相同的。

Yes, the compiler checks operators by it's result as any other function.

In this case if you had, for example,

S* foo() { ... }

(foo())->func();

the result would be the same.

冷清清 2024-09-10 02:09:30

在 C++ 语言中,您无法在运行时按名称选择类成员。类成员选择(通过直接成员名称)始终在编译时完成。没有办法解决这个问题。

如果您想在运行时实现成员选择,唯一可以使用的是运算符 .*->* (前者 - 不可重载)。然而,这些内置形式的运算符期望将指向成员的指针作为其右侧操作数。如果您想按名称(作为字符串)选择某些内容,您可以重载 ->* 以使其采用不同的参数类型,但无论如何您都必须实现字符串的映射手动发送给实际成员。然而,对于成员函数(而不是数据成员)来说,这通常非常棘手。

In C++ language you cannot select a class member by name at run-time. Class member selection (by immediate member name) is always done at compile-time. There's no way around it.

If you want to implement member selection at run-time, the only thing you can use is operators .* and ->* (the former - non overloadable). However, these operators in their built-in form expect pointers-to-members as their right-hand operands. If you want to select something by name (as a string) you can overload ->* to make it take a different argument type, but in any case you'll have to implement the mapping from string to actual member manually. However, for member functions (as opposed to data members) this is usually pretty tricky.

二货你真萌 2024-09-10 02:09:30

object->func() 只是用户定义类型的 object->operator->()->func() 的语法糖。由于 O::operator->() 产生 S*,因此需要存在 S::func() 方法编译时间。

object->func() is just syntactic sugar for object->operator->()->func() for user-defined types. Since O::operator->() yields an S*, this requires the existence of the method S::func() at compile time.

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