c++에서 자식 객체의 type알아내는 방법 있나요?

omando의 이미지

class A 
{
      .......
}
class B : public A
{
    CLock lock;
    get(int n=10 )
    {
         lock.tryget(this, n);
    }
}

class C : public A
{
    CLock lock;
    get(int n=10 )
    {
         lock.tryget(this, n);
    }
}

class CLock
{
    tryget(A a, int n)
    {
         [b]........(1)..........[/b]
    }
}

이와 같은 상속 관계가 존재 합니다.
tryget() 함수 내의 (1)에서 인자로 넘어온 A a의 실제 객체 type이 class B 인지 class C 인지 알아 낼수 있는 방법은 없을까요?

멀티 스레드 환경에서 mutex lock시도중에 약간의 문제가 있어
실제 어떤 스레드에서 호출할때 dead lock같은 문제가 발생하는지 알고자
printf 를 하고자 시도중에 이와 같은게 과연 가능할지 궁금해서리..

lsj0713의 이미지

원칙적으로는 dynamic_cast 로 해결이 가능해야 합니다만...

struct animal { virtual void ~animal(); }; 
struct dog : animal { void bark(); }; 
struct cat : animal { void mew(); }; 

void test(animal* a) 
{ 
    if (dog* d = dynamic_cast<dog*>(a)) d->bark(); else 
    if (cat* c = dynamic_cast<cat*>(a)) c->mew(); 
} 

묘하게도 제가 MinGW/gcc 위에서 시도했을 때는 세그먼트 폴트가 나면서 뻗더군요. 그땐 시간이 촉박할때라 그냥 포기하고서, 클래스마다 type을 따로 변수로 저장해서 해결했기 때문에, dynamic_cast가 불완전한 것인지 다른 문제 때문이었는지는 잘 모르겠습니다.

ps. 관련글을 하나 링크하겠습니다. 단순비교라면 typeid도 괜찮겠군요.
http://groups.google.com/groups?selm=3E7EA9A8.37790D88%40bawi.org

B00m의 이미지

이런식으로 하면 되겠네요

class A 
{ 
      ....... 
    virtual What() = 0;
} 
class B : public A 
{ 
    CLock lock; 
    get(int n=10 ) 
    { 
         lock.tryget(this, n); 
    } 
    virtual What() {
         std::cout << "class B\n";
    }
}

class C : public A 
{ 
    CLock lock; 
    get(int n=10 ) 
    { 
         lock.tryget(this, n); 
    }
    virtual What() {
         std::cout << "class C\n";
    }
} 

class CLock 
{ 
    tryget(A *a, int n) 
    { 
         a->What();
} 
} 

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.