2011. 3. 7. 20:53
<const키워드 기능>
1. const 키워드는 변수의 선언 앞에 붙어서 변수를 상수화한다.
const double PI=3.14;
PI=3.1415; //컴파일 오류


2. const 키워드는 포인터가 가리키는 데이터를 상수화한다.
int n=10;
const int* pN=&n;
*pN=20; //컴파일 오류


3. const 키워드는 포인터 선언 시 이름 앞에 붙어서 포인터 자체를 상수화한다.
int n1=10;
int n2=20;
int* const pN=&n1;
*pN=20; //OK
pN=&n2; //컴파일 오류
=> 현재 pN은 끝까지 n1만을 가리켜야 한다.


참고: 열혈강의 C++

'Language > C언어' 카테고리의 다른 글

memset함수  (0) 2014.05.09
10진수 2진수, 8진수, 16진수로 표현 - - C Programming  (0) 2011.10.06
매크로와 전처리  (0) 2011.03.02
열거형(enum) 자료형  (0) 2011.03.01
문자열 처리함수  (0) 2011.03.01
Posted by Triany
2011. 3. 7. 17:54
<복사생성자가 호출되는 시점>
1. 기존에 생성된 객체로 새로운 객체를 초기화하는 경우
2. 함수 호출 시 객체를 값에 의해 전달하는 경우
3. 함수 내에서 객체를 값에 의해 리턴하는 경우



1. 기존에 생성된 객체로 새로운 객체를 초기화하는 경우

 #include <iostream>
using std::cout;
using std::cin;
using std::endl;

class AAA
{
public:
 AAA()
 {
  cout<<"AAA() 호출"<<endl;
 }
 AAA(const AAA& a)
 {
  cout<<"AAA(const A& a) 호출"<<endl;
 }
};
int main()
{
 AAA obj1;
 AAA obj2=obj1; //AAA ojb2(obj1)이라는 문장으로 묵시적으로 변환됨

 return 0;
}



2. 함수 호출 시 객체를 값에 의해 전달하는 경우
call-by-value 함수 호출 과정
1) 매개 변수를 위한 메모리 공간 할당
2) 전달 인자 값의 복사
  => 복사생성자의 호출을 통해서 이 과정을 처리

 #include <iostream>
using std::cout;
using std::cin;
using std::endl;

class AAA
{
public:
 int val;
 AAA()
 {
  cout<<"AAA() 호출"<<endl;
 }
 AAA(int i)
 {
  val=i;
 }
 AAA(const AAA& a)
 {
  cout<<"AAA(const A& a) 호출"<<endl;
  val=a.val;
 }
 void ShowData()
 {
  cout<<"val: "<<val<<endl;
 }
};
void function(AAA a)
{
 a.ShowData();
}
int main()
{
 AAA obj1(30);
 function(obj1);

 return 0;
}




3. 함수 내에서 객체를 값에 의해 리턴하는 경우
- 리턴되는 값은 받아주는 변수가 없더라도, 함수를 호출한 영역으로 복사되어 넘어간다.
- 호출한 영역으로 객체가 복사되어 넘어갈 것.!

 #include <iostream>
using std::cout;
using std::cin;
using std::endl;

class AAA
{
public:
 int val;
 AAA()
 {
  cout<<"AAA() 호출"<<endl;
 }
 AAA(int i)
 {
  val=i;
 }
 AAA(const AAA& a)
 {
  cout<<"AAA(const A& a) 호출"<<endl;
  val=a.val;
 }
 void ShowData()
 {
  cout<<"val: "<<val<<endl;
 }
};
AAA function(void)
{
 AAA a(10);
 return a;
}
int main()
{
 function(); //function().ShowData();

 return 0;
}





출처: 열혈강의 C++프로그래밍
Posted by Triany
2011. 3. 7. 17:32
아래와 같이 변수를 동적할당 해 줄 경우,
명시적으로 복사생성자를 정의해 주어야 한다.
=>생성자 내에서 동적할당을 하면 반드시 제공해야 하는 것은 소멸자이다. 소멸자가 있어야 메모리 누수(유출)이 발생하지 않는다.
뿐만 아니라 복사 생성자도 정의해야 한다. 그래서 메모리 참조를 막을 수 있다.

 #include <iostream>
using std::cout;
using std::cin;
using std::endl;

class Person
{
 char *name;
 char *phone;
 int age;
public:
 Person(char* _name, char* _phone, int _age);
 Person(const Person& p);
 ~Person();
 void ShowData();
};
Person::Person(char* _name, char* _phone, int _age)
{
 name = new char[strlen(_name)+1];
 strcpy(name, _name);
 phone = new char[strlen(_phone)+1];
 strcpy(phone, _phone);
 age = _age;
}

Person::Person(const Person& p)

 name = new char[strlen(p.name)+1];
 strcpy(name, p.name);
 phone = new char[strlen(p.phone)+1];
 strcpy(phone, p.phone);
 age = p.age;
}

Person::~Person()
{
 delete[] name;
 delete[] phone;
}
void Person::ShowData()
{
 cout<<"name : "<<name<<endl;
 cout<<"phone: "<<phone<<endl;
 cout<<"age  : "<<age<<endl;
}
int main()
{
 Person p1("KIM", "013-333-5555", 22);
 Person p2=p1; //Person p2(p1);
 return 0;
}


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

멤버 이니셜라이저의 필요성 _const 멤버 변수를 초기화  (0) 2011.03.07
복사생성자가 호출되는 시점  (0) 2011.03.07
객체 포인터 배열  (0) 2011.03.04
생성자와 동적할당  (0) 2011.03.04
new / delete  (0) 2011.03.03
Posted by Triany