c++ sizeof() of a class with functions(具有函数的类的c ++ sizeof())
问题描述
我有一个 C++ 问题.我写了以下课程:
I have a C++ question. I wrote the following class:
class c
{
int f(int x, int y){ return x; }
};
c 类的 sizeof() 返回1".我真的不明白为什么它会返回 1.
the sizeof() of class c returns "1". I I really don't understand why it returns 1.
为了更好地理解发生了什么,我添加了另一个函数:
Trying to understand better what is going on, I added another function:
class c
{
int f(int x, int y){ return x; }
int g(int x, int y){ return x; }
};
现在下面真的让我很困惑!sizeof(c) 仍然是 1 (!?!?!?!).所以我猜函数不会改变类的大小,但是为什么呢???为什么尺寸是 1 ?它是特定于编译器的吗?
Now the following really got me confused! sizeof(c) is still 1 (!?!?!?!). So I guess that functions doesn't change the size of the class, but why??? and why does the size is 1 ? And is it compiler specific ?
谢谢!:-)
推荐答案
该类不包含数据成员,因此为空.标准要求每个班级至少有大小 1,所以这就是你得到的.(成员函数实际上并不在类内部",它们实际上只是具有隐藏参数、命名空间和访问控制的自由函数.)
The class contains no data members, so it's empty. The standard demands that every class have at least size 1, so that's what you get. (Member functions aren't physically "inside" a class, they're really just free functions with a hidden argument and a namespace and access control.)
这篇关于具有函数的类的c ++ sizeof()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:具有函数的类的c ++ sizeof()
基础教程推荐
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 初始化列表*参数*评估顺序 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- CString 到 char* 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
