7

我首先要说我知道只有非静态成员函数可以是虚拟的,但这就是我想要的:

  1. 定义接口的基类:因此我可以使用基类指针来访问函数。

  2. 出于内存管理目的(这是一个内存有限的嵌入式系统),我希望静态分配覆盖函数。我接受这样的结果,即使用静态函数,我如何操作函数中的数据会受到限制。

    我目前的想法是,我可以通过使其成为实际上是静态的函数的包装器来保持轻重载函数。

请不要告诉我我需要重新考虑我的设计。这就是我问这个问题的原因。如果您想告诉我最好使用 c 和回调,请指导我阅读一些阅读材料来解释使用面向对象方法的缺陷。是否存在满足我列举的要求的面向对象的设计模式?

4

4 回答 4

7

是否存在满足我列举的要求的面向对象的设计模式?

是的,普通的旧虚拟功能。您的愿望是“静态分配的压倒一切的功能”。虚拟功能静态分配的。也就是说,实现这些功能的代码只存在一次,并且只存在一次,并且在编译/链接时是固定的。根据您的链接器命令,它们很可能像任何其他函数一样存储在闪存中。

class I {
  public:
  virtual void doit() = 0;
  virtual void undoit() = 0;
};

class A : public I {
  public:
  virtual void doit () {
    // The code for this function is created statically and stored in the code segment
    std::cout << "hello, ";
  }
  virtual void undoit () {
    // ditto for this one
    std::cout << "HELLO, ";
  }
};

class B : public I {
  public:
  int i;
  virtual void doit() {
    // ditto for this one
    std::cout << "world\n";
  }
  virtual void undoit() {
    // yes, you got it.
    std::cout << "WORLD\n";
  }
};

int main () {
   B b; // So, what is stored inside b?
        // There are sizeof(int) bytes for "i",
        // There are probably sizeof(void*) bytes for the vtable pointer.
        // Note that the vtable pointer doesn't change size, regardless of how
        // many virtual methods there are.
        // sizeof(b) is probably 8 bytes or so.
}
于 2012-06-26T21:49:52.753 回答
4

For memory management purposes (this is an embedded system with limited ram) I want the overriding functions to be statically allocated.

All functions in C++ are always statically allocated. The only exception is if you manually download and utilize a JIT.

于 2012-06-26T22:04:14.043 回答
2

静态成员函数只是普通函数(如非成员函数),位于类的命名空间内。这意味着您可以将它们视为普通函数,以下解决方案应该这样做:

class Interface
{
  public:
    void (*function) ();
};

class Implementation: public Interface
{
  public:
    Implementation()
    {
      function = impl_function;
    }

  private:
    static void impl_function() 
    {
      // do something
    }
};

然后

Implementation a;
Interface* b = &a;
b->function(); // will do something...

The problem with this approach is that you would be doing almost what the compiler does for you when you use virtual member functions, just better (needs less code, is less error-prone, and the pointer to the implementation functions are shared). The main difference is that using virtual your function would receive the (invisible) this parameter when called, and you would be able to access the member variables.

Thus, I would recommend to you to simply not do this, and use ordinary virtual methods.

于 2012-06-26T21:53:44.870 回答
0

The overhead with virtual functions is two-fold: besides the code for the actual implementations (which resides in the code segment, just like any other function you write), there is the virtual function table, and there are pointers to that table. The virtual function table is present once for each derived class, and its size depends on the number of virtual functions. Every object must carry a pointer to its virtual function table.

My point is, the per-object overhead of virtual functions is the same no matter how many virtual functions you have, or how much code they contain. So the way you arrange your virtual functions should have little impact on your memory consumtion, once you have decided that you want some degree of polymorphism.

于 2012-06-26T22:06:09.213 回答