다시 활용하려면 캐스팅을 거쳐야만 한다.
(예제 1)
char *p;
void *c;
c=p;
char *v;
v=(char*)c;
(예제 2)
#include "stdafx.h"
#includeusing namespace std; void printit(void* pData, char cOption);
int main(int argc, char* argv[])
{
char cOption;char* pChar;
int* piValue;
float* pfValue;cout << "input output option\n" << " press c for char, i for int or f for float:";
cin >> cOption;
switch(cOption)
{
case 'c':
pChar = new char;
cout << "\nEnter a character:"; cin >> *pChar;
printit(pChar, cOption);
break;
case 'i':
piValue = new int;
cout << "\nEnter a interger:"; cin >> *piValue;
printit(piValue, cOption);
break;
default:
pfValue = new float;
cout << "\nEnter a float:"; cin >> *pfValue;
printit(pfValue, cOption);
}return 0;
}void printit(void* pData, char cOption)
{
cout << "\nDaynamic data type entered was ";switch(cOption)
{
case 'c':
cout << "char and a value of " << *(char*)pData;
break;
case 'i':
cout << "int and a value of " << *(int*)pData;
break;
default:
cout << "float and a value of " << *(float*)pData;
}
cout << "\n";
delete pData;
}
추가: static void의 개념은?
관련 내용 답변:
Submitted by 정장혁 on 수, 2003/06/25 - 10:01am.
static void 선언은 리턴값은 void고, 함수의 형태가 static인것을 말하는것 같습니다.
기본 C에서는 (ANSI표준인지는 모르겠는데) 함수선언시 명시하지 않으면,extern으로 선언 됩니다.
static함수이므로, 당연히 선언된 파일 *.c에서만 call할수 있지요. :D
Submitted by anfl on 수, 2003/06/25 - 10:02am.
함수의 선언부에 static이 붙어 있어면 그 함수의 사용범위를 그 파일내에 한정 짓겠다는 말입니다.
즉
a.c
static void func() {}
이말은 func 함수의 범위를 a.c내로 한정 짓겠다는 말입니다.
b.c
int main() {
func();
return 0;
}
gcc -o test a.c b.c
이렇게 하면 컴파일 오류가 나죠.
b.c에서는 func가 안보이니깐요.
주로 커널 같이 수많은 심볼이 등록되어 있어서 symbol의 충돌이 발생할수 있는 경우에 static로 범위를 파일내로 한정해서 많이 사용한답니다.
답변 출처: static void 의 개념은?
0 개의 댓글:
댓글 쓰기