Re: Simple inheritance-template question
On Apr 14, 1:19 pm, "crea" <n...@invalid.com> wrote:
"Goran" <goran.pu...@gmail.com> wrote in message
news:3ba65432-9aa1-4c9c-8326-59496b82f3e0@p3g2000vbv.googlegroups.com...
On Apr 14, 12:12 pm, "crea" <n...@invalid.com> wrote:
"I would simply make sure (through correct coding) that BB does use
DataHolder that has a pointer to BBData (not "plain" data) and then
use this:
class AA
{
Data& GetData() { assert(m_data.data); return *m_data.data; }
}
class BB : public AA
{
BBData& GetData() { return static_cast<BBData&>(AA::GetData(); }
};
Goran."
I was also thinking that first. But the problem is that inside AA I have
code like:
void AA::DoSomething()
{
...(a lot of code)
m_data.data.Add(new Data());
...(a lot of code)
}
So if I create a BB object and call this DoSomething , then it will creat=
e
Data-instance and not BBData. I know I could make DoSomething a virtual
fucntion , but DoSomething is *very* big function so I would rather now c=
opy
all of it to BB. Yes, I could then create a virtual function for only tha=
t
"Add"... :
void BB::AddData(..)
and then have virtual in AA:
virtual void AA::AddData(..). So now DoSomething becomes:
void AA::DoSomething()
{
...(a lot of code)
AddData(..);
...(a lot of code)
}
but AA has like 5 different this kind of fucntions. Shall I just create a
virtual function for all of them (5 of them)? I could obviously put them =
to
be private. But is there any easier way... this is doable, but needs to
override many functions.
(Not private, protected, you need to override).
Overrides seem^^^ to be trivial: create a different object type given
some parameters, so why not?
(^^^: I am guessing that after new Data() you actually put something
useful in that object).
Goran.