C ++中的接口 [英] Interfaces in C++

查看:268
本文介绍了C ++中的接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我声明一个纯虚函数:


A类

{

virtual void myfunc()= 0;

}


我从A派出一个班级:


班级B:公共A

{

void anotherfunc();

}


编译此程序时,

编译器并没有抱怨没有方法myfunc()在B中实现了



有没有办法强制执行这样的编译器错误/警告?


问候


R4DIUM

解决方案

< blockquote> A.Gallus写道:


如果我声明一个纯虚函数:


class A

{

virtual void myfunc()= 0;

}



;


>

我从A派出一个班级:


班级B:公共A
{

无效a notherfunc();

}



;


>

当我编译这个程序时,

编译器没有抱怨有没有没有方法myfunc()

在B中实现。

有没有办法强制执行这样的编译器错误/警告?



是的。尝试实例化你的''B''类或者至少声明一个函数

按值返回''B'。


V
-

请在通过电子邮件回复时删除资金''A'

我没有回复最热门的回复,请不要不要问


Bingo,做了这个工作,谢谢!


" Victor Bazarov" < v。******** @ comAcast.netschrieb im Newsbeitrag

news:gc ********** @ news.datemas.de ...


A.Gallus写道:


>如果我宣布一个纯虚函数:

A级
{void void myfunc()= 0;
}



;


>>
我从A派出一个班级:

B班:公开A
{
void anotherfunc ();
}



;


>>
当我编译这个程序时,编译器并没有抱怨没有方法myfunc()< br实现了B.
有没有办法强制执行这样的编译器错误/警告?



是的。尝试实例化你的''B''类或者至少声明一个函数

按值返回''B'。


V
-

请在通过电子邮件回复时删除资金''A'

我没有回复最热门的回复,请不要不要问


On 6 Okt。,21:12,A.Gallus < u ... @ rz.uni-karlsruhe.dewrote:


Bingo,做了这个工作,thx!


> class A
{
* * virtual void myfunc()= 0 ;
};


> class B:public A
{



您应该考虑更喜欢

B级:公共虚拟A

以避免被咬伤:
http://www.parashift .com / c ++ - faq-lit .... html#faq-25.8

如果使用界面模式,很可能会发生这种情况。


Best,

- Maik


If I declare a function pure virtual:

class A
{
virtual void myfunc() = 0;
}

and I derive a class from A:

class B : public A
{
void anotherfunc();
}

when I compile this program,
the compiler doesn''t complain that there is no method myfunc() implemented
in B.
Is there a way to enforce such a compiler error/warning?

Regards

R4DIUM

解决方案

A.Gallus wrote:

If I declare a function pure virtual:

class A
{
virtual void myfunc() = 0;
}

;

>
and I derive a class from A:

class B : public A
{
void anotherfunc();
}

;

>
when I compile this program,
the compiler doesn''t complain that there is no method myfunc()
implemented in B.
Is there a way to enforce such a compiler error/warning?

Yes. Try to instantiate your ''B'' class or at least declare a function
that returns ''B'' by value.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


Bingo, that did the job, thx!


"Victor Bazarov" <v.********@comAcast.netschrieb im Newsbeitrag
news:gc**********@news.datemas.de...

A.Gallus wrote:

>If I declare a function pure virtual:

class A
{
virtual void myfunc() = 0;
}

;

>>
and I derive a class from A:

class B : public A
{
void anotherfunc();
}


;

>>
when I compile this program,
the compiler doesn''t complain that there is no method myfunc()
implemented in B.
Is there a way to enforce such a compiler error/warning?


Yes. Try to instantiate your ''B'' class or at least declare a function
that returns ''B'' by value.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


On 6 Okt., 21:12, "A.Gallus" <u...@rz.uni-karlsruhe.dewrote:

Bingo, that did the job, thx!

>class A
{
* *virtual void myfunc() = 0;
};

>class B : public A
{

You should consider to prefer
class B : public virtual A
to avoid being bitten by:
http://www.parashift.com/c++-faq-lit....html#faq-25.8
which will most likely happen if one uses the interface pattern.

Best,
-- Maik


这篇关于C ++中的接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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