How to subclass a function?

Discussion in 'C++' started by richard, May 5, 2005.

  1. richard

    richard Guest

    I'd like to use the seekToPoint function from the following excerpt of
    the OpenInventor SoXtViewer header file:


    class SoXtViewer : public SoXtRenderArea {
    protected:
    SbBool seekToPoint(const SbVec2s &mouseLocation);
    }


    When I try to invoke the function directly in my code, eg:

    viewer->seekToPoint(SbVec2s(1,3));

    gcc complains that the function is protected. Google searching has
    indicated that I need to use a subclass, but I can't seem to find how to
    do this.

    Thanks!
     
    richard, May 5, 2005
    #1
    1. Advertising

  2. richard wrote:
    > I'd like to use the seekToPoint function from the following excerpt of
    > the OpenInventor SoXtViewer header file:
    >
    >
    > class SoXtViewer : public SoXtRenderArea {
    > protected:
    > SbBool seekToPoint(const SbVec2s &mouseLocation);
    > }
    >
    >
    > When I try to invoke the function directly in my code, eg:
    >
    > viewer->seekToPoint(SbVec2s(1,3));
    >
    > gcc complains that the function is protected. Google searching has
    > indicated that I need to use a subclass, but I can't seem to find how to
    > do this.


    class MySoXtViewer : public SoXtViewer {
    public:
    using SoXtViewer::seekToPoint;
    };

    Now you need to declare 'viewer' to be of type 'MySoXtViewer*' and call
    your function.

    V
     
    Victor Bazarov, May 5, 2005
    #2
    1. Advertising

  3. richard

    Rade Guest

    > When I try to invoke the function directly in my code, eg:
    >
    > viewer->seekToPoint(SbVec2s(1,3));
    >
    > gcc complains that the function is protected. Google searching has
    > indicated that I need to use a subclass, but I can't seem to find how to
    > do this.


    If the method is protected, it is not meant to be called the way you want to
    call it (it is meant to be used only in implementations of derived classes).
    I guess you are not going to derive from SoXtViewer, but merely to use an
    existing SoXtViewer to do the job. In this case, you'd better forget that
    seekToPoint even exists and try to do everything you want using the public
    methods.

    I would go with the Victor's suggestion (which is essentially a derivation
    but also seems to me as a small hack) only if I had no other choice e.g.:

    1) If you cannot do what you want using the public methods, and you should
    be able to, knowing the SoXtViewer class semantics (i.e. basically if the
    class SoXtViewer is poorly designed), or
    2) If you are in a hurry to finish a project, and you have no time to think
    about these issues now (but then remember to return to them later)

    Rade
     
    Rade, May 6, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jstorta
    Replies:
    3
    Views:
    446
    jstorta
    Feb 20, 2006
  2. Pyenos
    Replies:
    3
    Views:
    351
    Pyenos
    Dec 22, 2006
  3. S.Volkov
    Replies:
    2
    Views:
    221
    S.Volkov
    Mar 12, 2006
  4. Trans
    Replies:
    8
    Views:
    323
    Robert Klemme
    Oct 23, 2008
  5. Fab

    Subclass of subclass

    Fab, Aug 9, 2012, in forum: C++
    Replies:
    0
    Views:
    396
Loading...

Share This Page