Оператор стрелки, вызывающий ссылку на классC++

Программы на C++. Форум разработчиков
Anonymous
Оператор стрелки, вызывающий ссылку на класс

Сообщение Anonymous »

У меня есть примеры примеров: < /p>
  • Использование Template - Compiles без выпуска < /p>

    Код: Выделить всё

    #include 
    #include 
    using namespace std;
    
    template  class Test;
    template 
    bool operator==(const Test &, const Test &);
    
    template 
    class Test {
    friend bool operator==(const Test &, const Test &);
    private:
    int data;
    };
    
    template 
    bool operator==(const Test &lhs, const Test &rhs)
    {
    return lhs->data == rhs->data; // clause 1. This seems to compile even though lhs/rhs is not a pointer. The arrow operator would be something like (*lhs).data. But in this case lhs/rhs is a reference.
    }
    
    int main()
    {
    return 0;
    }
    < /code>
    < /li>
      Без шаблона - компилируется с выпуском < /p>
    #include 
    #include 
    using namespace std;
    
    class Test {
    friend bool operator==(const Test&, const Test&);
    public:
    int data;
    };
    
    bool operator==(const Test&lhs, const Test&rhs)
    {
    return lhs->data == rhs->data; // clause 2. Now this fail which is expected.
    //return lhs.data == rhs.data; // clause 3. This would work as expected.
    }
    
    int main()
    {
    return 0;
    }
    
Похоже, что пункт 1 противоречит значению использования оператора стрелки в качестве LHS/RHS не является указателем, но еще он составил. Это должно было терпеть неудачу, но это не так. Какая -либо причина такого поведения?

Подробнее здесь: https://stackoverflow.com/questions/794 ... r-from-inv

Вернуться в «C++»