我错过了私人继承的东西吗? [英] Am I missing something in private inheritance?

查看:57
本文介绍了我错过了私人继承的东西吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以解释为什么这会编译?

class HiddenSealBaseClass

{

public:

HiddenSealBaseClass( ){}

};


类密封:虚拟HiddenSealBaseClass

{};


class SomeClass:Sealed

{

//无论什么

};



int main()

{

SomeClass obj;

}

因为我们有*私人*虚拟继承,公共构造函数

HiddenSealBaseClass()应该在Sealed

派生后变为私有,因此SomeClass无法访问。


-

Ioannis Vranos

http://www23.brinkster.com/noicys

解决方案

" Ioannis Vranos" < iv*@remove.this.grad.com>写了...

可能有人解释为什么这会编译?

class HiddenSealBaseClass
{
公开:
HiddenSealBaseClass(){}
};

类密封:虚拟HiddenSealBaseClass
{};

类SomeClass:密封
//

};

int main()
{
SomeClass obj;
}

因为我们有* private *虚拟继承,公共构造函数
HiddenSealBaseClass()应该在密封派生后变为私有,
因此SomeClass无法访问。




这完全是胡说八道。


A级{

公开:

void foo();

};


B级:A {//私人继承

};


int main (){

A a;

a.foo(); //根据你不应该编译因为

//其他人私下从A继承

}


Victor


* Victor Bazarov:

" Ioannis Vranos" < iv*@remove.this.grad.com>写了...

可能有人解释为什么这会编译?

class HiddenSealBaseClass
{
公开:
HiddenSealBaseClass(){}
};

类密封:虚拟HiddenSealBaseClass
{};

类SomeClass:密封
//

};

int main()
{
SomeClass obj;
}

因为我们有* private *虚拟继承,公共构造函数
HiddenSealBaseClass()应该在密封派生后变为私有,
因此SomeClass无法访问。



那是'完全胡说八道。

A班{
公开:
void foo();
};

B班:A {//私人继承
};

int main(){
A a;
a.foo(); //根据你不应该编译因为
//其他人私下从A继承
}




这个问题比较微妙比起那个,Victor,在你的例子中

有点类似于


C级:公共B

{

void victorLookHere(){foo(); }

};


可能使类比分解的是这是一个关于

的默认构造函数和虚拟继承,不是普通的

继承和一个普通的成员函数或构造函数。


有疑问的原因是至少有三个现代编译器

不要抱怨。


-

答:因为它弄乱了人们通常阅读文字的顺序。

问:为什么这么糟糕?

A:热门发布。

问:usenet和电子邮件中最烦人的事情是什么?


Victor Bazarov写道:

这完全是胡说八道。

A级{公开:
void foo();
};

B类:A {//私人继承
};
int main(){
a a;
a.foo(); //根据你不应该编译因为
//其他人私下从A继承
}




Nope 。我正在使用两个课程,而我正在使用三个课程。使你的

例子更等效:


A级{

public:

void foo( );

};


B级:A {

};

C级: B {};


int main(){

C a;

a.foo();

}

这不能编译。


甚至更多等价物:


A类{

public:

void foo();

};


B级:虚拟A {

};

C级:B {};


int main(){

C a;

a.foo();

}


上面也没有编译,但它确实编译如果我们使用

公共A()构造函数。


-

Ioannis Vranos

http://www23.brinkster.com/noicys


May someone explain why does this compile?
class HiddenSealBaseClass
{
public:
HiddenSealBaseClass() { }
};

class Sealed: virtual HiddenSealBaseClass
{};

class SomeClass: Sealed
{
// Whatever
};


int main()
{
SomeClass obj;
}
Since we have *private* virtual inheritance, the public constructor
HiddenSealBaseClass() should have become private after Sealed
derivation, and thus inaccessible to SomeClass.


--
Ioannis Vranos

http://www23.brinkster.com/noicys

解决方案

"Ioannis Vranos" <iv*@remove.this.grad.com> wrote...

May someone explain why does this compile?
class HiddenSealBaseClass
{
public:
HiddenSealBaseClass() { }
};

class Sealed: virtual HiddenSealBaseClass
{};

class SomeClass: Sealed
{
// Whatever
};


int main()
{
SomeClass obj;
}
Since we have *private* virtual inheritance, the public constructor
HiddenSealBaseClass() should have become private after Sealed derivation,
and thus inaccessible to SomeClass.



That''s utter nonsense.

class A {
public:
void foo();
};

class B : A { // private inheritance
};

int main() {
A a;
a.foo(); // according to you it shouldn''t compile because
// somebody else inherits from A privately
}

Victor


* Victor Bazarov:

"Ioannis Vranos" <iv*@remove.this.grad.com> wrote...

May someone explain why does this compile?
class HiddenSealBaseClass
{
public:
HiddenSealBaseClass() { }
};

class Sealed: virtual HiddenSealBaseClass
{};

class SomeClass: Sealed
{
// Whatever
};


int main()
{
SomeClass obj;
}
Since we have *private* virtual inheritance, the public constructor
HiddenSealBaseClass() should have become private after Sealed derivation,
and thus inaccessible to SomeClass.



That''s utter nonsense.

class A {
public:
void foo();
};

class B : A { // private inheritance
};

int main() {
A a;
a.foo(); // according to you it shouldn''t compile because
// somebody else inherits from A privately
}



The problem is more subtle than that, Victor, in your example
somewhat analogous to

class C: public B
{
void victorLookHere(){ foo(); }
};

What might make the analogy break down is that this is about a
default constructor and virtual inheritance, not ordinary
inheritance and an ordinary member function or constructor.

The reason there is doubt is that at least three modern compilers
don''t complain.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


Victor Bazarov wrote:

That''s utter nonsense.

class A {
public:
void foo();
};

class B : A { // private inheritance
};

int main() {
A a;
a.foo(); // according to you it shouldn''t compile because
// somebody else inherits from A privately
}




Nope. You are using two classes while I am using three. Making your
example more equivalent:

class A {
public:
void foo();
};

class B : A {
};
class C: B {};

int main() {
C a;
a.foo();
}
This does not compile.

or even more equivalent:

class A {
public:
void foo();
};

class B : virtual A {
};
class C: B {};

int main() {
C a;
a.foo();
}

The above does not compile too, however it does compile if we use a
public A() constructor.


--
Ioannis Vranos

http://www23.brinkster.com/noicys


这篇关于我错过了私人继承的东西吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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