简体   繁体   English

C ++,通用编程和虚函数。 我如何得到想要的东西?

[英]C++, generic programming and virtual functions. How do I get what I want?

This is what I would like to do using templates: 这是我想使用模板做的事情:

struct op1
{
   virtual void Method1() = 0;
}

...

struct opN
{
   virtual void MethodN() = 0;
}

struct test : op1, op2, op3, op4
{
    virtual void Method1(){/*do work1*/};
    virtual void Method2(){/*do work2*/};
    virtual void Method3(){/*do work3*/};
    virtual void Method4(){/*do work4*/};
}

I would like to have a class that simply derives from a template class that provides these method declarations while at the same time making them virtual. 我希望有一个仅从模板类派生的类,该模板类提供这些方法声明,同时使它们成为虚拟方法。 This is what I've managed to come up with: 这是我设法提出的:

#include <iostream>

template< size_t N >
struct ops : ops< N - 1 >
{
protected:
    virtual void DoStuff(){ std::cout<<N<<std::endl; };
public:
    template< size_t i >
    void Method()
    { if( i < N ) ops<i>::DoStuff(); } 
    //leaving out compile time asserts for brevity
};

template<>
struct ops<0>
{
};

struct test : ops<6>
{
};

int main( int argc, char ** argv )
{
  test obj;
  obj.Method<3>(); //prints 3
  return 0;
}

However, as you've probably guessed, I am unable to override any of the 6 methods I have inherited. 但是,您可能已经猜到了,我无法覆盖已继承的6种方法中的任何一种。 I'm obviously missing something here. 我显然在这里错过了一些东西。 What is my error? 我怎么了 No, this isn't homework. 不,这不是家庭作业。 This is curiosity. 这是好奇心。

Tested with GCC 4.3. 经过GCC 4.3测试。 Don't even know why I spent time on this :-/ 甚至不知道我为什么要花时间在这上:-/

#include <iostream>

template <std::size_t N>
struct mark
{ };

template <std::size_t N>
struct op : op <N - 1>
{
  virtual  void  do_method (const mark <N>&) = 0;
};

template <>
struct op <1>
{
  virtual  void  do_method (const mark <1>&) = 0;
};

struct test : op <2>
{
  template <std::size_t K>
  void
  method ()
  {  do_method (mark <K> ());  }

  virtual  void do_method (const mark <1>&)
  {  std::cout << "1\n";  }

  virtual  void do_method (const mark <2>&)
  {  std::cout << "2\n";  }
};

int
main ()
{
  test  x;

  x.method <1> ();
  x.method <2> ();
}

I don't know how to move the "prettifier" method() template function out of test . 我不知道如何将“ prettifier” method()模板函数移出test

template< size_t N >
struct ops : ops< N - 1 >

This codes an endless loop. 这编码了一个无限循环。 The recursion doesn't stop when N reaches 0. Add a specialization for the end case, immediately after the primary template: 当N达到0时,递归不会停止。在主模板之后,立即添加针对特殊情况的特殊化:

template<>
struct ops<0> {}

Also, what does this do? 另外,这是做什么的? Why not just call ops<i>::DoStuff() directly? 为什么不直接调用ops<i>::DoStuff()

template< size_t i >
void Method()
{ if( i < N ) ops<i>::DoStuff(); } 

To mimic your original desire: 模仿您的原始愿望:

#define MAKE_OPS(N) template<> struct Ops<N> : Ops<N-1> { virtual void Method##N() = 0; }

template<int N>
struct Ops;

template<>
struct Ops<0> { };

MAKE_OPS(1);
MAKE_OPS(2);
template<> struct Ops<3> : Ops<2> { virtual void Method3() { std::cout << "3" << std::endl; } };
MAKE_OPS(4);
MAKE_OPS(5);
MAKE_OPS(6);

struct Test : Ops<3> {
    virtual void Method1() { std::cout << 1 << std::endl; }
    virtual void Method2() { std::cout << 2 << std::endl; }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我需要通过函数传递什么样的参数。 另外,如何让它循环用户所需的次数? - What sort of arguments do I need to pass through my functions. Also how do I get it to loop as many times as the user wants? 我如何知道在 C++ 中使用哪些函数? - How do I know what functions to use in C++? 我在C ++,Generic Functions中的代码中做错了什么? - What am i doing wrong in this code in C++ , Generic Functions? 工厂:如何将临时智能指针传递给函数。 C ++ - Factories: how to pass temporary smart pointers to functions. C++ 存储递归函数的堆栈有多大。 我应该考虑哪些因素,如操作系统、编译器和硬件 - How big is the stack to store recursive functions. What factors like OS, compiler, and hardware should I consider 在C / C ++中,如何获取虚拟内存空间中的数据? - In C/C++, how do I get the data in a virtual memory space? c++ 如果我想将 ifstream::get() 拆分成单词,我该如何工作? - c++ how do i get ifstream::get() working if i want to split it into words? C++ 函数。 使用循环 - c++ functions. using loops C ++如何从虚拟类的模板化子类中获取数据? - C++ How do I get the data out of a templated child of a virtual class? C++ 如何获取指向类的虚拟 function 表的指针? - C++ How do i get a pointer to a class' virtual function table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM