现在的位置: 首页 > 综合 > 正文

【C++ Primer】深度解析struct –面试常见题

2013年09月10日 ⁄ 综合 ⁄ 共 2238字 ⁄ 字号 评论关闭

一,C和C++间struct的深层区别

  1)在C++语言中struct具有了“类” 的功能,其与关键字class的区别在于struct中成员变量和函数的默认访问权限为public,而class的为private。
   例如,定义struct类和class类:

struct structA
{
char a;

}
class classB
{
      char a;  //默认为私有的
      …
}

  则:
       struct A a;
                a.a = 'a';    //访问public成员,合法
      classB b;
                b.a = 'a';    //访问private成员,不合法

  许多文献写到这里就认为已经给出了C++中struct和class的全部区别,实则不然,另外一点需要注意的是:

  C++中的struct保持了对C中struct的全面兼容(这符合C++的初衷——“a better c”),因而,下面的操作是合法的:


struct structA
{
     char a;
     char b;
     int c;
};

structA a = {'a' , 'a' ,1};    // 定义时直接赋初值

  即struct可以在定义的时候直接以{ }对其成员变量赋初值,而class则不能。

      

   2)在C中,struct不能包含函数。在C++中,对struct进行了扩展,可以包含函数

#include   "stdio.h"

void fun()  
{  
 printf("hello,world ");  

}  
struct test  
{  
  void (*Fun)();  

}; 

int main()  
{  
  struct test _t;  
  _t.Fun = fun;  //一定要这样让一个函数的地址赋值给结构的成员函数
 (*_t.Fun)();  //通过函数地址调用函数 

 return 0;  
}  

以上程序运行正确,如果在结构体内包含函数实现就不行了。例如:

#include   

struct test  
{   
      void fun(){printf("hello,world ");} //不允许

}; 
int main()  
{  
  struct test _t;   

 _t.fun();

 return 0;  

}  

  3)C++中结构struct 可以包含构造函数

#include <iostream>
using namespace std;

void Fun();
struct test
{
       test(int n){         
           cout<<"test("<<n<<")"<<endl;
       }

       test(){
           cout<<"test()"<<endl;
}

      void (*Fun)();
  };
  
void Fun(){

    cout<<"Fun()"<<endl;
         // printf("Fun()\n");
      }
  
  int main()
  {
      test a(1);
      a.Fun=Fun;
      (*a.Fun)();
      
      return 0; 
  }

  4)struct 嵌套问题(C语言)

#include "stdio.h"

typedef struct   test2
{
         int   aa;
         int   bb;
}test2;
typedef struct   test1
{
    test2 t;
        
}test1;
int main()
{        
      printf("%d\n",sizeof(test1));
      
      return 0; 
}

输出为: 8

另一种表达方式

#include "stdio.h"
typedef struct   test1
{
     struct   test2
   {
         int   aa;
         int   bb;
   }test2;           //特别注意,如果这里没有变量,则sizeof 就是1
        
}test1;
int main()
{
          
      printf("%d\n",sizeof(test1));
      
      return 0; 
}


二,struct编程注意事项

  看看下面的程序:

1. #include <iostream.h>

2. struct structA
3. {
4.       int      iMember;
5.       char  *cMember;
6. };

7. int main(int argc, char* argv[])
8. {
9.        structA instant1,instant2;
10.      char c = 'a';
   
11.      instant1.iMember = 1;
12.      instant1.cMember = &c;

13.     instant2 = instant1;

14.     cout << *(instant1.cMember) << endl;

15.    *(instant2.cMember) = 'b';

16.    cout << *(instant1.cMember) << endl;

17.    return 0;
     }

  14   行的输出结果是:a
       16   行的输出结果是:b    //改变是instant2  怎么instant1也被改变了呢?

  我们在15行对instant2的修改改变了instant1中成员的值!

  原因在于13行的instant2 = instant1赋值语句采用的是变量逐个拷贝,这使得instant1和instant2中的cMember指向了同一片内存,因而对instant2的修改也是对instant1的修改。

  在C语言中,当结构体中存在指针型成员时,一定要注意在采用赋值语句时是否将2个实例中的指针型成员指向了同一片内存

  在C++语言中,当结构体中存在指针型成员时,我们需要重写struct的拷贝构造函数并进行“=”操作符重载

 三,有关大小对齐等问题参考

struct 内存对齐

抱歉!评论已关闭.