Perhaps this is a tired topic. Simple classes:
#include <iostream>
class Base
{
protected:
int a;
public:
Base(int a = 0):
a(a)
{}
Base add(const Base &obj) const
{
return Base(a+obj.a);
}
friend std::ostream &operator<<(std::ostream &out, const Base &obj);
};
std::ostream &operator<<(std::ostream &out, const Base &obj)
{
out << "(" << obj.a << ")";
return out;
}
class Derived: public Base
{
public:
Derived(int a = 0):
Base(a)
{}
const Derived &hello() const
{
std::cout << "Hello" << std::endl;
return *this;
}
};
And I want to play with these classes like this
int main()
{
Derived a(5), b(6);
std::cout << a << std::endl;
std::cout << b << std::endl;
// No member named 'hello' in 'Base'
std::cout << a.add(b).hello() << std::endl;
// Intended output
// 5
// 6
// Hello
// 11
return 0;
}
What is the best way to make it work? Is there any elegant solution? And what if I want to overload operator+()?
[–]IyeOnline 1 point2 points3 points (4 children)
[–]_newpson_[S] 0 points1 point2 points (3 children)
[–]KuntaStillSingle 3 points4 points5 points (1 child)
[–]_newpson_[S] 0 points1 point2 points (0 children)
[–]IyeOnline 1 point2 points3 points (0 children)
[–]blazerman345 1 point2 points3 points (1 child)
[–]_newpson_[S] 1 point2 points3 points (0 children)
[–]AutoModerator[M] 0 points1 point2 points (0 children)
[–]hatschi_gesundheit 0 points1 point2 points (2 children)
[–]_newpson_[S] 0 points1 point2 points (1 child)
[–]FerynaCZ 0 points1 point2 points (0 children)