0

!=オペレーターをオーバーロードし、他のオペレーターオーバーロードの1つで使用した場合、class!=オーバーロードされていない、またはオーバーロードされたものとして受け入れられますか?noob_ptr(私が考えているカスタムポインタラッパーの一種)を作成しようとしています

class noob_ptr 
 {
      private: //does this change the behaviour? public? protected?
      bool operator!=(noob_ptr x)
       {
         ...
        }
      bool operator,(noob_ptr y)
      {
         ...
         if(y!=z)...
         ...
      }
    ...
      }

以下の例は、私のクラスでのオーバーロードされた演算子の使用をキャンセルしますか?

class noob_ptr 
 {
      protected: //or public
      bool operator,(noob_ptr y) //yes, z is also a noob_ptr
      {
         ...
         if(y!=z)...
         ...
      }
    ...
      private: 
      bool operator!=(noob_ptr x)
       {
         ...
        }
      }
4

3 に答える 3

1

タイプzもであるnoob_ptr場合、答えはYESです。これoperator !=により、クラスのオーバーロードが呼び出されます。また、この比較メソッドのシグネチャをお勧めします。

bool operator != (const noob_ptr& x) const;

したがって、定数ポインターに使用でき、オーバーロードされた演算子の呼び出し中にオブジェクトのコピーを回避することもできます。

UPDoperator != :プライベートとして宣言すると、クラスのすべてのメンバー関数noob_ptr、フレンドクラス、およびクラスの関数で使用できるようになります。noob_ptr他のすべての使用法では、次のようなメッセージを含むコンパイルエラーが発生します: "operator!= is inaccessibleその保護レベルのために」

于 2012-09-12T19:24:57.833 に答える
1

C++ は常に、修飾子、private、scope、namespace などのアクセス指定子で最も近い「最適な一致」を使用します。

したがって、グローバル名前空間 operator!= とクラス 1 (メソッドが const の場合、class& または const class& であると想定される左側の引数が欠けている場合) は、内部にあります。クラス(名前空間)は、クラス内のものを取得します。

グローバル名前空間とクラスの間に 1 つしかない場合は、明らかにそれを取得します。

次のコードは、グローバル スコープとクラス スコープの間を示しています。const 修飾子などを追加することで、これを拡張できます。

 #include <iostream>
using namespace std;

// Forward declaration to use without the class definition
class X;

bool operator!=(     int lhs, const X& rhs) {
  cout << "bool operator!=(     int lhs, const X& rhs)" << endl;
  return false;
}
bool operator!=(const X& lhs, const X& rhs) {
  cout << "bool operator!=(const X& lhs, const X& rhs)" << endl;
  return false;
}
bool operator!=(const X& lhs,      int rhs) {
  cout << "bool operator!=(const X& lhs,      int rhs)" << endl;
  return false;
}
// Note: Can't do: bool operator!=(int lhs, int rhs) -- already defined

class X {
private:
  int x;
public:
  X(int value) : x(value) { }

  bool operator !=(const X& rhs) {
    cout << "bool X::operator !=(const X& rhs)" << endl;
    return true;
  }

  bool operator !=(int rhs) {
    cout << "bool X::operator !=(int rhs)" << endl;
    return true;
  }

  void f() {
    X compare(1);
    cout << "X::f()" << endl;
    cout << (5 != 3) << endl;         // Uses built-in
    cout << (*this != 3) << endl;     // Uses member function
    cout << (*this != 1) << endl;     // Uses member function
    cout << (1     != *this) << endl; // There can be no member function, uses global namespace
    cout << (*this != compare) << endl;
  }
};


void f(const X& arg) {
  cout << "f()" << endl;
  X compare(1);
  cout << (5 != 3) << endl;         // Uses built in
  cout << (arg != 3) << endl;       // Uses global namespace
  cout << (arg != 1) << endl;       // Uses global namespace
  cout << (1   != arg) << endl;     // Uses global namespace
  cout << (arg != compare) << endl; // Uses global namespace
}

int main(int argc, char **argv) {
  X x(1);
  x.f();
  f(x);
  return 0;
}
于 2012-09-12T19:46:00.440 に答える
1

指定したオペランドが言語の組み込みが機能するものである場合!=、それが使用されます。operator!=それらがユーザー定義型の場合、それらの型 (またはそれらの型からの暗黙的な変換をサポートするいくつかの型) に対して定義されているユーザー定義を検索します。

于 2012-09-12T19:19:17.957 に答える