πŸ‘©‍πŸ’» ν”„λ‘œκ·Έλž˜λ°/🎠 C++

[C++] 볡사 μƒμ„±μž | κΉŠμ€ 볡사와 얕은 볡사

peewoong 2024. 4. 19. 15:13
// C μŠ€νƒ€μΌ μ΄ˆκΈ°ν™”
int num = 20;
int &ref = num;

// C++ μŠ€νƒ€μΌ μ΄ˆκΈ°ν™”
int num(20);
int &ref(num);

μœ„ λ¬Έμž₯듀은 λͺ¨λ‘ λ™μΌν•˜λ‹€.

class SoSimple{
private:
	int num1;
    int num2;
public:
	SoSimple(int n1, int n2) : num1(n1), num2(n2){}
    
    void ShowSimpleData(){
    	cout << num1 << endl;
        cout << num2 << endl;
    }
}

int main(void){
	SoSimple sim1(15,20);
    SoSimple sim2 = sim1;
    sim2.ShowSimpleData();
    return 0;
}

λŒ€μž…μ—°μ‚°μ˜ 의미처럼 μ‹€μ œ 멀버 λŒ€ λ©€λ²„μ˜ 볡사가 μΌμ–΄λ‚œλ‹€.

SoSimple sim2 = sim1;
SoSimple sim2(sim1);

μœ„ 두 λ¬Έμž₯은 λ™μΌν•˜λ‹€.

 

🌟 SoSimple sim2(sim1) 해석

SoSimpleν˜• 객체λ₯Ό  μƒμ„±ν•˜λΌ.

객체의 이름은 sim2

sim1을 인자둜 받을 수 μžˆλŠ” μƒμ„±μžμ˜ ν˜ΈμΆœμ„ ν†΅ν•΄μ„œ 객체생성을 μ™„λ£Œν•œλ‹€.

SoSimple(SoSimple &copy) : num1(copy.num1), num2)(copy.num2) {
	cout << "Called SoSimple(SoSimple &copy)" << endl;
}

πŸ‘‰ SoSimple sim2=sim1은 λ¬΅μ‹œμ μœΌλ‘œ SoSimple sim2(sim1)으둜 ν•΄μ„λœλ‹€.

 

🟩 λ””ν΄νŠΈ 볡사 μƒμ„±μž

볡사 μƒμ„±μžλ₯Ό μ •μ˜ν•˜μ§€ μ•ŠμœΌλ©΄, 멀버 λŒ€ λ©€λ²„μ˜ 볡사λ₯Ό μ§„ν–‰ν•˜λŠ” λ””ν΄νŠΈ 볡사 μƒμ„±μžκ°€ μ‚½μž…λœλ‹€.

SoSimple(const SoSimple &copy) : num1(copy.num1), num2(copy.num2){}

 

λ¬΅μ‹œμ  ν˜•λ³€ν™˜μ„ λ§‰μœΌλ €λ©΄ explicit을 ν™œμš©ν•œλ‹€.

explicit SoSimple(const SoSimple &copy) : num1(copy.num1), num2(copy.num2){
	// empty
}

Sosimple obj = 3 // μ΄λŸ¬ν•œ ν˜•νƒœμ˜ 객체 생성 λΆˆκ°€λŠ₯

 

🟩 문제점

객체 μ†Œλ©Έμ‹œ λ¬Έμ œκ°€ λ˜λŠ” ꡬ쑰 (얕은 볡사)

class Person{
private:
	char * name;
    int age;
    
public:
	Person(char * myName, int myAge){
    	int len = strlen(myName) + 1;
        name = new char[len];
        strcpy(name, myName);
        age = myAge;
    }
    
    ~Person(){
    	delete []name;
    }
}

 

🌟 κΉŠμ€ 볡사λ₯Ό μœ„ν•œ 볡사 μƒμ„±μž μ •μ˜

Person(const Person &copy) : age(copy.age){
	name = new char[strlen(copy.name)+1];
    strcpy(name, copy.name);
}

 

🟩 볡사 μƒμ„±μž 호좜 μ‹œμ 

λ©”λͺ¨λ¦¬ κ³΅κ°„μ˜ ν• λ‹Ήκ³Ό μ΄ˆκΈ°ν™”κ°€ λ™μ‹œμ— μΌμ–΄λ‚˜λŠ” 상황

 

1. 기쑴에 μƒμ„±λœ 객체λ₯Ό μ΄μš©ν•΄μ„œ μƒˆλ‘œμš΄ 객체λ₯Ό μ΄ˆκΈ°ν™”ν•˜λŠ” 경우(μ•žμ„œ 보인 경우)

Person man1("Lee dong woo", 29);
Person man2 = man1; // 볡사 μƒμ„±μž 호좜

 

2. call-by-value λ°©μ‹μ˜ ν•¨μˆ˜ν˜ΈμΆœ κ³Όμ •μ—μ„œ 객체λ₯Ό 인자둜 μ „λ‹¬ν•˜λŠ” 경우

3. 객체λ₯Ό λ°˜ν™˜ν•˜λ˜, μ°Έμ‘°ν˜•μœΌλ‘œ λ°˜ν™˜ν•˜μ§€ μ•ŠλŠ” 경우

SoSImpel SimpleFuncObj(SoSimple ob){ // 인자 전달 μ‹œ μ„ μ–Έκ³Ό λ™μ‹œμ— μ΄ˆκΈ°ν™”
	...
	return ob; // λ°˜ν™˜μ‹œ λ©”λͺ¨λ¦¬ 곡간 ν• λ‹Ήκ³Ό λ™μ‹œμ— μ΄ˆκΈ°ν™”
}

int main(void){
	SoSimple obj;
    SimpleFuncObj(obj);
}

 

🌟 λ°˜ν™˜ν•  λ•Œ λ§Œλ“€μ–΄μ§„ 객체의  μ†Œλ©Έ μ‹œμ 

class Temporary{
private:
	int num;
    
public:
	Temporary(int n) : num(n) {
    	cout << "create obj : " << num < endl;
    }
    
    ~Temporary(){
    	cout << "destroy obj : " << num << endl;
    }
    
    void ShowTempInfo(){
    	cout << "My num is " << num << endl;
    }
}

int main(void){
	Temporary(100);
    cout << "******** after make!" << endl << endl;
    
    Tmporary(200).ShowTempInfo();
    cout << "******** after make!" << endl << endl;
    
    const Temporary &ref = Temporary(300);  // 참쑰값이 λ°˜ν™˜λ˜λ―€λ‘œ 참쑰자둜 μ°Έμ‘° κ°€λŠ₯
    cout << "******** end of main!" << endl << endl;
    return 0;
}

const 참쑰값은 λ‹€ λλ‚˜κ³  μ†Œλ©Έλœλ‹€.