你可以觉得像类成员函数:
ReturnType Function(TypeOfThisClass* this_, TypeOfParam param, ...)
( 您在std::thread
使用这种形式过于:(即把类PTR作为参数)
class MyClass { void Function() { ; }; }
....
MyClass myClass;
std::thread(&MyClass::Function, &myClass);
)
所以你的成员是:
class MyClass{
....
friend MyClass operator*(const MyClass& this_, float &other)
{
return MyClass(this_.Data * other);
};
/*or MyClass operator*(float &other)
{
return MyClass(this->Data * other);
};, which is the same, except you use it with reference not pointer)*/
friend MyClass operator*(float &other, const MyClass& this_)
{
return this_ * other; //you can use the overloaded op. since it is defined above (first arg is class)
};
};
,如果你想访问你的类protected
和private
数据(这应该是在OOP的情况下),你应该只使用friend
关键字。
是的,C++有免费的功能,我不会称之为解决方法。你的例子也是错误的。你不能将'0.4f'作为'float'来传递。 – chris 2014-11-22 15:45:52