Pointer to function won't

This is a discussion on Pointer to function won't within the C++ Programming forums, part of the General Programming Boards category; Well that's just stupid isn't it? It's a derived class, so the base classes members are also the derived classes ...

  1. #16
    Budding Synth Programmer samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Trefforest
    Posts
    368
    Well that's just stupid isn't it?

    It's a derived class, so the base classes members are also the derived classes members. The classes are related somehow and the signatures are the same.

    Guess I'll just have to abandon the project.
    MSVC++ 6.0

  2. #17
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by samGwilliam View Post

    It's a derived class, so the base classes members are also the derived classes members.
    This sentence makes no sense whatsoever. Why would you think a viewPort is also a vSpec? (And for that matter, it doesn't work the other way around either: a vSpec isn't a viewPort -- it may be built on top of a viewPort, yes, but they are not actually the same thing.) If you mean that a vSpec contains all the same things as a viewPort, yes that's true, but the whole point of vSpec::aPlot is that it can access all the new stuff in vSpec and thus claiming that it should somehow be the same as a viewPort:: function is just silly. My understanding is that you think this should work, somehow:
    Code:
    class viewPort {
    //... stuff ...
    void aPlot(int, int, unsigned long);
    };
    
    class vSpec : public viewPort {
    //... stuff ...
    void aPlot(int, int, unsigned long);
    };
    
    viewPort vpobject;
    void (viewPort::*funcp)(int, int, unsigned long);
    funcp = dynamic_cast<viewPort::*>(&vSpec::aPlot); //this is the line you want to use (for some appropriate cast)
    vpobject.*funcp(2,3,4L); //what the heck is this going to DO?
    If this isn't what you want, well I don't know what you want then, because there's no other reason to have a pointer to member function.


    Quote Originally Posted by samGwilliam View Post
    The classes are related somehow
    True...
    Quote Originally Posted by samGwilliam
    and the signatures are the same.
    And back to madness.
    Quote Originally Posted by samGwilliam
    Guess I'll just have to abandon the project.
    My suggestion would be to temporarily put the project on hold and read a book, or at least a couple of chapters.

  3. #18
    Budding Synth Programmer samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Trefforest
    Posts
    368
    Well it allowed: viewPort *v = new vSpec; so there is some degree of interchangabliliy. It makes perfect sense to be able to do what I want to do and I think it's crappy that it's not allowed.
    MSVC++ 6.0

  4. #19
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by samGwilliam View Post
    Well it allowed: viewPort *v = new vSpec; so there is some degree of interchangabliliy. It makes perfect sense to be able to do what I want to do and I think it's crappy that it's not allowed.
    There is:
    Code:
    class viewPort {
    //... stuff ...
    virtual void aPlot(int, int, unsigned long);
    };
    
    class vSpec : public viewPort {
    //... stuff ...
    void aPlot(int, int, unsigned long);
    };
    
    viewPort * vpptr = new vSpec();
    vpptr->aPlot(2,3,4L);
    Notice that you don't have a pointer to a function, you have a pointer to the object, which is the key, since the object itself knows what kind it is, and the compiler doesn't as you're lying to the compiler about what class vpptr points to.

    Again, my suggestion is to read the chapters about polymorphism.

  5. #20
    Budding Synth Programmer samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Trefforest
    Posts
    368
    But vSpec needs the proper plot function too. I just wanted to be able to extend the repertoire of plot methods in the derived class.
    MSVC++ 6.0

  6. #21
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by samGwilliam View Post
    But vSpec needs the proper plot function too. I just wanted to be able to extend the repertoire of plot methods in the derived class.
    It has it. (Assuming by "proper plot function" you mean it's own.)

  7. #22
    Registered User
    Join Date
    Nov 2003
    Posts
    161
    Another solution:

    Code:
    class viewPort
    {
    .
    .
    .
    public:
       static void plot (viewPort* self, int x, int y, int col);
       void (*plotH) (viewPort* self, int x, int y, int col);
    };
    
    .
    .
    .
    
    viewPort::viewPort (int width, int height, HWND hwnd)
    {
    .
    .
    .
    plotH = &plot;
    .
    .
    .
    }
    When you declare a function in a class (f(x)) it's really (f(self, x)). where self is a pointer in memory of the data to act upon.

    In simple terms 'static' say's leave as is, unfortunately if you want to act on data upon the memory location, you'll need to supply it yourself. I think there is also another way but I don't remember. There is a strategy paradigm, I know little about though.

    When calling within the viewPort class

    Code:
    void viewPort::somefunction(){
        plotH(this, x, y, col);
    }
    When calling from somewhere else

    Code:
    int main(){
        viewPort view;
        viewPort *pview;
        pview = &view;
        // initialize and stuff
        view.plotH(&view, x, y, col);
        // or if you have a pointer
        pview->plotH(pview, x, y, col);
        // ...
    }
    Last edited by Benzakhar; 09-02-2008 at 08:24 PM.

  8. #23
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The MORE I read the this, the MORE I think my suggestion is the right one - don't use function pointers, use virtual functions in a class. Not only does it simplify the syntax of the whole lot, but it will "magically work" without much effort, instead of trying to make something that work that doesn't.

    Of course, Benzakhar's solution will work - you can pass a pointer to the object, and then call a static function. But this is basically just "doing your own virtual functions" - you are reinventing the wheel, except your wheel is a octagon in approximation of a circle, so your ride will be a bit bumpy.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #24
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    I agree with mats - polymorphism should do fine. Either static or dynamic.
    Another "way" around your problem is also templates, because it can be any type, you can pass "any" member function pointer. But I don't think your problem requires this. It can be a more advanced solution.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM
  3. Function Pointer help
    By Skydt in forum C Programming
    Replies: 5
    Last Post: 12-02-2005, 08:13 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21