c ++ sizeof()一个类的函数 [英] c++ sizeof() of a class with functions

查看:203
本文介绍了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。
II真的不明白为什么它返回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()一个类的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆