This is for a school assignment, and in the specifications, it says,
1 2 3 4 5
Your design also includes a helper operator, which supports your class:
const SuperHero& operator*(const SuperHero& first, const SuperHe-ro& second): a non-friend
that returns an unmodifiable reference to the winner of the battle between the SuperHeroes
after max_rounds rounds.
Granting friendship pierces encapsulation, but without the friend keyword, I do not know how to access the data members. I could use a query, yes, but again -- I need to use this helper function as per specifications.
But it's underlining operator* in red, saying there +1 overload. Also, data members of first and second are underlined in red (such as first.name and second.health), saying they are inaccessible.
Cheers
const SuperHero& operator*(const SuperHero& first, const SuperHero& second): a non-friend...
It looks like a trap set up by your teacher :)
When defined outside classes, overloaded operators are usually declared friend to allow easy access to their private properties, but that’s not mandatory. As coder777 said:
coder777 wrote:
You can always have getters for the members.
In this case, it seems your teacher is pushing you to write getters/setters to access your private properties (or to declare them public).
To use in a way like this: Edit: sorry, the following example is wrong, since you can't modify the objects passed by const reference. You definitely need to create a modifiable copy of them as you currently do in your code. Anyway, I hope I've been able to describe the general idea.
1 2 3 4 5
const SuperHero& SuperHero::operator*(const SuperHero& first, const SuperHero& second) {
for (int i = 0; i < max_rounds; ++i) {
while (first.isAlive() && second.isAlive()) {
first.setHealth(first.getHealth() - second.attackStrength() + first.defend());
// ...
I’m a bit bemused by the request of having that overloaded operator returning a const reference, while it’s usually required to return by copy.
I’ve searched inside C++ Programming Language and en.cppreference.com and I couldn’t find any distinctive characteristic.
The C++ Programming Language, 4th Edition - 18.2.1 Binary and Unary Operators
Stroustrup wrote:
A binary operator can be defined by either a non-static member function taking one argument or a nonmember function taking two arguments.
...
A unary operator, whether prefix or postfix, can be defined by either a non-static member function taking no arguments or a nonmember function taking one argument.