How to subclass a function?

R

richard

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!
 
V

Victor Bazarov

richard said:
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
 
R

Rade

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top