最終更新日時(UTC):
が更新

履歴 編集

function
<complex>

std::complex::コンストラクタ

complex(const T& re = T(), const T& im = T());            // (1) C++03
constexpr complex(const T& re = T(), const T& im = T());  // (1) C++14

complex(const complex& other);                            // (2) C++03
constexpr complex(const complex& other);                  // (2) C++14
constexpr complex(const complex& other) = default;        // (2) C++23

template <class X>
complex(const complex<X>& other);                         // (3) C++03
template <class X>
constexpr complex(const complex<X>& other);               // (3) C++14
template <class X>
constexpr explicit(see below)
  complex(const complex<X>& other);                       // (3) C++23

概要

  • (1) : 実部(re)と虚部(im)の値をそれぞれ受け取って構築
  • (2) : コピーコンストラクタ
  • (3) : 変換可能な要素型のcomplexオブジェクトからのコピー

効果

  • (1) : 実部(re)、虚部(im)の値をそれぞれ受け取り、メンバ変数に保持する。どちらもデフォルト値は0.0である。
  • (2), (3) : otherが持つ実部および虚部の値を、*thisにコピーする。

備考

  • (3) : 浮動小数点数型として縮小変換となるものは explicit と宣言されているため、暗黙の型変換には使用されない

#include <iostream>
#include <complex>

int main()
{
  // (1)
  // 実部(real)に1.0f、虚部(imag)に2.0fを設定して構築
  std::complex<float> c1(1.0f, 2.0f);

  // (2)
  // コピー構築
  std::complex<float> c2 = c1;

  // (3)
  // 変換可能なcomplexオブジェクトからコピー
  std::complex<double> c3 = c2;

  // (3)
  // 縮小変換となるコンストラクタは explicit
  // std::complex<float> c4 = c3; // エラー
  std::complex<float> c4(c3);     // 直接初期化なら OK

  std::cout << "c1 : " << c1 << std::endl;
  std::cout << "c2 : " << c2 << std::endl;
  std::cout << "c3 : " << c3 << std::endl;
  std::cout << "c4 : " << c4 << std::endl;
}

出力

c1 : (1,2)
c2 : (1,2)
c3 : (1,2)
c4 : (1,2)

関連項目

名前 説明
real 実部を取得、あるいは、設定する。
imag 虚部を取得、あるいは、設定する。

参照