Consider the following code:
class MyClass
{
template <typename Datatype>
friend MyClass& operator<<(MyClass& MyClassReference, Datatype SomeData);
// ...
};
template <typename Datatype>
MyClass& operator<<(MyClass& MyClassReference, Datatype SomeData)
{
// ...
}
How can I define operator<< inside the class, rather than as a friend function? Something like this:
class MyClass
{
// ...
public:
template <typename Datatype>
MyCLass& operator<<(MyClass& MyClassReference, Datatype SomeData)
{
// ...
}
};
The above code produces compilation errors because it accepts two arguments. Removing the MyClassReference argument fixes the errors, but I have code that relies on that argument. Is MyClassReference just the equivalent of *this?
You have
inside of the class. It is a method of the class
MyClass. Non-static methods have an implicit parameter called thethispointer. Thethispointer is a pointer to the object the method was called on. You do not need theMyClassReferenceparameter because thethispointer fulfills that purpose.Change that method declaration to
.