开发者

Class member function pointers in C++

开发者 https://www.devze.com 2023-03-06 20:01 出处:网络
I want to call a member function of another class on an object, but I cant seem to figure out how this works. As example code on how it should work:

I want to call a member function of another class on an object, but I cant seem to figure out how this works. As example code on how it should work:

Class A {
  void somefunction(int x);
}

Class B : A {
  void someotherfunction(int x);
}

Class C {
  void x() {
      callY(&ofthefunction); 
}   //here you call the function, you dont have an object yet,    and you don't know the argument yet, this will be found in function callY

  void Y(*thefunction) {
       find int x;
       if(something)
             A a = find a;开发者_如何学运维
             a->thefunction(x);
       else 
             B b = find b;
             b->thefunction(x);
}
}

I hope this makes sence, It is also possible to split this in 2 methods, Y1 and Y2, but seeing as 90% of the code is the same (finding things in a XML file), only the object and argument where to save it is different, i'd like to do this


You can use something known as a virtual function. By the way, your syntax is hideous, it's class not Class, you need braces for your conditionals, and a judicious application of public, some extra semicolons, etc. It would be appreciated if you would go near a compiler before coming here, y'know.

class A {
public:
  virtual void somefunction(int x);
};

class B : public A {
public:
  virtual void somefunction(int x);
};

void func(A& a) {
    int x = 0;
    // Do something to find x
    a.somefunction(x); 
    // calls A::somefunction if this refers to an A
    // or B::somefunction if it's a B
}
int main() {
    A a;
    func(a); // calls A::somefunction
    B b;
    func(b); // calls B::somefunction
}


What you want to do can be done, although I woudn't solve it this way:

class A {
public:
    virtual int doit(int x) { return x+1; }
};

class B : public A {
public:
    int doit2(int x) { return x*3; }
    int doit(int x) { return x*2; }
};

int foo(int (A::*func)(int), int x, bool usea) {
    if (usea) {
        A a;
        return (a.*func)(x);
    } else {
        B b;
        return (b.*func)(x);
    }
}

int main() {
    int (A::*bla)(int) = &A::doit;
    foo(bla, 3, true);
    foo(bla, 3, false);

}

However, for this to work, the following has to be satisfied:

  1. You must use function pointers of the base class (e.g. int (A::*bla)(int)), otherwise you won't be able to call it on that base class (e.g. int (B::*bla)(int) can only be used on B instances, not on A instances, even if the method is already defined in A).
  2. The methods must have the same names as in the base class
  3. To use overriding (e.g. different impl in derived class), you have to use virtual functions.

But I would rather rethink your design...


No, that won't work at all. A pointer to a member of A will always point to that function, even when it's called on B because B inherits from A.

You need to use virtual functions. I see DeadMG has beaten me to it.

0

精彩评论

暂无评论...
验证码 换一张
取 消