English 中文(简体)
基级中的私人元素是否增加衍生类的大小?
原标题:Do private elements in the base class add to the size of the derived class?
// inheritence experiment

#include"stdafx.h"
#include<iostream> 

using namespace std;

class base
{
private:
    int i;
};

class derived: public base
{
private:
    int j;
};

int main()
{
    cout << endl << sizeof(derived) << endl << sizeof(base);
    derived o1;
    base o2;
    cout << endl << sizeof(o1) << endl << sizeof(o2); 
}

我得到了这个输出 :

8
4
8
4

为何如此?一个基级的私人数据成员没有继承到衍生类,所以为什么我同时得到8字节,即派生和O1的大小?

最佳回答
class base {
public:
  base(int v) : m_value(v) { }
  int value() { return m_value; }
private:
  int m_value;
};

class derived : private base {
  derived(int v) : base(v) { }
  int value2() { return value(); }
};

int main()
{
  derived d(2);
  return d.value2();
}

如果私人基地阶级不将价值储存在某个地方,你又如何指望这个计划能奏效呢?

您选择的单词 "继承到衍生类" 使我认为您对继承如何运作感到困惑。 基类的成员不会继承 into 或复制 in to 衍生类。 衍生类包含一个基准类型的实例, 作为完整衍生对象中的一个子对象, 因此导出类至少( 通常) 与基类的和一样大 。

考虑此代码 :

struct A {
private:
  int i;
};

struct B {
  int j;
};

struct derived : A, B {
  int k;
};

An A object is laid out in memory as an int, and so is a B. A derived object could be laid out in memory as an A object, followed by a B object, followed by an int, which means it is a sequence of three ints. Whether the base class members are public or private doesn t affect that.

问题回答

私人成员是继承的 你只是不能从你的派生阶级获得他们

私人成员实际上在派生类中,它们只是在派生类中无法进入。 私人关键词是防止来自基级的程序员改变基级设计师不希望你改变的数值, 因为它可能会改变基级的功能 。

找找更深入的解释

Fragile basicle level

当然,他们是继承的——毕竟,基础阶级的行为 可能取决于他们是否在那里—— 它们只是无法进入!

基本等级的继承方法仍然可以使用私人的memebers。 他们必须在那里。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?