From: Bogdan on
Hi,

I have 2 abstract structs as follows:

struct s1 {
virtual void f1() = 0;
virtual void f2() = 0;
};

struct s2 : public s1{
virtual void f3() = 0;
};

I also have 2 implementation classes:

class s1imp : pubic s1 {
virtual void f1() {}
virtual void f2() {}
};

class s2imp : public s2 {
virtual void f1() {}
virtual void f2() {}
virtual void f3() {}
};


Is there a way for class s2imp to derive from s1imp to take advantage of its
implementation of s1 (i.e. f1() and f2()) and provide the implementation of
f3() only?

Something like the following would be ideal (but it can't work for obvious
reasons):
class s2imp: public s1imp, public s2 {
virtual void f3();
}

Could somone please suggest a solution for the above?

Thanks,
Bogdan


From: Alex Blekhman on
"Bogdan" wrote:
> Hi,
>
> I have 2 abstract structs as follows:
>
> struct s1 {
> virtual void f1() = 0;
> virtual void f2() = 0;
> };
>
> struct s2 : public s1{
> virtual void f3() = 0;
> };
>
> I also have 2 implementation classes:
>
> class s1imp : pubic s1 {
> virtual void f1() {}
> virtual void f2() {}
> };
>
> class s2imp : public s2 {
> virtual void f1() {}
> virtual void f2() {}
> virtual void f3() {}
> };
>
>
> Is there a way for class s2imp to derive from s1imp to take
> advantage of its implementation of s1 (i.e. f1() and f2()) and
> provide the implementation of f3() only?

You can do this, however you will required to surpress one nasty
warning.

First, make inheritance virtual:

struct s2 : virtual public s1{
virtual void f3() = 0;
};

I also have 2 implementation classes:

struct s1imp : virtual pubic s1 {
virtual void f1() {}
virtual void f2() {}
};

Second, handle the C4250 warning:

#pragma warning(push)
#pragma warning(disable: 4250)

struct s2imp : public s1imp, public s2 {
virtual void f3() { PRINTME(); }
};

#pragma warning(pop)
#pragma

Third, use the code:

s2imp s2i;

s2i.f1(); // calls s1imp::f1
s2i.f2(); // calls s1imp::f2
s2i.f3(); // calls s2imp::f3


HTH
Alex


From: Igor Tandetnik on
"Bogdan" <bogdan(a)company.com> wrote in message
news:%23PhXpM70IHA.4164(a)TK2MSFTNGP03.phx.gbl
> I have 2 abstract structs as follows:
>
> struct s1 {
> virtual void f1() = 0;
> virtual void f2() = 0;
> };
>
> struct s2 : public s1{
> virtual void f3() = 0;
> };
>
> I also have 2 implementation classes:
>
> class s1imp : pubic s1 {
> virtual void f1() {}
> virtual void f2() {}
> };
>
> class s2imp : public s2 {
> virtual void f1() {}
> virtual void f2() {}
> virtual void f3() {}
> };
>
>
> Is there a way for class s2imp to derive from s1imp to take advantage
> of its implementation of s1 (i.e. f1() and f2()) and provide the
> implementation of f3() only?

template <typename Itf>
class s1imp_t : public Itf {
virtual void f1() {}
virtual void f2() {}
};

class s1imp : public s1imp_t<s1> {};
class s2imp : public s1imp_t<s2> {
virtual void f3() {}
};

--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925


 | 
Pages: 1
Prev: Child Window
Next: VC++ DLL Debug