Talk About Network



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++ Moderated > Is this "upcast...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 4 Topic 9550 of 9593
Post > Topic >>

Is this "upcast" legal?

by Peter Simons <simons@[EMAIL PROTECTED] > Apr 26, 2008 at 09:43 AM

Hi,

the following code snippet compiles fine and behaves as expected, but I
wonder whether that behavior is guaranteed by the standard:

#include <iostream>

struct base
{
   struct iterator
   {
     iterator(base & b) : _base(b) { }

     base & _base;
   };
};

struct derived : public base
{
   struct iterator : public base::iterator
   {
     iterator(derived & d) : base::iterator(d) { }
   };

   void print() { std::cout << "derived::print()" << std::endl; }
};

int main(int, char**)
{
   derived d;
   derived::iterator i( d );
   static_cast<derived &>(i._base).print();
   return 0;
}

Why does that static_cast from `base' to `derived' succeed? Would this
code still work in case multiple inheritance?

Best regards,
Peter

-- 
      [ See http://www.gotw.ca/resources/clcm.htm
for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]




 4 Posts in Topic:
Is this "upcast" legal?
Peter Simons <simons@[  2008-04-26 09:43:21 
Re: Is this "upcast" legal?
alasham.said@[EMAIL PROTE  2008-04-26 16:28:49 
Re: Is this "upcast" legal?
Martin Bonner <martinf  2008-04-26 16:32:27 
Re: Is this "upcast" legal?
Sean Hunt <rideau3@[EM  2008-04-27 07:01:55 

Post A Reply:
  Go here to Signup

AddThis Feed Button


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

Contact
tan12V112 Wed May 14 6:46:36 CDT 2008.