抽象类和指针 [英] Abstract classes and Pointers

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

问题描述

我有一个类

//我想要一个抽象类。

// i want an abstract class.

class Foo
{
    public:
    virtual void bar()=0;
};

//我想让这个抽象calss用于所有的程序:)享受多态性。 / p>

// i want this abstract calss to be used all over the program :) to enjoy polymorphism.

class EatFoo
{
public:
    vector<Foo> fooV; // not working
    vector<Foo *> fooPV;
};

我得到一个编译时错误,抽象类​​不能被实例化。

I get a compile time error that abstract class cannot be instantiated.

是它的真实,但我真的想要或我想学习:

Yes its true but i really want or i want to learn :

如何让其他程序员 to - 必须实现一些功能,我不想在我的程序中使用指针。 [我不知道为什么 ?但我有那种直觉。]

how to make other programmers "have to - have to" implement some function and i donot want to use pointers in my programs. [i donot know why ? but i have that gut feeling..]

有一些模式或东西可以帮助我。
对于java,它是所有引用,并且它是可行的。

Is there some pattern or something that can help me. With java it is all references and yup it is doable.

谢谢。

推荐答案

如果你想要容器项目的多态行为,你别无选择,只能使用指针。为了让你的生活尽可能简单,你应该使用智能指针,如 shared_ptr< Foo>

If you want polymorphic behaviour for container items, you have no choice but to use pointers. To make your life as easy as possible, you should use smart pointers, such as shared_ptr<Foo>.

这篇关于抽象类和指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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