Using overloaded operators on pointers

后端 未结 4 903
悲&欢浪女
悲&欢浪女 2021-02-19 15:28

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::str         


        
4条回答
  •  感情败类
    2021-02-19 15:46

    You need to dereference the pointer first.

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

    The only other way is the way you described above

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

提交回复
热议问题