【问题标题】:c++: Passing objects to functionsc ++:将对象传递给函数
【发布时间】:2015-08-30 14:06:59
【问题描述】:

我正在查看一个遇到问题的代码,并且能够破解这段代码:

#include <iostream>
#include <stdint.h>
#include <unistd.h>
#include <errno.h>
#include <vector>
#include <sys/types.h>

using namespace std;

class abc {
  public:
    abc(int x,int y) {
      cout << "x:" << x << endl;
      cout << "y:" << y << endl;
    }

    virtual ~abc() {}

    enum example {
      a = 1,
      b = 2,
      c = 3,
      d = 4
    };
};

template<typename T>
class xyz {
  public:
    void some_func(abc *a) {
      cout<<"some_func called"<<endl;
    }
};

int main() {}

我要调用函数

some_func()

来自main()。我该怎么做。有人可以帮我解决这个问题吗?

【问题讨论】:

  • 这个问题不是关于“将对象传递给函数”

标签: c++ templates object


【解决方案1】:

您需要创建一个模板类 xyz 的一些特化的对象和一个 abc 类型的对象。

例如

int main()
{
   abc a( 10, 20 );

   xyz<int> x;

   x.some_func( &a );
}

【讨论】:

  • 谢谢弗拉德这真的很有帮助
猜你喜欢
  • 1970-01-01
  • 2015-02-01
  • 1970-01-01
  • 2013-12-26
  • 1970-01-01
  • 2013-09-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多