The Wayback Machine - https://web.archive.org/web/20180310024439/http://ja.cppreference.com:80/w/cpp/language/operators
名前空間
変種
操作

operators

提供: cppreference.com
< cpp‎ | language

 
 
C++言語
一般的なトピック
フロー制御
条件付き実行文
繰り返し文 (ループ)
ジャンプ文
関数
関数宣言
ラムダ関数宣言
inline 指定子
例外指定(廃止予定)
noexcept 指定子(C++11)
例外
名前空間
指定子
decltype(C++11)
auto(C++11)
alignas(C++11)
記憶域期間指定子
初期化
代替表現
リテラル
ブーリアン - 整数 - 浮動小数点
文字 - 文字列 - nullptr(C++11)
ユーザ定義(C++11)
ユーティリティ
アトリビュート(C++11)
typedef 宣言
型エイリアス宣言(C++11)
キャスト
暗黙の変換 - 明示的な変換
static_cast - dynamic_cast
const_cast - reinterpret_cast
メモリ確保
クラス
クラス固有の関数特性
特別なメンバ関数
テンプレート
その他
 

目次

[編集]演算子のオーバーロード

[編集]構文

typeoperatorop(params) ;

[編集]説明

  • <type>は/変数のタイプ(複数可)ですさ.
    Original:
    <type> is/are the type(s) of the variables.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • <op>には、特定の演算子(例えば++=<<>>&&||%など)です.
    Original:
    <op> is the particular operator (e.g. +, +=, <<, >>, &&, ||, %, etc.).
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • は、<params>は/必要なパラメータ(オペレータに依存します)の名前(s)です.
    Original:
    <params> is/are the name(s) of the required parameters (depends on the operator).
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.

[編集]制限

  • あなたは**または&|などの新しい演算子を作成することはできません.
    Original:
    You cannot create new operators such as ** or &|.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • すべての演算子はオーバーロードできません
    Original:
    Not all operators can be overloaded
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • いくつかの演算子は、非静的クラスのメンバとしてオーバーロードすることができます
    Original:
    Some operators can only be overloaded as non-static class members
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • 短絡評価はオーバーロードされた演算子では動作しません
    Original:
    Short-circuit evaluation doesn't work with overloaded operators
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.

[編集]オペレータコール

オーバーロードされた演算子は通常の中置記法を使って呼び出すことができます
Original:
Overloaded operators can be called using the usual infix notation
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
a+b
や関数のような表記
Original:
or a function-like notation
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
operator+(a,b)

[編集]

#include <iostream>usingnamespace std;   class Fraction{private:int numerator, denominator;   public: Fraction(int n, int d): numerator(n), denominator(d){}// Note that the keyword operator combined with an actual// operator is used as the function namefriend ostream& operator<<(ostream&, Fraction&);};   ostream& operator<<(ostream& out, Fraction& f){ out << f.numerator<<'/'<< f.denominator;return out;}   int main(){ Fraction f1(3, 8); Fraction f2(1, 2);   cout << f1 << endl; cout <<3<<' '<< f2 << endl;   return0;}

出力:

3/8 3 1/2

[編集]参照

Common operators
代入incrementNJdecrement算術論理比較memberNJaccess他の

a = b
a = rvalue
a += b
a -= b
a *= b
a /= b
a %= b
a &= b
a |= b
a ^= b
a <<= b
a >>= b

++a
--a
a++
a--

+a
-a
a + b
a - b
a * b
a / b
a % b
~a
a & b
a | b
a ^ b
a << b
a >> b

!a
a && b
a || b

a == b
a != b
a < b
a > b
a <= b
a >= b

a[b]
*a
&a
a->b
a.b
a->*b
a.*b

a(...)
a, b
(type) a
?:

Special operators
static_cast別の互換性のあるタイプ
に1型に変換します
Original:
static_cast converts one type to another compatible type
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
dynamic_cast派生class
に仮想基底クラスに変換します
Original:
dynamic_cast converts virtual base class to derived class
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
const_cast異なるcvqualifiers
と互換性のある型に型変換されます
Original:
const_cast converts type to compatible type with different cv qualifiers
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
reinterpret_cast互換性type
に型を変換します
Original:
reinterpret_cast converts type to incompatible type
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
new割り当てmemory
Original:
new allocates memory
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
delete割り当て解除memory
Original:
delete deallocates memory
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
sizeoftype
のサイズを照会します
Original:
sizeof queries the size of a type
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
sizeof...パラメーターパック(C++11およびそれ以降)
のサイズを照会します
Original:
sizeof... queries the size of a パラメーターパック(C++11およびそれ以降)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
typeidtype
の型情報を照会します
Original:
typeid queries the type information of a type
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
noexcept式が例外(C++11およびそれ以降)
を投げることができるかどうかをチェックします
Original:
noexcept checks if an expression can throw an exception (C++11およびそれ以降)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
alignofタイプ(C++11およびそれ以降)のクエリアラインメント要件を
Original:
alignof queries alignment requirements of a type (C++11およびそれ以降)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
close