728x90
반응형
#include <iostream>
#include <cstring>
using namespace std;

class SoSimple
{
private:
	int num;

public:
	SoSimple(int n) : num(n)
	{
		cout << "num = " << num<<", ";
		cout << "address=" << this << endl;
		//cout << "size = " << sizeof(this) << endl;
	}
	void ShowSimpleData()
	{
		cout << num << endl;
	}
	SoSimple* GetThisPointer()
	{
		return this;
	}
};

int main(void)
{
	SoSimple sim1(100);
	SoSimple* ptr1 = sim1.GetThisPointer();
	cout << ptr1 << ",";
	ptr1->ShowSimpleData();

	SoSimple sim2(200);
	SoSimple* ptr2 = sim2.GetThisPointer();
	cout << ptr2 << ",";
	ptr2->ShowSimpleData();
	
	return 0;
}
#include <iostream>
using namespace std;

class SelfRef
{
private:
	int num;
public:
	SelfRef(int n) : num(n)
	{
		cout << "Generate Object !" << endl;
	}
	SelfRef& Adder(int n)
	{
		num += n;
		return *this;
	}
	SelfRef& ShowTwoNumber()
	{
		cout << num << endl;
		return *this;
	}
};

int main(void)
{
	SelfRef obj(3);
	SelfRef& ref = obj.Adder(2);

	obj.ShowTwoNumber();
	ref.ShowTwoNumber();
	
	ref.Adder(1).ShowTwoNumber().Adder(2).ShowTwoNumber();

	return 0;
}
728x90
반응형

'Language > C & C++' 카테고리의 다른 글

[Instant]Homework  (0) 2022.10.18
[C++]Related to Copy Constructor  (0) 2022.10.17
[C++] Object Array  (0) 2022.10.17
[C++] Constructor & Destructor  (0) 2022.10.17
[C++]How to create two Class-based object(*, &)  (0) 2022.10.17

+ Recent posts