Why can't I declare a friend function as const
?
//Types.h
#pragma once
#include <string>
#include <ostream>
class Player
{
public:
//constructors
Player();
Player(const std::string&, unsigned short);
//operator overload
friend std::ostream& operator<<(std::ostream&, const Player&);
// (I can't declare it as const)
//getter
const std::string& get_id() const;
private:
std::string id;
unsigned short lvl;
};
//Types.cpp
#include "Types.h"
#include <iostream>
#include <iomanip>
/*other definitions*/
std::ostream& operator<<(std::ostream& out, const Player& print)
{
out << "Player: " << std::setw(6) << print.id << " | " << "Level: " << print.lvl;
return out;
}
I mean, if I want to call operator<<
on a constant variable or in a constant function, I will get an error because operator<<
is not constant, even if it doesn't change anithing inside the class.