The Open Source Swiss Army Knife

/code/cPP/
/code/cPP/ + sub-categories
http://www.sirfsup.com/
web directory content
    
      

Not logged in
Chat Register Login
return to:  http:/www.sirfsup.com      /code   /cPP 
Permalink: casting.htm
Title: add
article options : please login   |  print view

casting

casting MEANING: Used in conversion between objects of different types

  1. links
  2. static casting
  3. dynamic casting
  4. reinterpret_cast
  5. const_cast

http://www.cplusplus.com/doc/tutorial/tut5-4.html

static casting

static_cast is used to cast a pointer of a base class to a derived class. This can always be implicitly performed.

explicitly casting a base class to its derived class:

class Base {};
class Derived: public Base {};
Base * a = new Base;
Derived * b = static_cast<Derived*>(a);

implicitly casting a base class to its derived class: int main()
      {
           const int N = 5;
           int i;
           Shape* sptrs[N];
     
      /*
      Pointer to vector of Shape* pointers. Pointers to classes
      derived from Shape can be assigned to Shape* pointers.
      */
          
           // initialize set of Shape object pointers
          
           sptrs[0] = new Line(0.1, 0.1, Co_blue, 0.4, 0.5);
           sptrs[1] = new Line(0.3, 0.2, Co_red, 0.9, 0.75);
           sptrs[2] = new Circle(0.5, 0.5, Co_green, 0.3);
           sptrs[3] = new Text(0.7, 0.4, Co_blue, "Howdy!");
           sptrs[4] = new Circle(0.3, 0.3, Co_red, 0.1);
          
      /*

dynamic casting

  1. dynamic_cast
    In the following, it is used to downcast from TemplateObject to ObjectContainer, where, ObjectContainer inherits from TemplateObject. And here, TemplateObject is downcast to a ObjectContainer, one of its children:

    void doDataObject(TemplateContainer *tmpl,TemplateObject *cur,TmplTag &tag,bool active,FILE *out) {
    ...
    try {
         TemplateObject *t=dynamic_cast<ObjectContainer *>(cur)->getVal(tag.args["name"]);
         if(t) {
    The function "getVal" is a member of the children of ObjectContainer.
    Now, here is another example where <dynamicCast> is used to downcast from a parent to its child, and then call a method on the child object:
    .....
    .....
         TemplateObject *t=dynamic_cast<ObjectContainer *>(cur)->getVal(tag.args["name"]);
         if(t) {
             if(t->getType()!=DataObject) throw ParserError("Cannot cast to DataObject",tag);
         } else { // skip it ..
             Parser *p=new Parser(tmpl,0,"DataObject",out);
             p->setActive(false);
             SET_GENERAL_HANDLERS(p);
             p->parse();
             delete p;
             return;
         }
        
         DataObject *d=dynamic_cast<DataObject *>(t);


    In that final line, a TemplateObject is downcast two stories, because as you know now, ObjectContainer inherits from TemplateObject, but furthermore DataObject inherits from ObjectContainer, so there it is: a two story downcast in action.

reinterpret_const

casts pointers between non-related classes

const_cast

(#55) poster : anonymous (owner)date: 2005-11-17

Wanna to learn reinterpret_const


Leave a Reply
Your Name:     anonymous
Your Email:
Website:  
Comments:

The author will be notified of your reply.
return to top