c++ 템플릿 예제 질문
이퓨리한은지
먼저 이 예제는 Animal의 무게를 나타내는 Animal class와 이 class를 가지고 배열을 만드는 Array class가 있습니다. main문에서 처음에 객체의 인스턴스를 생성하는 부분에서 생성자 함수가 어떻게 호출이 되는지 궁금해서 질문을 하고자 합니다.main문을 보시면
1. Arrayint theArray; // an array of integer 2. ArrayAnimal theZoo; // an array of Animals
이렇게 두가지가 있는데요. 객체를 생성하면 메모리가 할당되고 생성자 함수를 호출하게 됩니다. 첫번째 자료형이 int로 나타나 있을 때는 Array가 int로 템플릿화 되어있으니까
// implement the Constructor
template class T
ArrayT::Array(int size) :
itsSize(size)
{
pType = new T[size];
// the constructors of the type you are creating
}
위의 생성자 함수가 호출되어 size만큼 배열이 만들어 지는데요두번째 ArrayAnimal theZoo; 의 경우 에는 템플릿화 된 생성자 함수중에 copy constructor 외에는 호출이 될 수 있는 곳이없는 것 같습니다 그래서 첫번째 int 경우처럼 size만큼 배열을 만들 수 있는 곳이 copy constructor밖에 없는것 같은데요.복사 생성자(copy constructor)가 호출되는 경우는
1. 기존에 생성된 객체를 이용해서 새로운 객체를 초기화하는 경우2. call-by-value 방식의 함수호출 과정에서 객체를 인자로 전달하는 경우3. 객체를 반환하되 참조형으로 반환하지 않는 경우
이렇게 세가지가 있다고 합니다. 여기서 2번째 객체를 인자로 전달하는 경우가 근접한거 같은데이것때문에 ArrayAnimal theZoo;의 경우copy constructor가 호출이 되는게 맞을까요??
실제 호출이 되는지 보기위해 copy constructor에 std::cout 나는 theZoo; 구문을 넣어보았지만이 문장이 출력이 되지 않더라구요 ArrayAnimal theZoo;는 어떤 생성자를 호출하게 되는지 궁금합니다.
===================================================================================================================
#include iostreamconst int DefaultSize = 10;
//an array of Animal을 만들기 위해 Animal class를 선언class Animal
{
public:
Animal(int);
Animal();
~Animal() {}
int GetWeight() const { return itsWeight; }
void Display() const { std::cout itsWeight; }
private:
int itsWeight;
};Animal::Animal(int weight) :
itsWeight(weight)
{}Animal::Animal() :
itsWeight(0)
{}
template class T
class Array
{
public:
// constructors
Array(int itsSize = DefaultSize);
Array(const Array &rhs);
~Array() { delete[] pType; }// operators
Array& operator=(const Array&);
T& operator[](int offSet) { return pType[offSet]; }
const T& operator[](int offSet) const
{return pType[offSet];}
// accessors
int GetSize() const { return itsSize; }private:
T *pType;
int itsSize;
};// implementations follow...// implement the Constructor
template class T
ArrayT::Array(int size) :
itsSize(size)
{
pType = new T[size];
// the constructors of the type you are creating
// the constructors of the type you are creating
}// copy constructor
template class T
ArrayT::Array(const Array &rhs)
{
itsSize = rhs.GetSize();
pType = new T[itsSize];
for (int i = 0; iitsSize; i++)
pType[i] = rhs[i];
}// operator=
template class T
ArrayT& ArrayT::operator=(const Array &rhs)
{
if (this == &rhs)
return *this;
delete[] pType;
itsSize = rhs.GetSize();
pType = new T[itsSize];
for (int i = 0; iitsSize; i++)
pType[i] = rhs[i];
return *this;
}// driver program
int main()
{
Arrayint theArray; // an array of integers
ArrayAnimal theZoo; // an array of Animals
Animal *pAnimal;// fill the arrays
for (int i = 0; i theArray.GetSize(); i++)
{
theArray[i] = i * 2;
pAnimal = new Animal(i * 3);
theZoo[i] = *pAnimal;
delete pAnimal;
}
// print the contents of the arrays
for (int j = 0; j theArray.GetSize(); j++)
{
std::cout theArray[ j ]:\t;
std::cout theArray[j] \t\t;
std::cout theZoo[ j ]:\t;
theZoo[j].Display();
std::cout std::endl;
}return 0;
}
d