English 中文(简体)
“建筑部有公共遗产,不成事实”。
原标题:"a struct has public inheritance by default"

这种说法真的意思是,“建筑部有公共继承的违约”。 为什么以下法典有误,因为我没有公开关键词,而是从c类中得出?

struct c 
{
protected:
    int i;
public:
    c(int ii=0):i(ii){} 
    virtual c *fun();
};

c* c::fun(){
    cout<<"in c";
    return &c();
}

class d : c
{  
public:
    d(){}
    d* fun()
    {
        i = 9;
        cout<<"in d"<< 	 <<i;
        return &d();
    }
};


int main()
{
    c *cc;
    d dd;
    cc = &dd;
    cc->fun();
}
最佳回答

这意味着:

struct c;
struct d : c

等于

struct d : public c

页: 1 扩大<代码>struct:

struct c;
class d : c;

等于

class d : private c;

由于<条码><>/条码>在违约情况下具有私人继承。

And it means that all inherited and not overriden/overloaded/hidden methods from c are private in d.

问题回答

“建筑部有公共继承,不成事实”意味着:

struct Derived : Base {};

is equivalent to

struct Derived : public Base {};

Classes have private inheritance by default, so when you remove the public from a class inheritance you have the equivalent of

class Derived : private Base {};

在这种私人继承情景中,Derived没有is-aBase的关系,实质上是has-aBase。 因此,您正试图在此尝试:

cc = &dd;

is not allowed.

当你写上<条码>和从某种东西继承而没有注明出处方时,遗产被作为<条码>公开<>。 当你写上<条码>并继承某条,但没有具体说明出处方(即便是哪条是<条码>/条码>),继承即被视为私人继承。 在你的法典中,你重新做后一项工作,因此继承是私人的,因此发现的错误。

Put another way, to say that struct inheritance is public by default really means that inheritance done when writing a struct is public by default, not that inheriting from a struct is public by default.





相关问题
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?

热门标签