Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/158.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 类/结构函数是否存储在对象中?_C++_Class_Object_Storage - Fatal编程技术网

C++ 类/结构函数是否存储在对象中?

C++ 类/结构函数是否存储在对象中?,c++,class,object,storage,C++,Class,Object,Storage,假设: struct A { int a; int b; }; struct B { int a; int b; int func() { return this->a + this->b; } }; B的实例是否包含指向func的指针 要用代码说明此问题,请执行以下操作: A a; // first 4 bytes are for `int a`, second 4 bytes are for `int b` B

假设:

struct A {
    int a;
    int b;
};

struct B {
    int a;
    int b;

    int func() {
        return this->a + this->b;
    }
};
B
的实例是否包含指向
func
的指针

要用代码说明此问题,请执行以下操作:

A a; // first 4 bytes are for `int a`, second 4 bytes are for `int b`
B b: // ditto, but is there an extra word for a pointer to function `func`?

否。
a
b
大小完全相同(
b
不存储指向
func
的指针)

< C++中的类函数没有与对象本身链接(尖),它们只是作为任何其他函数存储。当您调用一个类函数时,您只是在调用一个普通函数(您不是从指针调用它)。这就是为什么要做像
b.func=other\u func在C++中是非法的。
要在代码中说明这一点,请执行以下操作:

/////////
// C++
/////////
struct B {
    int a;
    int b;

    int func() {
        return this->a + this->b;
    }
};

B b;
b.func();


//////////////////////////////
// Example compile output if it was compiled to C (not actual compile output!)
// i.e. this C code will do the equivalent of the C++ code above
//////////////////////////////
struct B {
    int a;
    int b;
};

int B_func(struct B* this) {
    return this->a + this->b;
}

B b;
B_func(&b);

// This should illustrate why it is impossible to do this in C++:
// b.func = another_func;

否。
a
b
大小完全相同(
b
不存储指向
func
的指针)

< C++中的类函数没有与对象本身链接(尖),它们只是作为任何其他函数存储。当您调用一个类函数时,您只是在调用一个普通函数(您不是从指针调用它)。这就是为什么要做像
b.func=other\u func在C++中是非法的。
要在代码中说明这一点,请执行以下操作:

/////////
// C++
/////////
struct B {
    int a;
    int b;

    int func() {
        return this->a + this->b;
    }
};

B b;
b.func();


//////////////////////////////
// Example compile output if it was compiled to C (not actual compile output!)
// i.e. this C code will do the equivalent of the C++ code above
//////////////////////////////
struct B {
    int a;
    int b;
};

int B_func(struct B* this) {
    return this->a + this->b;
}

B b;
B_func(&b);

// This should illustrate why it is impossible to do this in C++:
// b.func = another_func;

C++函数实际上只是(可能)将指向对象的隐藏指针作为第一个参数的函数。独特性是通过以下方式实现的


除非功能是虚拟的;具有一个或多个虚拟函数的类具有“虚拟函数表”(vtable),这些类的实例具有指向其实例特定vtable的指针的开销。vtable是在对象之前还是之后取决于编译器/实现。

C++函数实际上只是(可能)将指向对象的隐藏指针作为第一个参数的函数。独特性是通过以下方式实现的


除非功能是虚拟的;具有一个或多个虚拟函数的类具有“虚拟函数表”(vtable),这些类的实例具有指向其实例特定vtable的指针的开销。vtable是在对象之前还是之后取决于编译器/实现。

您可能需要补充的是,虚拟函数有点不同。成员函数并不完全是“任何普通函数”。他们确实隐藏了这个
参数。事实上,C++示例正是C++编译器将要做的…也许你的措词把我弄糊涂了,你实际上就是这个意思。@PetrBudnik是的,我的措词不是最好的:P基本上,我的意思是,当你调用一个成员函数时,你只是正常地调用它,而不是通过存储在对象内部的指针。您可能想补充一点,虚拟函数有点不同。成员函数并不完全是“任何普通函数”。他们确实隐藏了这个
参数。事实上,C++示例正是C++编译器将要做的…也许你的措词把我弄糊涂了,你实际上就是这个意思。@PetrBudnik是的,我的措词不是最好的:P基本上,我的意思是,当你调用成员函数时,你只是正常地调用它,而不是通过存储在对象中的指针。