Spoiler-Alert: I forgot the * in the header file, it compiled but ignored my operator overload, and just printed the adress.
Leading to this confused mess of a question, i'm so sorry! Thank you all!
I've included a simple example of what i want to do. I have a pointer to a class, and i need to output it using <<, WITHOUT copying the value first.
I implemented an operator<< function that takes a reference as an argument. This has been suggested in several answers on StackOverflow as well -
However, i can't get the compiler to actually pick my overloaded operator function.
When i run the example below, it simply returns 0x6af59ffac0
(because the standard << implementation for pointers is used)
Granted, you could always dereference the pointer and write a pass-by-value operator<< function. But in my case this is not alllowed, as no other instance of the class should be created on the stack. (Basically trying to achive this without copying the class)
So, at this point i am probably going to simply avoid the << operator in that specific case, and write my own function. (see the workaround at the bottom) But i can't help being frustrated at this. Surely, there should be a way to hint to the compiler which version of the operator to use? (Similar to rust's Turbofish Operator ::<>)
Or is this legitimately impossible?
Example::
#include <iostream>
using namespace std;
class TEST {
public:
string name;
int id;
TEST(string nname, int iid) : name(nname), id(iid) {}
};
std::ostream& operator<<(std::ostream& out, TEST &m) {
return out << "Test Output, class has values: ID" << m.id << "; NAME: " << m.name;
}
int main() {
TEST var("tester", 22);
cout << &var; // Just prints the memory location
cout << (TEST*) &var; // Still just prints the memory location
return 0;
}
What i would do to avoid the language limitation (in case noone has an answer): (this works for me since a method doesn't have a copy of its class in its stack frame, just a pointer)
#include <iostream>
using namespace std;
class TEST {
public:
string name;
int id;
TEST(string nname, int iid) : name(nname), id(iid) {}
std::ostream& display(std::ostream& out) {
return out << "Test Output, class has values: ID" << this->id << "; NAME: " << this->name;
}
};
std::ostream& operator<<(std::ostream& out, TEST m) {
return m.display(out);
}
int main() {
TEST var("tester", 22);
var.display(cout);
return 0;
}