|
이단자 님이 쓰신 글 :
: 현재 변수가 일반 변수인지 포인터 변수인지 검사하는 방법이 있나요?
:
: 현재 템플릿변수가 포인터변수인지 아닌지 확인을 하고 싶은데.. 어떻게 해야 하는지 쩝..
:
: 예를 들어
:
: template<typename T>
: class AAA
: {
: T bc;
: };
:
: 일경우 bc가 포인터인지 아닌지를 알고 싶은데..
:
: 포인터라면 AAA에서 메모리를 할당하거나 제거 하기 위해서 ...
RTTI 를 이용하시면 됩니다. C++ 에서는 RTTI 를 제공합니다. 실시간 형 정보를 얻을 수 있습니다.
typeid() 연산자를 호출하면 반환값으로 type_info 라는 클래스 인스턴스를 반환합니다. type_info
클래스를 보면
class type_info {
public:
virtual ~type_info();
int operator==(const type_info& rhs) const;
int operator!=(const type_info& rhs) const;
int before(const type_info& rhs) const;
const char* name() const;
const char* raw_name() const;
private:
...
};
이처럼 되어 있습니다.
name 과 raw_name 함수를 이용하시면 되겠네요.
아래는 예제입니다.
#include <windows.h>
#include <stdio.h>
#include <typeinfo.h>
template<class T>
class TYPE
{
private:
T a;
public:
void type_info(void);
};
template<class T>
void TYPE<T>::type_info(void)
{
printf("type_info %s\n", typeid(a).name());
printf("type_info %s\n", typeid(a).raw_name());
}
int main(void)
{
//일반 형 정보
int a=100;
int* pt_a = &a;
printf("type_info %s\n", typeid(a).name());
printf("type_info %s\n", typeid(pt_a).name());
printf("\n\n");
printf("type_info %s\n", typeid(a).raw_name());
printf("type_info %s\n", typeid(pt_a).raw_name());
printf("\n\n");
//템플릿 클래스에서의 형정보
TYPE<int> int_a;
int_a.type_info();
TYPE<int *> int_pa;
int_pa.type_info();
system("PAUSE");
return 0;
}
결과
type_info int
type_info int *
type_info .H
type_info .PAH
type_info int
type_info .H
type_info int *
type_info .PAH
|