Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ > Casting from ba...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 3 Topic 43123 of 47925
Post > Topic >>

Casting from base to derived class in base constructor

by pastbin@[EMAIL PROTECTED] Feb 6, 2008 at 02:59 AM

Dear members,

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

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
 




 3 Posts in Topic:
Casting from base to derived class in base constructor
pastbin@[EMAIL PROTECTED]  2008-02-06 02:59:56 
Re: Casting from base to derived class in base constructor
James Kanze <james.kan  2008-02-07 00:58:19 
Re: Casting from base to derived class in base constructor
pastbin@[EMAIL PROTECTED]  2008-02-07 06:41:06 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Mon Oct 6 17:55:24 CDT 2008.