[완료]클래스 내에서 함수 포인터 변수에 값 설정시에...

임종규의 이미지

함수 포인터를 쓸일이 있어서 테스트를 하는 중입니다.

#include <stdio.h>
 
class test
{
public :
    typedef void(test::*wise)(int &a, int &b);
 
    int a, b;
    wise turn[2];
 
 
    test()
    {
        a = 0;
        b = 0;
        turn[0] = &test::swap;
        turn[1] = &test::swap2;
    }
 
    void run()
    {
        swap(a, b);
        print();
        swap2(a, b);
        print();
    }
 
    void print()
    {
        printf("%d, %d\n", a, b);
    }
 
    void swap(int &a, int &b)
    {
        int t;
        t = a;
        a = b;
        b = t;
    }
 
    void swap2(int &a, int &b)
    {
        int s,t;
        s = a;
        t = b;
        a = t;
        b = s;
    }
 
};
 
int main(void)
{
    test a;
 
    a.a = 2;
    a.b = 3;
 
    a.run();
 
    return 0;
}

위 소스중 test 클래스의 생성자에서 함수 포인터 변수에 값을 입력하는 부분에서요...

&test::swap 값이 정상적으로 turn[0] 에 입력되지 않습니다. 물론 turn[1] 역시 제대로 입력되지 않습니다.

&test::swap 값이 0x004115f0 인데 turn[0] 에는 0x004110f5 가 입력됩니다.
&test::swap2 값은 0x00411650 이고, turn[1] 에는 0x004110aa 가 입력되고요.

저희 경우엔 그렇게 나오더라구요.(주소값이니 사용자마다 다르실 겁니다...)

내부적으로 주어진 코드값에 따라 다른 함수를 호출하기 위해 함수포인터를 쓸려고 했는데, 테스트 부터 막히네요.(그냥 if 로 분기하라고 하시면 할말 없습니다만...)

어느 부분이 잘못되었는지 지적해주셨으면 좋겠습니다.(일반 외부에서의 함수포인터 설정은 정상적으로 됩니다만...)

부탁드립니다.

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

자문자답이 됐네요...

디버거상(vc2005)에는 다른 값으로 나와서 질문드린건데... 혹시나 해서 printf 로 찍어보니 동일하게 나오는군요...

(this->*turn[0])(a,b) 식으로 호출하면 된다는군요....

괜히 혼자 설래발쳣네요....

댓글 달기

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