我希望c ++做得更好。 [英] I wish c++ did interfaces better.

查看:81
本文介绍了我希望c ++做得更好。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个接口(在c ++意义上,只不过是一个包含纯虚方法的结构)


struct Iv1 {

virtual method0()= 0; };


实现接口的类


类Iv1Impl:public Iv1 {

virtual method0( ){...}

}


创建版本2界面并不合理


struct Iv2 :Iv1 {

虚拟方法1()= 0;};


有人可能*尝试*来创建这样的实现类


class Iv2Impl:public Iv2,Iv1Impl {

virtual method1(){...}; //实现Iv2方法

};


这当然永远不会永远不会有效。为什么不?那我刚才介绍了另一种方法0()= 0?为什么它不能弄清楚它已经有一个我真的想让它使用的方法的实现?

解决方案

Chris Becke写道:


给定一个接口(在c ++意义上,只不过是一个包含纯虚方法的结构)


< ...>


这当然永远不会永远不会有效。为什么不呢?

那么我引入了另一种方法0()= 0?

为什么它不能弄清楚它已经有了实现
$ b这个方法的$ b我真的想让它使用吗?



struct Iv1 {

virtual void method0()= 0;

};


//实现接口的类


类Iv1Impl:public virtual Iv1 {

virtual void method0(){}

};


//创建版本2界面并不合理


struct Iv2:virtual Iv1 {

virtual void method1()= 0;};


//可能*尝试*来创建这样的实现类


class Iv2Impl:public Iv2,Iv1Impl {

virtual void method1(){} //实现Iv2方法

};


int main()

{

Iv2Impl x;

}


问候

Andy Little


你知道。

这样有效。

我确定我之前尝试过虚拟继承而且它不起作用。

也许它崩溃了,因为我试过的编译器(VC ++ 8)没有实现vir正确的基类,或者当尝试混合重写和继承的方法时,可能存在一些基本的错误。


" kwikius" < an ** @ servocomm.freeserve.co.ukwrote留言新闻:48 ******** @ mk-nntp-2.news.uk.tiscali.com ...


Chris Becke写道:


>给定一个接口(在c ++意义上,只不过是一个包含纯虚方法的结构)



< ...>


>这当然永远不会永远不会奏效。为什么不?

那么我引入了另一种方法0()= 0?

为什么它不能弄清楚它已经有了实现

for那个我真的想让它使用的方法?



struct Iv1 {

virtual void method0()= 0;

};


//实现接口的类


类Iv1Impl:public virtual Iv1 {

virtual void method0(){}

};


//创建版本2界面并不合理


struct Iv2:virtual Iv1 {

virtual void method1()= 0;};


//可能*尝试*来创建这样的实现类


class Iv2Impl:public Iv2,Iv1Impl {

virtual void method1(){} //实现Iv2方法

};


int main()

{

Iv2Impl x;

}


问候

Andy Little


Chris Becke写道:


你知道吗。

这样可行。

我确定我之前尝试过虚拟继承没用。

也许它崩溃了,因为我试过的编译器(VC ++ 8)没有正确实现虚拟基类,或者当一个人尝试混合时可能存在一些基本的错误重写和继承的方法。



嗯..我想我也知道Do​​h ..!感觉... ;-)


示例在VC8 FWIW中测试过,也许你的版本脾气暴躁

今天。我有时会这样做。编译器...爱他们讨厌,但不能做他们没有他们的b $ b。


问候

Andy Little


Given an interface (in the c++ sense, nothing more than a struct containing pure virtual methods)

struct Iv1 {
virtual method0()=0; };

And a class that implements the interface

class Iv1Impl : public Iv1 {
virtual method0(){...}
}

Its not unreasonable to create a version 2 interface

struct Iv2 : Iv1 {
virtual method1()=0;};

One might *try* to create an implementation class like this

class Iv2Impl : public Iv2, Iv1Impl {
virtual method1(){...}; // implement Iv2 method
};

That of course never never never works. Why not? So what that Ive introduced another method0()=0 ? Why can''t it figure out it already has an implementation for that method that I''d really like to allow it to use?

解决方案

Chris Becke wrote:

Given an interface (in the c++ sense, nothing more than a struct containing pure virtual methods)

<...>

That of course never never never works. Why not?
So what that Ive introduced another method0()=0 ?
Why can''t it figure out it already has an implementation
for that method that I''d really like to allow it to use?


struct Iv1 {
virtual void method0()=0;
};

//And a class that implements the interface

class Iv1Impl : public virtual Iv1 {
virtual void method0(){}
};

//Its not unreasonable to create a version 2 interface

struct Iv2 : virtual Iv1 {
virtual void method1()=0;};

//One might *try* to create an implementation class like this

class Iv2Impl : public Iv2, Iv1Impl {
virtual void method1(){} // implement Iv2 method
};

int main()
{
Iv2Impl x;
}

regards
Andy Little


You know.
That works.
Im sure I tried virtual inheritance before and it did not work.
Perhaps it collapsed because the compiler i tried with (VC++ 8) doesn''t implement virtual base classes correctly, or perhaps there is something fundamental that goes wrong when one tries to mix in overridden and inherited methods.

"kwikius" <an**@servocomm.freeserve.co.ukwrote in message news:48********@mk-nntp-2.news.uk.tiscali.com...

Chris Becke wrote:

>Given an interface (in the c++ sense, nothing more than a struct containing pure virtual methods)

<...>

>That of course never never never works. Why not?
So what that Ive introduced another method0()=0 ?
Why can''t it figure out it already has an implementation
for that method that I''d really like to allow it to use?


struct Iv1 {
virtual void method0()=0;
};

//And a class that implements the interface

class Iv1Impl : public virtual Iv1 {
virtual void method0(){}
};

//Its not unreasonable to create a version 2 interface

struct Iv2 : virtual Iv1 {
virtual void method1()=0;};

//One might *try* to create an implementation class like this

class Iv2Impl : public Iv2, Iv1Impl {
virtual void method1(){} // implement Iv2 method
};

int main()
{
Iv2Impl x;
}

regards
Andy Little


Chris Becke wrote:

You know.
That works.
Im sure I tried virtual inheritance before and it did not work.
Perhaps it collapsed because the compiler i tried with (VC++ 8) doesn''t implement virtual base classes correctly, or perhaps there is something fundamental that goes wrong when one tries to mix in overridden and inherited methods.

Well.. I think I too know the "Doh..!" feeling... ;-)

Example was tested in VC8 FWIW, maybe your version was being grumpy
today. Mine does that sometimes. Compilers... Love em hate but cant do
without em.

regards
Andy Little


这篇关于我希望c ++做得更好。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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