|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
Hi All,
is it possible to do something like this ? : template <typename Base> class Object : public Base::Interface { }; class IA{ virtual void print()=0; }; class A : public Object<A>{ public: typedef IA A::Interface; void print(){ } }; int main(){ return 0; } thx Bera |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
Berardino la Torre wrote:
> is it possible to do something like this ? : > > template <typename Base> > class Object : public Base::Interface { > }; > > class IA{ > virtual void print()=0; > }; > > class A : public Object<A>{ > public: > typedef IA A::Interface; > void print(){ > } > }; > > > int main(){ > > return 0; > } What is the intent here? I have hard time figuring out what you are trying to accomplish. Perhaps if you would show the _use_ for class A or class IA... V -- Please remove capital 'A's when replying by e-mail I do not respond to top-posted replies, please don't ask |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
On 2008-02-21 05:05:38 -0500, Berardino la Torre
<berardino.latorre@hotmail.com> said: > Hi All, > > is it possible to do something like this ? : > > template <typename Base> > class Object : public Base::Interface { > }; g++ seems to complain that you cannot inherit from an incomplete type Base. This is interesting. Just when you think you've figured out how template works, here comes another example showing you how much you don't know. > > class IA{ > virtual void print()=0; > }; > > class A : public Object<A>{ > public: > typedef IA A::Interface; > void print(){ > } > }; Seems like your goal is to have class A inherit from A::Interface. So, why not consider the following: class IA {}; class A : public IA { public: typedef IA Interface; }; > > > int main(){ > > return 0; > } -- // kira |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
On 2008-02-21 10:59:16 -0500, Kira Yamato <kirakun@earthlink.net> said:
> On 2008-02-21 05:05:38 -0500, Berardino la Torre > <berardino.latorre@hotmail.com> said: > >> Hi All, >> >> is it possible to do something like this ? : >> >> template <typename Base> >> class Object : public Base::Interface { >> }; > > g++ seems to complain that you cannot inherit from an incomplete type Base. > > This is interesting. Just when you think you've figured out how > template works, here comes another example showing you how much you > don't know. Well I suppose it makes sense that this should be an error. It's basically a circular definition that could lead to infinite regress. For example, template<class T> class A : public T {}; class B : public A<B> { int x; }; But this code expands to class B : public B { int x; }; Hence the infinite regress. > >> >> class IA{ >> virtual void print()=0; >> }; >> >> class A : public Object<A>{ >> public: >> typedef IA A::Interface; >> void print(){ >> } >> }; > > Seems like your goal is to have class A inherit from A::Interface. So, > why not consider the following: > > class IA {}; > > class A : public IA > { > public: > typedef IA Interface; > }; > >> >> >> int main(){ >> >> return 0; >> } -- // kira |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
Berardino la Torre wrote:
> Hi All, > > is it possible to do something like this ? : [Summarized for brevity:] template <typename Base> struct Object: Base::Interface { }; struct IA { virtual void print() =0; }; struct A: Object<A> { typedef IA A::Interface; void print() { } }; You're trying to derive a class (indirectly) from a type defined within that class. I don't know of any direct way to do that. You can instead move the type definition outside the class itself, e.g. into a traits class: template<typename T> struct Interface; template <typename Base> struct Object: Interface<Base>::Type { }; struct IA { virtual void print() =0; }; struct A; template<> struct Interface<A> { typedef IA Type; }; struct A: Object<A> { void print() { } }; |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
On Feb 21, 8:57 pm, Jeff Schwab <j...@schwabcenter.com> wrote:
> Berardino la Torre wrote: > > is it possible to do something like this ? : [Berardino's original post isn't showing up here, but...] > [Summarized for brevity:] > template <typename Base> > struct Object: Base::Interface { }; This line isn't legal C++. What it almost certainly should be is: struct Object : typename Base::Interface {} ; > struct IA { virtual void print() =0; }; > struct A: Object<A> { > typedef IA A::Interface; > void print() { } > }; > You're trying to derive a class (indirectly) from a type > defined within that class. Which is impossible, templates or not. -- James Kanze (GABI Software) email:james.kanze@gmail.com Conseils en informatique orientée objet/ Beratung in objektorientierter Datenverarbeitung 9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34 |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
On Feb 22, 11:24 am, James Kanze <james.ka...@gmail.com> wrote:
> On Feb 21, 8:57 pm, Jeff Schwab <j...@schwabcenter.com> wrote: > > > Berardino la Torre wrote: > > > is it possible to do something like this ? : > > [Berardino's original post isn't showing up here, but...] > > > [Summarized for brevity:] > > template <typename Base> > > struct Object: Base::Interface { }; > > This line isn't legal C++. This is actually legal and correct. In that context you can only have a typename, so you do not need to add the 'typename' keyword. There is no ambiguity. > What it almost certainly should be > is: > > struct Object : typename Base::Interface {} ; And this is not. Both gcc and comeau online complain that typename is not allowed in this context. As if the rules weren't complicated enough... .IIRC the next standard will relax the rule and allow (but not require) a typename there. HTH, -- gpd |
|
![]() |
| Outils de la discussion | |
|
|