2010-06-13 40 views
1

我希望得到函数的参数一样streamstringget函数的参数一样streamstring

// Declartion 富(性病:: stringstream的strString);

//使用 foo(“Hello”< <“world”);

怎么办?

+0

首先,你必须让你的奇怪的字符串连接'”(?)你好“<<”世界“'工作;为什么不用'std :: string(“Hello”)+ std :: string(“world”)''来代替它?然后你会声明一个函数'foo',它将一个'std :: string'作为它的参数。 – stakx 2010-06-13 14:38:45

回答

2

用于流插入的<<语法是通过重载左移运算符(这就是<<)的各种流类实现的。你可以重载向左移位运算符为你的类:

struct Example 
{ 
    Example& operator<<(int i) 
    { 
     /* use i */ 
     return *this; 
    } 
}; 

Example e; 
e << 42; // calls operator<< overload