关于void*函数返回
一、
sample
#include<iostream> using namespace std; void* test(void* pass) { return pass; } int _tmain(int argc, _TCHAR* argv[]) { char* a=(char*)malloc(sizeof(char)); a="a"; char* b = (char*)malloc(sizeof(char)); b=(char*)test(a); cout<<"b:"<<b<<endl; cout<<"a:"<<a<<endl; getchar(); return 0; }
注意 test的返回值为void* 要强制转换成char*指针
二、
#include <iostream> using namespace std; class CBase { public: CBase(int a,long b,char c) { a=a; b=b; c=c; } private: int a; long b; char c; }; int main() { unsigned long num1 = 0; unsigned long num2 = 0; unsigned long num3 = 0; unsigned long num4 = 0; unsigned long num5 = 0; CBase *base1 = new CBase(10,100,'d'); CBase *base2 = new CBase(10,200,'c'); num1 = *((unsigned long*)base1); num2 = *((unsigned long*)base2); num3 = unsigned long((unsigned long*)base1); num4 = unsigned long((unsigned long*)base2); cout << "num1:"<< num1<< endl; cout << "num2:"<< num2<< endl; cout<<endl; cout << "num3:"<< num3<< endl; cout << "num4:"<< num4<< endl; getchar(); return 0; }
1)由于类的private里的数据排列时 先int,long然后是char.
指针Base1和Base2的指针自身的地址是不一样的;指向的内容不一样;
但是当指针强制转换成long(32位下是4个字节)型的时候;在再解引用时候;他们指向是分别是Base1类的前四个字节和Base2类的前四个字节;而由于他们的前四个字节
? 解引用为什么不是10?
用c++的强转