私有虚拟继承问题。 [英] Private Virtual inheritance problem.

查看:60
本文介绍了私有虚拟继承问题。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello All,


我参加OOP课程,在其中一个教程中,有一个冻结的

类示例,即一个类是不可能继承,

的例子如下:


struct ice__ {

ice(){};

};


class Frozen:private virtual ice__ {

Frozen(){}; //将调用ice __的默认c''tor,这没关系。

};


类违规:public Frozen {

违规(){}; //必须叫冰__''s c''tor这是不可能的。

};


据我所知,因为Frozen实际上继承了ice__,违反了

继承冻结必须召唤冰的'c''tor,这是不可能的,因为冰的'b $ b c''tor是冻结的私人部分。


然而,这个例子用ansi / pedantic标志在g ++

下编译并执行得很好。 cl(MS'的编译器)也吃这个。


任何线索?

谢谢,

Elad。

Hello All,

Im taking an OOP course, in one of the tutorials, there was a frozen
class example, i.e. a class which is impossible to inherit from, the
example was something like:

struct ice__ {
ice() {};
};

class Frozen : private virtual ice__ {
Frozen () {}; // Will call ice__''s default c''tor, which is OK.
};

class Violation : public Frozen {
Violation () {}; // Must call ice__''s c''tor which is impossible.
};

As I understand, since Frozen virtually inherits ice__, Violation which
inherits Frozen must call ice''s c''tor, which is impossible, as ice''s
c''tor is in the private part of Frozen.

nevertheless, this example compiles and executes just fine under g++
with ansi / pedantic flags. cl (MS''s compiler) eats this as well.

any clue ?
Thanks,
Elad.

推荐答案



Elad写道:

Elad wrote:
Hello All,
<我参加了一个OOP课程,在其中一个教程中,有一个冻结的
类示例,即一个不可能继承的类,
示例如下:

struct ice__ {
ice(){};
};

类Frozen:private virtual ice__ {
Frozen(){}; //会调用ice __的默认c''tor,这没关系。
};

类违规:public Frozen {
Violation(){}; //必须打电话给冰__'s''''这是不可能的。
};

据我了解,因为Frozen实际上继承了ice__,继承冻结的违规必须召唤ice'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''在g ++下很好用ansi / pedantic flags。 cl(MS的编译器)也吃了这个。

任何线索?

谢谢,
很高兴。
Hello All,

Im taking an OOP course, in one of the tutorials, there was a frozen
class example, i.e. a class which is impossible to inherit from, the
example was something like:

struct ice__ {
ice() {};
};

class Frozen : private virtual ice__ {
Frozen () {}; // Will call ice__''s default c''tor, which is OK.
};

class Violation : public Frozen {
Violation () {}; // Must call ice__''s c''tor which is impossible.
};

As I understand, since Frozen virtually inherits ice__, Violation which
inherits Frozen must call ice''s c''tor, which is impossible, as ice''s
c''tor is in the private part of Frozen.

nevertheless, this example compiles and executes just fine under g++
with ansi / pedantic flags. cl (MS''s compiler) eats this as well.

any clue ?
Thanks,
Elad.




在结构中,默认访问说明符在类中是公共的

默认说明符是私有的。


所以,Frozen会自编译以来调用公共部分的默认

构造函数。现在违规会自动调用Frozen的默认构造函数,因为Frozen的默认构造函数在私有范围内,因此会出现

错误。


所以你刚刚将Frozen作为一个不可导出的类。然而,我喜欢

在常见问题解答中提到的另一个步骤,通过

指定一个评论,在冻结之上说这个类应该是

final。



In structures the default access specifier is public while in classes
default specifier is private.

So, Frozen would compile properly since its calling the default
constructor of ice which is in public section. Now Violation would
automatically call default constructor of Frozen which would be an
error since Frozen''s default constructor is in private scope.

So you have just made Frozen as a non-derivable class. I however like
the other step as mentioned in the FAQs to make a class final by
specifying a comment on top of Frozen saying that this class should be
final.




Elad写道:

Elad wrote:
Hello All,
我参加了一个OOP课程,在其中一个教程中,有一个冷冻的
类示例,即一个不可能继承的类,
示例类似于: />
struct ice__ {
ice(){};
};

类Frozen:private virtual ice__ {
Frozen(){}; //会调用ice __的默认c''tor,这没关系。
};

类违规:public Frozen {
Violation(){}; //必须打电话给冰__'s''''这是不可能的。
};

据我了解,因为Frozen实际上继承了ice__,继承冻结的违规必须召唤ice'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''在g ++下很好用ansi / pedantic flags。 cl(MS的编译器)也吃了这个。

任何线索?

谢谢,
很高兴。
Hello All,

Im taking an OOP course, in one of the tutorials, there was a frozen
class example, i.e. a class which is impossible to inherit from, the
example was something like:

struct ice__ {
ice() {};
};

class Frozen : private virtual ice__ {
Frozen () {}; // Will call ice__''s default c''tor, which is OK.
};

class Violation : public Frozen {
Violation () {}; // Must call ice__''s c''tor which is impossible.
};

As I understand, since Frozen virtually inherits ice__, Violation which
inherits Frozen must call ice''s c''tor, which is impossible, as ice''s
c''tor is in the private part of Frozen.

nevertheless, this example compiles and executes just fine under g++
with ansi / pedantic flags. cl (MS''s compiler) eats this as well.

any clue ?
Thanks,
Elad.




我可以知道你正在使用的g ++编译器的版本。

i在gcc 3.2.2,borland 5.5.1和MS C ++编译器上试过它,我得到了
所有情况下
编译错误。



May i know the version of the g++ compiler you are using.
i tried it on gcc 3.2.2, borland 5.5.1 and MS C++ compiler , i got
compilation error in all the cases.


> struct ice__ {
> struct ice__ {
ice(){};
};

类Frozen:private virtual ice__ {
Frozen(){}; //会调用ice __的默认c''tor,这没关系。
};

类违规:public Frozen {
Violation(){}; //必须调用ice __''s c''tor这是不可能的。
};
ice() {};
};

class Frozen : private virtual ice__ {
Frozen () {}; // Will call ice__''s default c''tor, which is OK.
};

class Violation : public Frozen {
Violation () {}; // Must call ice__''s c''tor which is impossible.
};




上面的代码实际上使类Frozen FOTALLY无法使用,不仅如此

无法辨认。事实上,除非你没有在这里复制确切的代码,否则

编译器必须向你发出多个错误,例如错误地取代假定的冰___
ice和违规行为::违规行为打电话给

私人冻结::冻结。


也许你的确意味着以下几点:


struct ice__ {

ice __(){};

};


class Frozen:private virtual ice__ {

public://冻结是可用的

冻结(){};

};


class违规:public Frozen {};


现在最派生的类应该调用虚拟的

基类''构造函数。编译器现在不抱怨
的原因是,到目前为止编译器还没有一个线索是否

类违规将是派生最多的任何对象的类,

因为还没有创建单个对象。


要求输入错误,您需要制作违规对象。例如:


int main(){

冻结f; //确定

违规v; //错误

}


希望这会有所帮助。


问候,

Ben



The above code actually rendered class Frozen TOTALLY unusable, not only
underivable. In fact, unless you haven''t copy the exact code here the
compiler must issue you more than one errors, for example the typo of
ice replacing a supposed ice__ and Violation::Violation''s call to the
private Frozen::Frozen.

Perhaps you really meant the following:

struct ice__ {
ice__() {};
};

class Frozen : private virtual ice__ {
public: // Frozen is usable
Frozen () {};
};

class Violation : public Frozen {};

Now the most derived class is supposed to make a call to the virtual
base class''s constructor. The reason why the compiler doesn''t complain
now is that up to this point the compiler has not a single clue whether
class Violation is going to be the most derived class of any object,
because not a single object is created yet.

To ask for an error you need to make an object of Violation. For example:

int main(){
Frozen f; // Ok
Violation v; // Error
}

Hope this will help.

Regards,
Ben


这篇关于私有虚拟继承问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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