简体   繁体   English

在工作中运行“运算符ClassName *”。 这是什么意思?

[英]Ran into this at work “operator ClassName *”. What does this mean?

The class with this code is a reference class for a pointer of ClassName, ie: 具有此代码的类是ClassName指针的引用类,即:

class ClassName;

class ClassRef
{
    ClassName* m_class;
    ...
    operator ClassName *() const { return m_class; }
...

I am assuming this is used for pointer validity checks, such as: 我假设这用于指针有效性检查,例如:

ClassRef ref(new ClassName())
if (ref) { bla bla bla }

Am I correct in my thinking? 我的想法是否正确?

This is an overload of the conversion operator. 这是转换运算符的重载。 Whenever a ClassRef object needs to be converted to a ClassName pointer type, this operator is called. 每当需要将ClassRef对象转换为ClassName指针类型时,都会调用此运算符。

So; 所以;

ClassRef r;
ClassName * p = r;

will make use of this overload. 将利用这种超载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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