Re: Inheritance question
keith@bytebrothers.co.uk wrote:
On 29 Jun, 12:24, Kai-Uwe Bux <jkherci...@gmx.net> wrote:
k...@bytebrothers.co.uk wrote:
What I mean is that I have a context struct in my base class, which
a (non-virtual) method twiddles with. What I would like is the
overload the context struct in the derived class, and have the base
class method twiddle with the derived class context struct.
If you see what I mean.
However, to me the excessive use of pointers indicates that this
might not be a good design. What is the underlying problem that you
are trying to solve? It appears that you might be running into the
dark corners of the language because you are headed the wrong way.
I'm more than prepared to believe that I have made a basic design up-
cock, and this group is the closest thing I have to a language mentor,
so please redirect my thinking if that is what's needed...
Let's see if I can put together a minimal example (this is pseudo-code
- untested):
class BASE
{
private:
struct base_ctx
{
...
} ctx_;
public:
void twiddle();
};
void BASE::twiddle()
{
memset(&ctx_, somevalue, sizeof(ctx)); // for example only
}
//--------------------------------
class DERIVED : public BASE
{
struct derived_ctx : base_ctx
{
... // additional contents, different size
} ctx_;
}
//--------------------------------
int main()
{
DERIVED Derived;
// What I want is for the following call to twiddle
// with the contents of the derived_ctx struct
// instead of the contents of the base_ctx struct
// without having to exactly duplicate the contents
// of the twiddle member function in the DERIVED
// class.
Derived.twiddle();
}
Are you looking for polymorphic behaviour without the use of any
polymorphic constructs (like virtual functions)? The main problem
is that your 'twiddle' function does not know that it's called
for a 'DERIVED' object. It can only know that it's called for
an object of type 'BASE', but to learn whether it's a stand-alone
object or a subobject of something else is beyond its abilities.
I can only imagine the use of templates in this case, and then still
you need to pass the object itself to the function:
#include <iostream>
struct Base {
struct Base_inner {
void foo() { std::cout << "Base_inner\n"; }
} inner;
template<class T> static void twiddle(T &t) {
t.inner.foo();
}
};
struct Derived : Base {
struct Derived_inner : Base_inner {
void foo() { std::cout << "Derived_inner\n"; }
} inner;
};
int main() {
Derived d;
d.twiddle(d); // see how 'd' needs to have 'd' passed to it?
}
There is no way in C++ to make *data* virtual. Only functions (i.e.
the behaviour) can be virtual.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask