OperatorのOverloadでConstructor
C++の入門者です。下記(引用1)のIBM Knowledge Centerの例題にはConstructorが記述して有りますが、その次に引用の(引用2乃至3)の例題にはConstructorに見える記述がありません。これはどういうことですか?私の見方が間違っていたら教えてください。。
###(引用1)
https://www.ibm.com/support/knowledgecenter/en/SSLTBW_2.3.0/com.ibm.zos.v2r3.cbclx01/cplr318.htm
html
1#include <iostream> 2using namespace std; 3 4class complx 5{ 6 double real, 7 imag; 8public: 9 complx( double real = 0., double imag = 0.); // constructor 10 complx operator+(const complx&) const; // operator+() 11}; 12 13// define constructor 14complx::complx( double r, double i ) 15{ 16 real = r; imag = i; 17} 18 19// define overloaded + (plus) operator 20complx complx::operator+ (const complx& c) const 21{ 22 complx result; 23 result.real = (this->real + c.real); 24 result.imag = (this->imag + c.imag); 25 return result; 26} 27 28int main() 29{ 30 complx x(4,4); 31 complx y(6,6); 32 complx z = x + y; // calls complx::operator+() 33}
###(引用2乃至3)
http://wisdom.sakura.ne.jp/programming/cpp/cpp27.html
html
1#include<iostream> 2using namespace std; 3 4class String { 5public: 6 char *str; 7 String operator =(char *str) { 8 this->str = str; 9 return *this; 10 } 11}; 12 13int main() { 14 String str1 , str2; 15 str1 = str2 = "Kitty on your lap\n"; 16 cout << str1.str << str2.str; 17 return 0; 18}
html
1#include<iostream> 2using namespace std; 3 4class POINT { 5public: 6 long x; 7 long y; 8 void operator ++(int n) { 9 x++; y++; 10 } 11 POINT operator ++() { 12 ++x; ++y; 13 return *this; 14 } 15} obj1 , obj2 ; 16 17int main() { 18 obj1.x = 10; obj2.y = 5; 19 obj2 = ++obj1; 20 obj2++; 21 22 cout << "x = " << obj1.x << "\ty = " << obj1.y << '\n'; 23 cout << "x = " << obj2.x << "\ty = " << obj2.y << '\n'; 24 return 0; 25}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2019/01/26 03:59
2019/01/26 04:14
退会済みユーザー
2019/01/26 04:16
退会済みユーザー
2019/01/26 04:48
2019/01/26 05:09
2019/01/26 05:13
退会済みユーザー
2019/01/26 05:21
2019/01/26 06:46