struct A{ A() {} ~A() {} int m1; int m2; }; struct B:A{ B() {} ~B() {} int m1; char m2; static char m3; }; struct C{ C() {} virtual~C() {} int m1; short m2; };
答:分别为8字节,16字节,12字节。A只有两个int,所以是8字节;B继承A,char因为4字节对齐占4个字节,static不存储在类中,所以B=8+4+4=16字节;C因为4字节对齐,所以成员变量占8字节,有虚函数加4字节虚指针一共12字节。
void print( const std::list<int> &I) { //访问const必须使用const迭代器 for(const std::list<int>::const_iterator it = I.begin(); it!=I.end(); it++) std::cout<<*it<<std::endl; }
答:使用extern
int checkstr(const char *msg) { int i = 0, len; while(msg[i] != '\0')i++; len = i; for(i = 0; i < len/2; i++) if(msg[i] != msg[len-i-1]) return -1; return 0; }
typedef struct Node { struct Node *next; }Node; Node* findnode(Node *head, unsigned int k);
答:
Node* findnode(Node *head, unsigned int k) { int cnt = 0; Node *p = head; while(p) { cnt++; p = p->next; } if(cnt < k) return NULL; cnt = cnt - k - 1; p = head; while(cnt--) p = p->next; return p; }
int count = 3; int main() { int i, sum, count = 2; for(i = 0, sum = 0; i < count; i += 2,count++) { static int count = 4; count++; if(i % 2 == 0) { extern int count; count++; sum += count; } sum += count; } cout<<count<<' '<<sum<<endl; return 0; }
答:结果为:4 20。分析:extern是引用全局的count,定义为static的count只初始化一次,第二次跳过这条语句。for循环里的count指的是main()里的,static int count=4这句只执行一次,count++是static这个count,最后输出的是main()里的count。
void func(char str[50]) { cout<<sizeof(str)<<' '<<strlen(str)<<endl; } int main() { char stra[] = "HelloWorld"; char *strb = stra; cout<<sizeof(stra)<<' '<<sizeof(strb++)<<endl; func(++strb); cout<<strlen(stra)<<' '<<strlen(strb++)<<endl; return 0; }
答: 11 4 4 9 10 9 sizeof(stra) = 11:char数组的大小=数组元素的个数=字符个数+1 sizeof(strb++) = 4:strb是一个指针,在32位系统中,指针的大小是4。注意sizeof有一个特性:编译的时候就把strb替换成类型了,所以里面任何加减操作都是无效的,事实上++没有执行 sizeof(str) = 4:不管数组作为参数怎么表示,数组还是以地址来传递的。str是一个指向stra[1]的指针,所以大小是4,与后面的[50]没有关系 strlen(str) = 9:strlen是指不包括'\0'的字符的个数,与[50]没有关系 strlen(stra) = 10:strlen不包括'\0' strlen(strb++) = 9:strb指向stra[1],再计算长度,再更改strb为stra[2]
struct SC{int a,b,c;}; struct SD{int a,b,c,d;}; int main() { struct SC c1[] = {{3}, {4}, {5}, {6}}; struct SD *c2 = (struct SD*)c1 + 1; cout<<c2->a<<c2->b<<c2->c<<c2->d<<endl; return 0; }
答:0050 c1的存储方式是这样的: 3 0 0 | 4 0 0 | 5 0 0 | 6 0 0 c1[0] | c1[2] | c1[3] | c1[4] c1转换为CD结构后是这样的: 3 0 0 4 | 0 0 5 0 | 0 6 0 0 c1[0] | c1[1] | c1[2] c2 = c1 + 1,因此c2指向转换后的c1[1],即0050
class Base { public: int m_a; Base(int a = 2):m_a(a){cout<<'A'<<m_a;} virtual ~Base(){cout<<'B'<<m_a;} }; class Derived:public Base { public: Derived(int a = 4):Base(a){cout<<'C'<<m_a;} ~Derived(){cout<<'D'<<m_a;} }; int main() { Base *aa, bb; aa = new Derived; delete aa; return 0; }
答:
答:A2A4C4D4B4B2 L17:生成一个基类对象,调用一次基类的构造函数 L18:生成一个子类的对象,先调用基类构造函数,再调用子类构造函数 L19:由于析构函数是虚函数,根据待释放对象的类型来调用析构函数。aa指向的是子类对象,先调用子类析构函数,再调用基类析构函数 L21:bb是栈内对象,自动释放,调用一次基类的析构函数
class Base { public: int m_a, m_b; Base(int a = 3, int b = 5):m_a(a), m_b(b){} int func_a(){return m_a - m_b;} virtual int func_b(){return m_a + m_b;} }; class Derived:public Base { public: Derived(int a = 4, int b = 7):Base(a, b){} virtual int func_a(){return m_b + m_a;} int func_b(){return m_b - m_a;} }; int main() { Base *aa, *bb; aa = new Base(4, 7); bb = new Derived(3, 5); cout<<aa->func_a()<<' '<<aa->func_b()<<' '<<bb->func_a()<<' '<<bb->func_b()<<endl; delete aa;delete bb; return 0; }
答:-3 11 -2 2