c vs c++ in strcut and class
总习惯用c的用法,现在学习C++,老爱拿来比较。声明我用的是g++4.2.1 SUSE Linux。看例子吧
- #include <iostream>
-
#include <cstring>
-
#include <string>
-
using namespace std;
-
enum zoo_obj_kind{
-
null = 0,
-
#define null null
-
no = 0,
- #define no no
-
animal = 2,
- #define animal animal
-
plant = 4,
- #define plant plant
-
others = 8
- #define others others
-
};
-
struct zoo_obj{
-
zoo_obj_kind zo_kind;
-
char name [40];
-
};
-
class zoo_obj_1{
-
zoo_obj_kind zo_kind;
-
char name [40];
-
};
-
int main(void){
-
cout << "struct :" << sizeof(struct zoo_obj) << endl;
-
cout << "clsas :" << sizeof( zoo_obj_1) << endl;
- }
结果
- struct size:44
- clsas size:44
-------------------------------
- #include <iostream>
-
#include <cstring>
-
#include <string>
-
using namespace std;
-
enum zoo_obj_kind{
-
null = 0,
-
#define null null
-
no = 0,
- #define no no
-
animal = 2,
- #define animal animal
-
plant = 4,
- #define plant plant
-
others = 8
- #define others others
-
};
-
struct zoo_obj{
-
zoo_obj_kind zo_kind;
-
char name [40];
-
void (*say)(struct zoo_obj *);
-
};
-
void say(struct zoo_obj *obj){
-
if(!obj) {
-
printf("null\n");
-
return ;
-
}
-
printf("name:%s\n",obj->name);
-
}
-
class zoo_obj_1{
-
zoo_obj_kind zo_kind;
-
char name [40];
-
void say(zoo_obj_1 &obj){
-
cout << "name:" << name << endl;
-
}
-
};
-
int main(void){
-
cout << "struct :" << sizeof(struct zoo_obj) << endl;
-
cout << "clsas :" << sizeof( zoo_obj_1) << endl;
- }
结果
- struct size:48
- clsas size:44
呵呵,有意思吧,在class中成员函数不占空间。下面你看看他们有多像
- #include <iostream>
-
#include <cstring>
-
#include <string>
-
using namespace std;
-
enum zoo_obj_kind{
-
null = 0,
-
#define null null
-
no = 0,
- #define no no
-
animal = 2,
- #define animal animal
-
plant = 4,
- #define plant plant
-
others = 8
- #define others others
-
};
-
struct zoo_obj{
-
zoo_obj_kind zo_kind;
-
char name [40];
-
void (*say)(struct zoo_obj &);
-
};
-
void say(struct zoo_obj &obj){
-
printf("name:%s\n",obj.name);
-
}
-
class zoo_obj_1{
-
public:
-
zoo_obj_kind zo_kind;
-
char name [40];
-
void say(){cout << "name:" << name << endl;}
-
void say(zoo_obj_1 &obj){cout << "name:" << obj.name << endl;}
-
};
-
typedef struct zoo_obj s_zoo_obj;
-
typedef zoo_obj_1 c_zoo_obj;
-
int main(void){
-
s_zoo_obj s_obj = {animal,"dog",say};
-
zoo_obj_1 c_obj = {animal,"cat"};
-
cout << "struct size:" << sizeof(struct zoo_obj) << endl;
-
cout << "clsas size:" << sizeof( zoo_obj_1) << endl;
-
s_obj.say(s_obj);
-
c_obj.say(c_obj);
- }
结果
- struct size:48
- clsas size:44
- name:dog
- name:cat
这是同时使用了引用,那么指针呢。struct的指针当然没有问题,那么class的指针呢?看看代码
- #include <iostream>
-
#include <cstring>
-
#include <string>
-
using namespace std;
-
enum zoo_obj_kind{
-
null = 0,
-
#define null null
-
no = 0,
- #define no no
-
animal = 2,
- #define animal animal
-
plant = 4,
- #define plant plant
-
others = 8
- #define others others
-
};
-
struct zoo_obj{
-
zoo_obj_kind zo_kind;
-
char name [40];
-
void (*say)(struct zoo_obj *);
-
};
-
void say(struct zoo_obj *obj){
-
!obj
-
? printf("null\n")
-
: printf("name:%s\n",obj->name);
-
}
-
class zoo_obj_1{
-
public:
-
zoo_obj_kind zo_kind;
-
char name [40];
-
void say(){cout << "name:" << name << endl;}
-
void say(zoo_obj_1 *obj){
-
!obj
-
? cout << "null\n"
-
: cout << "name:" << obj->name << endl;
-
}
-
};
-
typedef struct zoo_obj s_zoo_obj;
-
typedef zoo_obj_1 c_zoo_obj;
-
int main(void){
-
s_zoo_obj s_obj = {animal,"dog",say};
-
zoo_obj_1 c_obj = {animal,"cat"};
-
cout << "struct size:" << sizeof(struct zoo_obj) << endl;
-
cout << "clsas size:" << sizeof( zoo_obj_1) << endl;
-
s_obj.say(&s_obj);
-
c_obj.say(&c_obj);
-
s_obj.say(NULL);
-
c_obj.say(NULL);
- }
哈哈,结果仍然是
- struct size:48
- clsas size:44
- name:dog
- name:cat
更高级的特性呢?
比如在继承,接口。。。
个人认为C的这类名词没有,但他确实能出色的实现诸如此类的功能,而且更直观。可能是我的C++还不行吧。C高效和简单直观是没得说的。