在指针上使用重载运算符 [英] Using overloaded operators on pointers

查看:90
本文介绍了在指针上使用重载运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我重载了 <<类的运算符.如果我想在指针上使用它,我该如何重载运算符,如下所示?

I overloaded the << operator of a class. How do I have to overload the operator if I want to use it on pointers, like the following?

class A {
    std::string operator<<(std::string&);
}

aInst << "This works";
aPointer << "This doesnt work";
aPointer->operator<<("Whereas this works but is useless");

希望你能帮我.

海因里希

推荐答案

需要先解引用指针.

A *ptr = new A();
(*ptr) << "Something";

唯一的另一种方式是你上面描述的方式

The only other way is the way you described above

下面安德烈的解决方案也是可行的,但正如他所说,这可能不是一个好主意.

Andre's solution below is workable as well, but like he said it may not be a good idea.

这篇关于在指针上使用重载运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆