visual studio 2010에서 클래스 함수포인터 만드는 법을 모르겠습니다.

lmk378의 이미지

다음과 같이 작성했습니다.
typedef struct
{
int a;
void (Animal::*func)(void);
}Entry

class Animal
{
public:
Entry *table;
void run();
void jump();
}

Animal::Animal()
{
Entry tmp[2] =
{
{1, run},
{2, jump}
}
table = tmp;
}

void Animal::run()
{
cout << "run" << endl;
}

void Animal::jump()
{
cout << "jump" << endl;
}

int main()
{
Animal ani;
ani.table[0].func();
return 0;
}

이런식의 프로그램을 작성하려 하는데 어디를 수정해야 할까요?

bacon의 이미지

그냥 구문오류만 고치면 이정도 되겠네요.

class Animal;
 
typedef struct
{
     int a;
     void (Animal::*xxx)(void);
} Entry;
 
class Animal
{
public:
     Animal ();
     Entry *table;
     void run();
     void jump();
};
 
Animal::Animal()
{
     Entry tmp[2] =
     {
          {1, &Animal::run},
          {2, &Animal::jump}
     };
     table = tmp;
}
 
void Animal::run()
{
     cout << "run" << endl;
}
 
void Animal::jump()
{
     cout << "jump" << endl;
}
 
int main()
{
     Animal ani;
     (ani.*(ani.table[0].xxx)) ();
     return 0;
}
lmk378의 이미지

아 추가로 혹시 클래스 객체로 호출하는 문법 말고
Animal 클래스 내부에서 함수포인터로 호출하는 방법 어떻게 되나요?

bacon의 이미지

main에 있던걸 동일하게 호출하려면,

(this->*table[0].xxx) (); 


(this->*(this->table[0].xxx)) ();

하면 되겠네요.

lmk378의 이미지

답변 감사합니다^^

lmk378의 이미지

답변 감사합니다. 많은 도움이 되었습니다.^^
약간 첨언을 하자면 위의 생성자코드에서 tmp를 static으로 선언 해주어야 합니다.

bacon의 이미지

table이 포인터인지 보질 못했네요. table이 고정적이라고 한다면 table을 포인터로 하지 말고 바로 static 배열로 선언하는것도 괜찮을것 같아요.

class Animal
{
....
    static Entry table[];
....
};
 
Entry Animal::table[] =
{
     {1, &Animal::run},
     {2, &Animal::jump}
};

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.