虚拟方法被unique_ptr移动语义覆盖的问题

huangapple go评论77阅读模式
英文:

virtual method overridden issue with unique_ptr move semantics

问题

在下面的代码片段中,似乎虚拟方法在适配器对象中没有被覆盖,而且目标(Target)对象和适配器(Adapter)对象都显示相同的输出。

#include <iostream>
#include <memory>

using namespace std;

class Target
{
public:
    virtual std::string getResponse()
    {
        return "simple response";
    }
};

class Adaptee
{
public:
    std::string getSpecialRequest()
    {
        return "Special response";
    }
};

class Adapter : public Target
{
    unique_ptr<Adaptee> m_adaptee;

public:
    Adapter(unique_ptr<Adaptee> adaptee) : m_adaptee(std::move(adaptee))
    {
    }
    std::string getResponse() const
    {
        return m_adaptee->getSpecialRequest();
    }
};

class client
{
    unique_ptr<Target> m_target;

public:
    client(unique_ptr<Target> target)
    {
        m_target = std::move(target);
        std::cout << m_target->getResponse();
    }
};

int main()
{
    unique_ptr<Target> oj = make_unique<Target>();
    unique_ptr<Adaptee> oj1 = make_unique<Adaptee>();
    unique_ptr<Target> oj2 = make_unique<Adapter>(std::move(oj1));
    client instance(std::move(oj));
    client oj3(std::move(oj2));
    return 0;
}

输出结果:simple response simple response

期望的输出结果:simple response special response;

看起来这是从目标(Target)传输到适配器(Adapter)对象的对象转移的问题。请提供建议。

英文:

It looks like in below code snippet virtual method overridden is not happening with Adapter object and both Target and Adapter object is showing same output.

    #include &lt;iostream&gt;
    #include &lt;memory&gt;
    
    using namespace std;
    
    class Target
    {
       public:
        virtual std::string getResponse ()   
      {
              return &quot;simple response&quot;;
       }
   };
    
    class Adaptee
    {
     public:
       std::string getSpecialRequest ()   
      {
              return &quot;Special response&quot;;
       }
    };
    
    class Adapter:public Target
    {
       unique_ptr &lt; Adaptee &gt;  m_adaptee;
     public:
        Adapter (unique_ptr &lt; Adaptee &gt;  adaptee )  :  m_adaptee (std::move (adaptee))   
      {
          // m_adaptee = std::move (adaptee);
       }
         std::string getResponse ()  const 
      {
           return m_adaptee-&gt;getSpecialRequest ();
       }
     };
    
    class client
    {
        				
          unique_ptr &lt; Target &gt;  m_target;
        public:
          client ( unique_ptr &lt; Target &gt; target)    
      {
                m_target = std::move (target);
           std::cout &lt;&lt; m_target-&gt;getResponse ();
        }
     };
    
    int main ()
    {
          
          unique_ptr &lt;Target&gt; oj =  make_unique &lt;Target&gt; ();
          unique_ptr &lt;Adaptee &gt; oj1  =  make_unique &lt;Adaptee&gt; ();
          unique_ptr &lt; Target &gt; oj2  = make_unique&lt;Adapter&gt; (std::move(oj1));
          client instance (std::move (oj));
          client oj3 (std::move (oj2));
          return 0;
    }

output : simple response
simple response

expected output : simple response
special response;

it looks like it is issue of object transfer-ship from Target to Adapter object.Please suggest.

答案1

得分: 1

Adapter 类中声明的 getResponse 函数

std::string getResponse () const 
{
    return m_adaptee->getSpecialRequest ();
}

不是一个虚函数,它没有覆盖 Target 类中同名函数的虚函数

class Target
{
public:
    virtual std::string getResponse ()
    {
        return "simple response";
    }
};

因为这两个函数在 const 限定符上有差异。

在声明覆盖其他虚函数的虚函数时,始终使用 override 限定符。在这种情况下,如果函数实际上没有覆盖其他虚函数,编译器会发出错误。

例如,如果你写成这样:

std::string getResponse () const override
{
    return m_adaptee->getSpecialRequest ();
}

那么编译器会报错,问题将会变得清晰。

英文:

The function getResponse declared in the class Adapter

  std::string getResponse ()  const 
  {
       return m_adaptee-&gt;getSpecialRequest ();
  }

is not a virtual function that overrides the function with the same name in the class Target

class Target
{
   public:
    virtual std::string getResponse ()   
  {
          return &quot;simple response&quot;;
   }

};

because the functions differ in the qualifier const.

Always use the specifier override in declarations of virtual functions that override other virtual functions. In this case the compiler can issue an error if a function actually does not override other virtual function.

For example if you would write

  std::string getResponse ()  const override
  {
       return m_adaptee-&gt;getSpecialRequest ();
  }

then the compiler issued an error and it would be clear what is the problem.

huangapple
  • 本文由 发表于 2023年3月7日 19:33:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/75661446.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定