C++ 中 struct 和 class的区别

struct的成员变量和函数的访问权限级别默认是public的,而class中是private型的。

另:在VS2010中,可以在struct 中定义函数体,与class相同。方法如下:

#include <stdio.h>

struct test
{
    void fun()
    {
        printf("hello,world\n");
    }
};

int main()
{
    struct test _t;
    _t.fun();
    return 0;
}

而VC6.0只支持函数指针,不能直接定义函数。

方法如下:

#include <stdio.h>

void fun()

{

    printf("hello,world\n");

}

struct test

{

    void (*Fun)();

};

int main()

{

    struct test _t;

    _t.Fun = fun;
 
    (*_t.Fun)();

    return 0;

}

C结构体内不能有函数的代码,但可以有函数的指针

原文地址:https://www.cnblogs.com/wenshanzh/p/2831142.html