成员函数到一个指针列表 [英] Member function to a list of pointer

查看:139
本文介绍了成员函数到一个指针列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

感谢您对以下内容发表评论。

  Class1 {debug(std :: ostream& ;){}}; 
int main(){
std :: vector< Class1 *> list1;
//一些工作要做
}

目标平台:




  • 平台(1):Win 7x64,VS2010

  • :Linux x32,g ++ 4.4



问:正确的方法是将std :: cout语句?

  std :: for_each(list1.begin(),
list1.end ,
afunction(& Class1 :: debug,std :: cout));

我以前在debug()函数中使用了std :: cout,但后来考虑给输出调试消息的灵活性。



编辑:更多信息:如果functor对象是要走的路,我应该如何实现函子来处理多个类类没有关系,除了相同的调试函数签名)?



编辑(2):使用std :: for_each,是可能销毁所有对象list1通过直接调用每个类的相应析构函数? (例如for_each(l.begin(),l.end(),Class ::〜Class1);



编辑(3)建议,我使语句为

  std :: for_each(l.begin(),
l.end ),
std :: bind2nd(std :: mem_fn(& Class1 :: debug),out));

它在Linux平台上编译并正确运行,但在VS2010上失败,Class1 :: debug的代码是

  void Class1 :: debug(const std :: ostream& out)
{
out<<some text<< someVar<<some text < std :: endl;
}

VS错误msg



错误C2678:binary'< <::没有操作符,它接受类型为const std :: ostream的左侧操作数(或没有可接受的转换) / p>

任何提示?



[Closed]
我现在实现了重载运算符<< for

解决方案

由于你使用的是g ++ 4.4你不能使用lambda表达式作为第一选择(以后的版本支持他们,MSVC也一样)。



所以你需要一个函数。函子是一个函数对象,它是一个实现 operator()的类(或结构体)。像这样:

  class Debug 
{
public:
Debug(ostream& os) :_os(os)
{}

void operator()(Class1 * instance)
{
//将打印指针,替换为用户代码
os<<实例<< endl;
}
private:
ostream& _os;
};

使用这样:

  Debug d(cout); 
std :: for_each(list1.begin(),list1.end(),d);


Thanks for giving comments to the following.

Class1 { debug(std::ostream&){} };
int main() {
  std::vector<Class1*> list1;
  // some work to do
}

Target Platform:

  • Platform(1): Win 7x64, VS2010
  • Platform(2): Linux x32, g++ 4.4

Q: What should be the correct way to pass "std::cout" to following statement?

std::for_each(list1.begin(), 
              list1.end(), 
              "afunction(&Class1::debug, std::cout)");

I previously used "std::cout" inside the debug() function, but later consider to give flexibility for the output of debug message.

Edit: More information: if functor objects is the way to go, how should I implements the functor to cope with multiple classes (those classes have no relationship except the same "debug" function signature)?

Edit(2): Using "std::for_each", is it possible to destroy all objects in list1 by invoking the corresponding destructor for each class directly? (e.g. for_each(l.begin(), l.end(), "Class::~Class1");

Edit(3): As per "pmr" suggested, I make the statement as

std::for_each(l.begin(), 
              l.end(), 
              std::bind2nd(std::mem_fn(&Class1::debug), out) );

It compiles and run correctly on linux platform, but failed on VS2010, the code for Class1::debug is

void Class1::debug(const std::ostream& out)
{ 
    out << "some text" << someVar << "some text" << std::endl; 
}

The VS error msg is

error C2678: binary '<<' : no operator found which takes a left-hand operand of type 'const std::ostream' (or there is no acceptable conversion)

Any cue?

[Closed] I now implemented the overloaded operator << for my classes, and the use of debug print function is closed. Thanks very much for all hints given.

解决方案

Since you are using g++ 4.4 you can't use lambda expressions which would be the first choice (later versions support them, MSVC does as well).

So you need a functor. A functor is a function object, that is a class (or struct) that implements operator(). Like this:

class Debug
{
public:
     Debug(ostream& os) : _os(os)
     { }

     void operator()(Class1* instance)
     {
          // will print the pointer, replace with user code
          os << instance << endl;
     }
private:
     ostream& _os;
};

Use like this:

 Debug d(cout);
 std::for_each(list1.begin(), list1.end(), d);

这篇关于成员函数到一个指针列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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