• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
C++ question: dynamic_cast
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

C++ question: dynamic_cast


  • Subject: C++ question: dynamic_cast
  • From: Dmitry Markman <email@hidden>
  • Date: Sun, 13 Jul 2008 19:39:38 -0400

Hi, all

I accidentally encountered the following code (not exactly of course that is very simplified version)

class V {
    virtual int getID() = 0;
};

class A : public V {
public:
    virtual int getID(){return 0;}
    virtual int getIDA(){return 10;}
};

class B  : public V {
public:
    virtual int getID(){return 1;}
    virtual int getIDB(){return 11;}
};

A *a = new A();
B *b = new B();


void *getA() {
    return a;
}

void *getB() {
    return b;
}

int main (int argc, char * const argv[]) {
    A *sa = static_cast<A *>(getB());
    A *aa = dynamic_cast<A *>(sa);
//here aa ISN'T NULL!!!!!

    return 0;
}


in that fragment 
aa isn't null
I'd expect NULL, of course
NOTE: gdb clearly shows that aa (or sa)  is an instance of the B class (with set print object on and set print vtbl on)
(gdb) p aa
$2 = (B *) 0x106940
(gdb) p *aa
$3 = (B) {
  <V> = {
    _vptr$V = 0x2078
  }, <No data fields>}
(gdb) 




from other hand
the following code behaves just fine

int main (int argc, char * const argv[]) {
    V *v = static_cast<V *>(getB());
    A *aa = dynamic_cast<A *>(v);
//here aa IS NULL which is good

    return 0;
}



thanks in advance for any help with clarification

Dmitry Markman


 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: C++ question: dynamic_cast
      • From: Dmitry Markman <email@hidden>
    • Re: C++ question: dynamic_cast
      • From: Jim Fridlund <email@hidden>
  • Prev by Date: Xcode 3.1 builds fails for OS X Cocoa apps (copystrings)
  • Next by Date: Re: C++ question: dynamic_cast
  • Previous by thread: Re: Xcode 3.1 builds fails for OS X Cocoa apps (copystrings)
  • Next by thread: Re: C++ question: dynamic_cast
  • Index(es):
    • Date
    • Thread