链表问题 [英] probleme with linked list

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

问题描述

您好,我正在从事一个音频项目,我有3个类(一个用于播放mp3,一个用于播放wav的类和一个用于播放自动生成的soud的类)的问题是,在我的项目中,我必须能够选择许多这些声音并同时播放(可能会播放几种相同类型的声音),这种想法是创建链接链,但是否可以创建类的链接链?
感谢您的帮助

Hello im working on an audio project and i have 3 classes ( one for playing mp3 , classe for playing wav and classe for playing autogenerated soud) the problem is that in my project i must be able to choose many types of these thre sounds and play them at the same time ( maybé several sounds of same type ) the idea is creating linked chain but is it possible to create a linked chain of classes ?
thanks for help

推荐答案

最好的方法是让您的3个类成为GenericAudio类的子类:

The best way to do this would be to have your 3 classes be subclasses of a GenericAudio class:

class GenericAudio 
{ 
    virtual void play_audio() abstract;
}

class MP3Audio : public GenericAudio
{
    virtual void play_audio();
    ...
}

class WavAudio : public GenericAudio
{
    virtual void play_audio();
    ...
}

class SelfGenAudio : public GenericAudio
{
     virtual void play_audio();
     ...
}



然后,您的链表可以是GenericAudio的链表,您可以在每个对象上调用play_audio()而不管它是什么特定的子类.

这三个类共有的任何其他功能也应在GenericAudio中声明为虚函数,因此您只需编写其余所有代码即可使用GenericAudio对象.

如果出于某种原因您认为需要了解某个GenericAudio的子类型,则可以随时对其进行测试-但这可能意味着您需要向GenericAudio添加另一个虚函数,这样您就不必在意了.

这样,如果您发现以后需要添加其他音频类型,则只需添加GenericAudio的另一个子类,其余所有代码都可以使用.



Then your linked list can be a linked list of GenericAudio''s and you can call play_audio() on each object without caring what specific sub-class it is.

Any other functionality that is common to the three classes you should also declare as virtual in GenericAudio, so you just write all the rest of your code to use a GenericAudio object.

If for some reason you think you need to know what sub-type some GenericAudio is, you can always test for it -- but probably that means you need to add another virtual function to GenericAudio so that you don''t have to care.

Doing it that way, if you find you need to add another audio type later, you just add another sub-class of GenericAudio and all the rest of the code just works.


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

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