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 > Casting from base to derived class in base constructor
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Casting from base to derived class in base constructor

Réponse
 
LinkBack Outils de la discussion
Vieux 06/02/2008, 10h59   #1
pastbin@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Casting from base to derived class in base constructor

Dear members,

The following code compiles and runs fine on MSVC++ 2005 Express
edition.
Warning level at maximum and nothing wrong is reported.

This is a minimal code I wrote in order to test something for my
personal needs.
I'm not sure if the code is "legal" because I'm downcasting in a
constructor of a base class
to call a member of another base class of a different branch in the
hierarchy.

//------------
#include <iostream>

template<typename Derived>
struct aspect
{
aspect() { // constructor
Derived* derived = static_cast<Derived*>(this); // downcast
derived->handler_member(); // call a member of a base class of
derived
}
};

template<typename Derived>
struct handler
{
void handler_member() {
std::cout << "handler";
}
};

template<typename Derived, template<class> class Handler>
struct win : public Handler<Derived>
{
};

struct my_win : public win<my_win, handler>
, public aspect<my_win>
{
void dummy() {
std::cout << "ok";
}
};

int main() {
my_win w; // print "handler" as expected
w.dummy(); // print "ok" as expected
return 0;
}
//------------

Isn't this code somewhat esoteric ?

Thank you

mark
  Réponse avec citation
Vieux 07/02/2008, 08h58   #2
James Kanze
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Casting from base to derived class in base constructor

On Feb 6, 11:59 am, past...@gmail.com wrote:

> The following code compiles and runs fine on MSVC++ 2005
> Express edition. Warning level at maximum and nothing wrong
> is reported.


> This is a minimal code I wrote in order to test something for
> my personal needs. I'm not sure if the code is "legal"
> because I'm downcasting in a constructor of a base class to
> call a member of another base class of a different branch in
> the hierarchy.


> //------------
> #include <iostream>


> template<typename Derived>
> struct aspect
> {
> aspect() { // constructor
> Derived* derived = static_cast<Derived*>(this); // downcast
> derived->handler_member(); // call a member of a base class of derived


This is undefined behavior. The cast itself is undefined
behavior, to begin with (but I can't really imagine an
implementation where it would fail). The call to the function
is undefined behavior, and may in fact cause problems in all but
the simplest cases.

Don't do it.

> }
> };


> template<typename Derived>
> struct handler
> {
> void handler_member() {
> std::cout << "handler";
> }
> };


> template<typename Derived, template<class> class Handler>
> struct win : public Handler<Derived>
> {
> };
>
> struct my_win : public win<my_win, handler>
> , public aspect<my_win>
> {
> void dummy() {
> std::cout << "ok";
> }
> };


> int main() {
> my_win w; // print "handler" as expected
> w.dummy(); // print "ok" as expected
> return 0;}


> //------------


> Isn't this code somewhat esoteric ?


More than esoteric, it's undefined behavior, and even if it
happens to work, it is fragile and unmaintainable.

Please explain what you are trying to do. There's likely a
clean solution for it.

--
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 07/02/2008, 14h41   #3
pastbin@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Casting from base to derived class in base constructor

On Feb 7, 9:58 am, James Kanze <james.ka...@gmail.com> wrote:
>
> More than esoteric, it's undefined behavior, and even if it
> happens to work, it is fragile and unmaintainable.
>
> Please explain what you are trying to do. There's likely a
> clean solution for it.
>


Thank you for the reply.
The code:

Derived* derived = static_cast<Derived*>(this);

is undefined behavior because we are in a base constructor ?
Isn't it ?

What I'm trying to do is close to "delegate to a sister class":
http://www.parashift.com/c++-faq-lit...html#faq-25.10
But at construction time if possible..

Using templates I was in hope of avoiding virtual functions.

Thank you

mark
  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 19h15.


Édité par : vBulletin® version 3.7.3
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 ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,10650 seconds with 11 queries