1

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 <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))   
      {
          // 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;
    }

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.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
user2997518
  • 812
  • 6
  • 17
  • 1
    This function std::string getResponse () const is not a virtual function. – Vlad from Moscow Mar 07 '23 at 11:36
  • Thanks , You have saved my day . I am wondering even chatgpt also couldn't find this mistake and giving multiple solution to fix this which is not working at all . It was my mistake that I forgot to use override keyword in Adapter class – user2997518 Mar 07 '23 at 11:41
  • The function in the base class is not `const`. The one in the derived class is. So the signatures don't match, and your derived class is NOT overriding the inherited virtual function. – Peter Mar 07 '23 at 11:41
  • Se dupe: [virtual function const vs virtual function non-const](https://stackoverflow.com/questions/9488168/virtual-function-const-vs-virtual-function-non-const). Refer to [how to ask](https://stackoverflow.com/help/how-to-ask) where the first step is to search and then research and you'll find plenty of related SO posts for this. – Jason Mar 07 '23 at 11:48
  • I wasn't aware about my mistake and this kind of issue can be caught using code review . – user2997518 Mar 07 '23 at 11:57

1 Answers1

1

The function getResponse declared in the class Adapter

  std::string getResponse ()  const 
  {
       return m_adaptee->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 "simple response";
   }

};

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->getSpecialRequest ();
  }

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

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335