PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > template and typedef
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
template and typedef

Réponse
 
LinkBack Outils de la discussion
Vieux 21/02/2008, 10h05   #1
Berardino la Torre
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut template and typedef

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
  Réponse avec citation
Vieux 21/02/2008, 14h43   #2
Victor Bazarov
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: template and typedef

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


  Réponse avec citation
Vieux 21/02/2008, 15h59   #3
Kira Yamato
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: template and typedef

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

  Réponse avec citation
Vieux 21/02/2008, 16h07   #4
Kira Yamato
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: template and typedef

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

  Réponse avec citation
Vieux 21/02/2008, 19h57   #5
Jeff Schwab
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: template and typedef

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() { }
};
  Réponse avec citation
Vieux 22/02/2008, 10h24   #6
James Kanze
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: template and typedef

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
  Réponse avec citation
Vieux 22/02/2008, 13h30   #7
gpderetta
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: template and typedef

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
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 12h03.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,25596 seconds with 15 queries