如何使用虚函数对基类进行抽象?

发布于 2024-12-03 15:55:44 字数 548 浏览 0 评论 0 原文

我有以下结构

class Base
{
public:
    Base(Type);
    virtual render
}

class A
{
public:
    Base(Type == A);
    void render()
}

class B
{
public:
    Base(Type == B);
    void render()
}

void client_function()
{
    Base baseObject(A);
    //Base is an instance of class A
    baseObject.render()//runs class A render
}

据我所知,上述代码中的某些内容不是 C++,它们与 Haskell 中发现的模式匹配密切相关,但这是我能找到的最好的方式来说明我的问题,而无需已经知道答案了;)

在写作中,我希望客户端能够创建一个基础对象并将对象的类型作为参数传递,返回对象的正确规范,并且客户端不需要关心它,只需知道运行渲染将运行正确的特定渲染。

如果我不清楚,请随时提问:)

I have the following structure

class Base
{
public:
    Base(Type);
    virtual render
}

class A
{
public:
    Base(Type == A);
    void render()
}

class B
{
public:
    Base(Type == B);
    void render()
}

void client_function()
{
    Base baseObject(A);
    //Base is an instance of class A
    baseObject.render()//runs class A render
}

There are things in the above code that are not c++ as far as I am aware, they are closely related to pattern matching found in Haskell for example, but this is the best way I could find to illustrate my question without already knowing the answer ;)

In writing I want the client to be able to create a base object and pass the type of object as an argument, the correct specification of the object is returned and the client need not care less about it, just knows that running render will run the correct specific render.

Please feel free to ask questions if I have been unclear :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

疑心病 2024-12-10 15:55:44

您需要运行时多态性。构造函数没有太多重要的部分。您必须将 Base 继承到 AB 中。例如:

class Base
{
public:
  virtual void render ();  // <--- declare 'virtual'
  virtual ~Base();  // <--- very much needed to avoid undefined behavior
};
class A : public Base //<---- inheritance
{
public:
  void render();  // <--- this also becomes 'virtual'
};
...

现在您可以根据您的要求使用。

Base *baseObject = new A();  // <----- need to use pointer (or reference)
(*baseObject).render();  // <--- other way to write: baseObject->render();
delete baseObject;

You need run-time polymorphism. There is not much important part of constructor. You have to inherit the Base into A and B. For example:

class Base
{
public:
  virtual void render ();  // <--- declare 'virtual'
  virtual ~Base();  // <--- very much needed to avoid undefined behavior
};
class A : public Base //<---- inheritance
{
public:
  void render();  // <--- this also becomes 'virtual'
};
...

Now you can use as per your requirement.

Base *baseObject = new A();  // <----- need to use pointer (or reference)
(*baseObject).render();  // <--- other way to write: baseObject->render();
delete baseObject;
不离久伴 2024-12-10 15:55:44

我不确定我是否理解你的问题。在 C++ 中,您无法在运行时选择基类,但您当然可以让基类依赖于派生类。这是通过使用模板和所谓的奇怪的重复模板模式来完成的:

template <typename T> class Base {
public:
    virtual void render(T *) {}
};

class A : public Base<A>
{
public:
    void render(A * t) {}
};

class B : public Base<B>
{
public:
    void render(B * t) {}
};

void client_function() {
    A a1;
    A a2;
    a1.render(&a2); // calls A::render
    a1.Base<A>::render(&a2); // calls Base<A>::render
    Base<A> * ba = &a1;
    ba->render(&a2); // calls A::render
}

希望这能回答您的问题问题。

I'm not sure I understood your question. In C++ you cannot choose your base class at runtime, but you certainly can have your base class depend from the derived class. This is done by using templates and what is known as the Curiously Recurring Template Pattern:

template <typename T> class Base {
public:
    virtual void render(T *) {}
};

class A : public Base<A>
{
public:
    void render(A * t) {}
};

class B : public Base<B>
{
public:
    void render(B * t) {}
};

void client_function() {
    A a1;
    A a2;
    a1.render(&a2); // calls A::render
    a1.Base<A>::render(&a2); // calls Base<A>::render
    Base<A> * ba = &a1;
    ba->render(&a2); // calls A::render
}

Hope this answers your question.

向地狱狂奔 2024-12-10 15:55:44

您所要求的正是继承的用途:从专用于功能的类层次结构创建对象。

在您的示例中,除了语法问题之外,一切都会按您的预期工作,即方法 A::render 将被调用,即使您在编译时不知道该对象(声明为 < code>Base,确实是一个A。这就是虚拟继承的魔力。

What you ask for is exactly what inheritance is for: creating object from a class hierarchy that specializes a functionality.

In your example, apart from syntax problems, things will work as you expect, i.e. method A::render will be called, even if you don't know at compile time that object, declared as a Base, is indeed a A. That's virtual inheritance magicness.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文