#define _CRT_SECURE_NO_WARNINGS #include <iostream> using namespace std; class Test { public: Test(int i) { mI = i; } int getI() { //this就是指向調用改爲員函數方法的對象地址 return this->mI; //return mI; } private: int mI; }; /* struct Test { int mI; }; void Test_init(Test *pthis, int i) { pthis->mI = i; } int getI(struct Test *pthis) { return pthis->mI; } */ int main(void) { Test t1(10);//Test(&t1, 10) Test t2(20); t1.getI();// getI(&t1) return 0; }
#define _CRT_SECURE_NO_WARNINGS #include <iostream> using namespace std; class Test { public: Test(int k) { this->m_k = k; } int getK() const//成員函數尾部出現const 修飾是this指針 { //this->m_k = 100; //this指針不是 const Test * //this++;// this指針是一個常指針, Test *const //this->m_k = 100; //this = this + 1; return this->m_k; } //static成員函數,只能返回static成員變量 static int s_getK() { //return m_k; return s_k; } private: int m_k; static int s_k; }; int Test::s_k = 0; int main(void) { Test t1(10); //Test(&t1, 10); Test t2(20); return 0; }
若是想返回一個對象的自己,在成員方法中,用*this返回
}ios
#define _CRT_SECURE_NO_WARNINGS #include <iostream> using namespace std; class Test { public: Test(int a, int b) { this->a = a; this->b = b; } void printT() { cout << "a = " << this->a << ", b=" << this->b << endl; } int getA() { return this->a; } int getB() { return this->b; } //成員方法 Test TestAdd(Test &another) { Test temp(this->a + another.a,this->b + another.b); return temp; } //+= 方法 Test& TestAdd2(Test &another) { this->a += another.a; this->b += another.b; //this===>&t1 return *this;//若是想返回一個對象的自己,在成員方法中,用*this返回 } private: int a; int b; }; /* //1 在全局提供一個兩個Test想加的函數 Test TestAdd(Test &t1, Test &t2) { Test temp(t1.getA() + t2.getA(), t1.getB() + t2.getB()); return temp; } */ int main(void) { Test t1(10, 20); Test t2(100, 200); //Test t3 = TestAdd(t1, t2); Test t3 = t1.TestAdd(t2); t3.printT(); //((t1 += t2) += t2 )+= t2 //若是相對一個對象連續調用成員方法,每次都會改變對象自己,成員方法須要返回引用。 t1.TestAdd2(t2).TestAdd2(t2); t1.printT(); return 0; }
main.cppc++
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #include <string.h> #include <iostream> #include <fstream> #include "MyArray.h" using namespace std; int main(void) { MyArray array1(10);//開闢10元素的數組 //賦值操做 for (int i = 0; i < 10; i++) { array1.setData(i, i + 10); } cout << "--------" << endl; cout << "array1:" << endl; for (int i = 0; i < 10; i++) { cout << array1.getData(i) << " "; } cout << endl; MyArray array2 = array1; cout << "array2:" << endl; for (int i = 0; i < array2.getLen(); i++) { cout << array2.getData(i) << " "; } cout << endl; MyArray array3; array3 = array1; cout << "array3:" << endl; for (int i = 0; i < array3.getLen(); i++) { cout << array3.getData(i) << " "; } cout << endl; return 0; }
Array.h數組
#pragma once #include <iostream> using namespace std; class MyArray { public: MyArray(); MyArray(int len); MyArray(const MyArray &another); ~MyArray(); void setData(int index, int data); int getData(int index); int getLen(); void operator=(const MyArray& another); private: int len; int *space; };
Array.cpp函數
#include "MyArray.h" MyArray::MyArray() { cout << "MyArray()..." << endl; this->len = 0; this->space = NULL; } MyArray::MyArray(int len) { if (len <= 0) { this->len = 0; return; } else { this->len = len; //給space開闢空間 this->space = new int[this->len]; cout << "MyArray::MyArray(int len) ..." << endl; } } MyArray::MyArray(const MyArray &another) { if (another.len >= 0) { this->len = another.len; //深拷貝 this->space = new int[this->len]; for (int i = 0; i < this->len; i++) { this->space[i] = another.space[i]; } cout << "MyArray::MyArray(const MyArray &another) ..." << endl; } } MyArray::~MyArray() { if (this->space != NULL) { delete[]this->space; this->space = NULL; len = 0; cout << "MyArray::~MyArray() ..." << endl; } } void MyArray::setData(int index, int data) { if (this->space != NULL) { this->space[index] = data; } } int MyArray::getData(int index) { return this->space[index]; } int MyArray::getLen() { return this->len; } void MyArray::operator=(const MyArray& another) { if (another.len >= 0) { this->len = another.len; //深拷貝 this->space = new int[this->len]; for (int i = 0; i < this->len; i++) { this->space[i] = another.space[i]; } cout << "MyArray::operator=(const MyArray& another) ..." << endl; } }