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++输出运算符(<我们怎样才能做到这一点?我需要新的类(class)来总结一下吗?任何输入都是有帮助的。谢谢
pseudocode:
operator << () { // operator << is not declared completely
char abc[30];
myvals var1 = GetMyVald();
GetStringFromMyVals(var1, abc, 30, 30);
// print the string here.
}
最佳答案
该运算符的签名如下:
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;
}