C++ - Calling a virtual function in another class

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);
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).
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
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
You need to create an instance of some class that inherits from Document_Interface and that overrides the addLine function.
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);




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
¿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?
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
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.