C++ij½ºÆÃ
FrontPage|FindPage|TitleIndex|RecentChanges|UserPreferences E D R S I M H RSS

±×µ¿¾È ¸ðÈ£Çß´ø(ÀúÈ¥ÀÚ ¸ðÈ£.. ¤Ì_¤Ì) C++ÀÇ °¢Á¾ castµé¿¡ ´ëÇؼ­ Á¤¸®Çغ¸¾Ò½À´Ï´Ù.

C++Àº C-style-castÀÇ ¹®Á¦Á¡À» º¸¿ÏÇϱâ À§ÇØ ´ÙÀ½°ú °°Àº 4°¡Áö cast¸¦ Áö¿øÇÕ´Ï´Ù.

static_cast<>() dynamic_cast<>() const_cast<>() reinterpret_cast<>()

<> ¾È¿¡ ŸÀÔÀÌ µé¾î°¡°í ij½ºÆà ÇÏ·Á´Â ŸÀÔÀº ¹Ýµå½Ã µÚ¿¡ °ýÈ£·Î ¹­¾îÁÖ¾î¾ß ÇÕ´Ï´Ù.

============================================================================ =========== static_cast ´Â ÄÄÆÄÀÏ Å¸ÀÓ¿¡ casting ŸÀÔÀ» ¾Ë ¼ö Àִ ŸÀÔµéÀ» casting ÇÒ ¶§ »ç¿ëÇÕ´Ï´Ù. dynamic_cast ´Â hierarchy °¡ ÀÖ´Â class µé »çÀ̸¦ ¿À°¥ ¶§(up, down casting ¸ðµÎ) »ç¿ëÇÕ´Ï´Ù. const_cast ´Â ŸÀÔÀÇ const ¼ºÀ» ¾ø¾Ù ¶§ »ç¿ëÇÕ´Ï´Ù. reinterpret_cast ´Â º° °ü°è ¾ø´Â ŸÀÔµé °£¿¡ castingÀ» ÇØ¾ß ÇÒ ¶§ ŸÀÔ¸¸ ¹Ù²ãÁִµ¥ »ç¿ëÇÕ´Ï´Ù. ============================================================================ ===========

C-style-cast == static_cast + reinterpret_cast Á¤µµ·Î º¸½Ã¸é µË´Ï´Ù.

dynamic_cast ´Â ¹Ýµå½Ã vtable(virtual function table)ÀÌ Á¸ÀçÇØ¾ß ÇÕ´Ï´Ù. dynamic_cast ´Â vtableÀ» Â÷·Ê·Î µÚÁö±â ¶§¹®¿¡ ½ÇÇà½Ã°£ÀÌ O(N) ÀÔ´Ï´Ù.

static_cast ´Â hierarchy°¡ Á¸ÀçÇÏ´Â class µé °£¿¡µµ upcastingÀº °¡´ÉÇÕ´Ï´Ù. (ÄÄÆÄÀÏ Å¸ÀÓ¿¡ ¾Ë ¼ö ÀÖÀ¸¹Ç·Î ´ç¿¬ÇÕ´Ï´Ù.) À̸¦ ¾ð±ÞÇÏ´Â °ÍÀº hierarchy°¡ Á¸ÀçÇÒ ¶§ ¹«Á¶°Ç dynamic_cast¸¦ »ç¿ëÇÏ´Â ¿ì¸¦ ¹üÇÏÁö ¾Ê±â À§ÇÔÀÔ´Ï´Ù. downcastingÀº ´ç¿¬È÷ dynamic_cast¸¦ »ç¿ëÇØ¾ß ÇÕ´Ï´Ù.

¾Æ·¡´Â ¿¹Á¦ ÇÁ·Î±×·¥ÀÔ´Ï´Ù. ½É½ÉÇÏ½Ç ¶§ Çѹø¾¿ µ¹·Áº¸½Ã±æ.. :)

============================================================================ ===========

#include <stdio.h>

class Root {
public:
virtual void rootx() = 0;
};
class A : public Root {
public:
virtual void fn() = 0;
};

class B : public Root {
public:
virtual void fx() = 0;
};

class Foo : public A, public B
{
    virtual void zz() { }
    void rootx() { }
    void fn() {
     }
     void fx() {
     }
};

int main()
{
    Foo *p = new Foo();
    B *z = (B *)p;
    Root *q = static_cast<Root *>(static_cast<B *>(p));
    Root *r = dynamic_cast<Root *>(dynamic_cast<B *>(p));
    Root *x = (Root *)(((B *)(p)));

    printf("foo = %p\n", p);
    printf("(A *) = %p\n", (A *)p);
    printf("(b *) = %p\n", (B *)p);
    printf("static_cast<A *>(Foo *) = %p\n", static_cast<A *>(p));
    printf("static_cast<B *>(Foo *) = %p\n", static_cast<B *>(p));

    void *v = static_cast<void *>(p);
    printf("static_cast<A *>(void *) = %p\n", static_cast<A *>(v));
    printf("static_cast<B *>(void *) = %p\n", static_cast<B *>(v));

    printf("dynamic_cast<A *>(Foo *) = %p\n", dynamic_cast<A *>(p));
    printf("dynamic_cast<B *>(Foo *) = %p\n", dynamic_cast<B *>(p));

    printf("reinterpret<A *>(Foo *) = %p\n", reinterpret_cast<A *>(p));
    printf("reinterpret<B *>(Foo *) = %p\n", reinterpret_cast<B *>(p));

    printf("q = %p\n", q);
    printf("r = %p\n", r);
    printf("x = %p\n", x);
    //printf("(Foo *) = %p\n", (Foo *)q);
    //printf("static<Foo *> = %p\n", static_cast<Foo *>(q));
    printf("dynamic_cast<Foo *>(Root *) = %p\n", dynamic_cast<Foo *>(q));
    printf("reinterpret_cast<Foo *>(Root *) = %p\n", reinterpret_cast<Foo
*>(q));

    printf("dynamic_cast<A *>(B *) = %p\n", dynamic_cast<A *>(z));
}

´Ù¸¥ ºÐ ÄÚ¸àÆ®

static_cast´Â compile time¿¡ ÁÖ¾îÁø typeÀ» ÅëÇØ compile time¿¡ °áÁ¤ÇÒ ¼ö ÀÖ´Â castingÀ» ¼öÇàÇÒ °æ¿ì »ç¿ëÇÏ°í,

¹Ý¸é, dynamic_cast´Â compile time¿¡ ÁÖ¾îÁø typeÀÌ ¾Æ´Ï¶ó runtime¿¡ casting´çÇÏ´Â objectÀÇ ½ÇÁ¦ type Á¤º¸ (vtable ÀÌ°ÚÁÒ)¸¦ ÀÌ¿ëÇؼ­ castingÀ» ¼öÇàÇÑ´Ù°í ¸»Çؾ߰ڽÀ´Ï´Ù. µû¶ó¼­, up/downcast´Â ¹°·Ð siblingÀ¸·Îµµ casting °¡´ÉÇÕ´Ï´Ù. dynamic_cast´Â ÁÖ·Î polymorphismÀÇ ¿ëµµ·Î ¾²ÀÔ´Ï´Ù.

reinterpret_cast´Â compilerÀÇ impl-dependentÇÑ castÀÌ°í µû¶ó¼­ non-portableÇϹǷÎ, °¡±ÞÀû ÀÚÁ¦ÇؾßÇÕ´Ï´Ù. ^^;

const_castµµ ¿ø·¡ constant°¡ ¾Æ´Ñ storage¸¦ °¡Áö´Â data¸¦ const_cast·Î »ç¿ëÇؼ­´Â ¾ÈµË´Ï´Ù.


dynamic_cast, reinterpret_cast, const_cast µîÀ» »ç¿ëÇϱâ Àü¿¡ cast¸¦ À¯¹ßÇÏ°Ô µÈ ¿øÀÎÀÌ ¹«¾ùÀÎÁö, design¿¡ ¹®Á¦°¡ ÀÖ´Â °ÍÀº ¾Æ´ÑÁö °ËÅäÇÏ´Â ½À°üÀÌ ÇÊ¿äÇÕ´Ï´Ù.

EditText|FindPage|DeletePage|LikePages| Valid XHTML 1.0! Valid CSS! powered by MoniWiki