C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Template-Klassen und Vererbung

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ (German)
View previous topic :: View next topic  
Author Message
Jw. Apri
Guest





PostPosted: Mon Apr 12, 2004 11:17 am    Post subject: Template-Klassen und Vererbung Reply with quote



Versuche mit Hilfe von Templates und Vererbung Code einzusparen. Ich
habe fünf Klassen: A, B, C, D, E. Die Klasse A ist eine Komposition aus
B, B ist eine Komposition aus C usw. E ist ein Teil von Komposition D, D
ist ein Teil von Komposition C usw.

Eine Komposition hat in etwa die Methoden einer std::list. Ein Teil hat
nur die Methode parent (E.parent() => D). Um nun nicht für A, B, C, D
die list-ähnlichen Methoden und für B, C, D, E die Methode parent zu
implementieren, hatte ich die Vorstellung von drei Template-Klassen:

template<class TEIL>
class Komposition
{
// std::list Methoden mit TEIL als Parameter/Rückgabe
};

template<class KOMPOSITION>
class Teil
{
// parent Methode mit KOMPOSITION als Rückgabe
};

template<class TEIL, KOMPOSITION>
class Komposition_Teil : public Komposition<TEIL>, Teil<KOMPOSITION>
{
// nix
};

Anschliessend könnte ich doch die Klassen A bis E von den
Template-Klassen vererben:

class A : public Komposition<B>
{
// A spezifische Methoden
};

class B : public Komposition_Teil<C, A>
{
// B spezifische Methoden
};

[...]

class E : public Teil<D>
{
// E spezifische Methoden
};

Ich glaube jedoch kaum, dass das jemals funktionieren wird, da die
Definition der Template-Parameter aus mehreren Gründen wohl nie aufgeht

Schön an einer solchen Ansatz wäre jedoch:
- gleichwertiger Code wie wenn A bis E ausprogrammiert, dank Templates.
- weiterer Verwaltungscode für A bis E könnte in den Template-Klassen
generisch untergebracht werden
- Typenprüfung zur Komplierzeit, dank Templates
- wirklich nur noch spezifische Methoden für A bis E implementieren

Kennt jemand andere Lösungsansätze oder gute Links zu diesem Problem?

mfg Jw. Apri

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
Back to top
Torsten Robitzki
Guest





PostPosted: Wed Apr 14, 2004 9:08 pm    Post subject: Re: Template-Klassen und Vererbung Reply with quote



Jw. Apri wrote:

Quote:
Versuche mit Hilfe von Templates und Vererbung Code einzusparen. Ich
habe fünf Klassen: A, B, C, D, E. Die Klasse A ist eine Komposition aus
B, B ist eine Komposition aus C usw. E ist ein Teil von Komposition D, D
ist ein Teil von Komposition C usw.

Eine Komposition hat in etwa die Methoden einer std::list. Ein Teil hat
nur die Methode parent (E.parent() => D). Um nun nicht für A, B, C, D
die list-ähnlichen Methoden und für B, C, D, E die Methode parent zu
implementieren, hatte ich die Vorstellung von drei Template-Klassen:

template<class TEIL
class Komposition
{
// std::list Methoden mit TEIL als Parameter/Rückgabe
};

template class Teil
{
// parent Methode mit KOMPOSITION als Rückgabe
};

template class Komposition_Teil : public Komposition {
// nix
};

Anschliessend könnte ich doch die Klassen A bis E von den
Template-Klassen vererben:

class A : public Komposition {
// A spezifische Methoden
};

class B : public Komposition_Teil {
// B spezifische Methoden
};

[...]

class E : public Teil {
// E spezifische Methoden
};

Ich glaube jedoch kaum, dass das jemals funktionieren wird, da die
Definition der Template-Parameter aus mehreren Gründen wohl nie aufgeht

Versuch es doch einfach mal in umgekehrter Reihenfolge und mit einen
paar zusätzlichen Deklarationen. In Deiner Beschreibung erkenne ich
keine zyklischen Abhängigkeiten.

class D;

class E : public Teil [...]
class B : public Komposition_Teil<C, A>

für die Rückwärtzreferenz Richtung parent braucht der compiler keine
vollständige Deklaration. Sollte ich mich vertun, kannst Du die
templates immernoch mit Zeigertypen instanziieren.

mfg Torsten

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ (German) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.