C++ - Calling a virtual function in another class

Feb 25, 2018 at 4:43pm
Hello,

I'm trying to call a virtual function declared in another class.
I understand that the normal method of creating an instance won't work.
I can't seem to work out the correct method.
Can somebody help?

Regards

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Document_Interface
{
public:
	...
    virtual void addLine(QPointF *start, QPointF *end) = 0;
	....
};


    QPointF p1 ( (double)endPoint.real(), (double)endPoint.imag() );
    QPointF p2 ( (double)newxStart, (double)newyStart );

    Document_Interface* myDI;
    myDI->addLine(p1, p2);
Feb 25, 2018 at 5:04pm
I understand that the normal method of creating an instance won't work.

What?

Classes have static and and non-static member functions.
The static members are like stand-alone functions: called without an object.

The non-static members are called on object whether they are virtual or not.

If you don't have an instance (of some class that inherits Document_interface), then you cannot call the addLine.


Your line 13 creates an uninitialized pointer. You cannot call anything with such pointer. The pointer must point to a valid object, when you call members (of that object).
Feb 25, 2018 at 5:54pm
Put another way, what do you think will happen here:

myDI->addLine(p1, p2);

Can you point to the code that will be executed? Point at the source code that is the body of the function addLine
Feb 25, 2018 at 7:56pm
I'm trying to call a virtual function declared in another class.

I know I'm doing it wrong, thats why I'm here,

I'm trying to call the virtual function 'addLine(pi, p2)' but don't know how!

Regards
Feb 25, 2018 at 8:50pm
You need to create an instance of some class that inherits from Document_Interface and that overrides the addLine function.
Feb 25, 2018 at 9:02pm
You have to actually write it.

1
2
3
4
5
6
7
8
9
10
11
12
13
class someOtherClass : public Document_Interface()
{
  void addLine(QPointF *start, QPointF *end)
  {
      // do something here!
  }
}

    QPointF p1 ( (double)endPoint.real(), (double)endPoint.imag() );
    QPointF p2 ( (double)newxStart, (double)newyStart );

    someOtherClass anInstance;
    anInstance.addLine(p1, p2);




Feb 26, 2018 at 8:04pm
Hello
Can you point to the code that will be executed? Point at the source code that is the body of the function addLine

Searching though the program I found this for "addLine".
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void Doc_plugin_interface::addLine(QPointF *start, QPointF *end){

    RS_Vector v1(start->x(), start->y());
    RS_Vector v2(end->x(), end->y());
    if (doc) {
		RS_Line* entity = new RS_Line{doc, v1, v2};
        doc->addEntity(entity);
        if (!haveUndo) {
            doc->startUndoCycle();
            haveUndo = true;
        }
        doc->addUndoable(entity);
    } else
		RS_DEBUG->print("Doc_plugin_interface::addLine: currentContainer is nullptr");
}

What would be better,
1 - adding in a call to this function,
2 - class someOtherClass... your suggestion,
3 - inlining it in my class.

Regards
Last edited on Feb 26, 2018 at 8:07pm
Feb 26, 2018 at 8:35pm
¿what are you trying to do? ¿what problem are you trying to solve?

I don't understand how at this point you don't have an instance of `Document_Interface', ¿where do you intend to add the line to?
Feb 28, 2018 at 10:41am
Hello,

My apologies, in my last post it should have said-:
"Searching though the program I found this for "addLine", which is declared as a non virtual function.
Thanks to @Repeater, I understand a little more about how to call virtual fuction in another class.
Thanks to all who took an interest.

Regards
Feb 28, 2018 at 2:07pm
which is declared as a non virtual function.


Actually, assuming Doc_plugin_interface is derived from Document_Interface, it's not. Because void addLine(QPointF *start, QPointF *end) is declared virtual in the base class, it will be virtual in all derived subclasses. So, even though the keyword virtual may not have been used in the Doc_plugin_interface declaration, the function was declared as a virtual function through inheritance.
Topic archived. No new replies allowed.