正在调用重载运算符->编译时就解决了?
当我尝试编译代码时: (注意: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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
是的,它被视为普通的函数调用,只是由重载运算符调用。编译器在编译时检查所有内容是否有效。 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 callsfunc()
.在 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;
}
是的,编译器像任何其他函数一样通过其结果检查运算符。
例如,在这种情况下,如果您有,
结果将是相同的。
Yes, the compiler checks operators by it's result as any other function.
In this case if you had, for example,
the result would be the same.
在 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.object->func()
只是用户定义类型的object->operator->()->func()
的语法糖。由于O::operator->()
产生S*
,因此需要存在S::func()
方法编译时间。object->func()
is just syntactic sugar forobject->operator->()->func()
for user-defined types. SinceO::operator->()
yields anS*
, this requires the existence of the methodS::func()
at compile time.