struct myVals {
int val1;
int val2;
};
I have static functions
static myVals GetMyVals(void)
{
// Do some calcaulation.
myVals val;
val.val1 = < calculatoin done in previous value is assigned here>;
val.val2 = < calculatoin done in previous value is assigned here>;
return val;
}
bool static GetStringFromMyVals( const myVals& val, char* pBuffer, int sizeOfBuffer, int count)
{
// Do some calcuation.
char cVal[25];
// use some calucations and logic to convert val to string and store to cVal;
strncpy(pBuffer, cVal, count);
return true;
}
我的要求是我应该按顺序调用上面两个函数,然后使用C ++输出运算符(&lt;&lt;)打印“myvals”字符串。 我们怎样才能做到这一点?我是否需要新的课程来包装它。任何输入都有帮助。感谢
pseudocode:
operator << () { // operator << is not declared completely
char abc[30];
myvals var1 = GetMyVald();
GetStringFromMyVals(var1, abc, 30, 30);
// print the string here.
}
答案 0 :(得分:5)
此运算符的签名如下:
std::ostream & operator<<(std::ostream & stream, const myVals & item);
实现可能如下所示:
std::ostream & operator<<(std::ostream & stream, const myVals & item) {
stream << item.val1 << " - " << item.val2;
return stream;
}