On Aug 3, 4:48 am, Greg Herlihy <gre...@mac.com> wrote:
Declare classes A and B like so:
class A
{
public:
virtual A& mem() { return *this; }
virtual ~A() {} // always a good idea
};
class B : public A
{
public:
virtual B& mem() { return *this; }
};
Now, calling mem() with an A or B object will return a reference to an
object of the same type as the object itself:
The problem can be solved this way also:
class B : public A
{
public:
B & memA() { A::memA(); return *this; }
void memB() { /*...*/ }};
Both of your and my solution works for the intended use case:
B().memA().memB()
The key point what I wanted to emphasize:
"
But I consider this again a code bloat, especially when there are lot
of base class member functions which I have to "repeat" this way just
to achieve my goal.
So the problem I have here is that when I want to do the _same_ in the
member (see the function body of my B::memA) it should be possible to
do it automatically, without any code written in the derived classes.
To differentiate it from the current behaviour, we need some proper
(new) function declaration syntax. I wanted this as the main topic of
the thread, sorry if I could not describe it clearly.
[ comp.lang.c++.moderated. First time posters: Do this! ]