Data Acquisition (DAQ) and Control from Microstar Laboratories

Knowledge Base: Platform

  • See the list of other topics for category Platform

Q10111 Using pure virtual functions in the DAPL command environment

Tags: Help, DAPL system, custom modules, custom commands, C++ compiler, class declarations, virtual

Applies to: Custom commands, Developer's Toolkit for DAPL versions 5.1 and earlier

I can't get my custom command module to link properly when I declare a class that declares a pure virtual method. Am I doing something wrong?

Probably not, if you are using the Developer's Toolkit for DAPL version 5.1 or earlier. These releases did not include support the pure virtual feature.

When a pure virtual function is declared, the compiler puts a placeholder function __purecall in the class's virtual function table. Ordinarily, when a subclass is derived from the base class, the subclass defines the declared method, and puts a pointer to that function into the virtual function table to replace the __purecall. The __purecall function should never be called by a valid program. However, because that link is there, and the Developer's Toolkit did not provide an equivalent, this left an unresolved external function reference in the compiled code.

A permanent solution for this problem will not be available until a release of the Developer's Toolkit version after version 5.1. In the meantime, there are two workaround options, neither one perfect, that can get your classes up and running.

  1. Remove the pure virtual initializer notation " = 0" from the method declaration. Provide a default virtual function definition in the base class. If the virtual function is somehow called directly from the base class rather than through a properly-constructed derived class object, this default method can issues an error message and suspend the task.

    While this solution is straightforward, it is possible to instantiate an object of the base class, and not realize that this is an error — this kind of error could be caught at compile time if the pure virtual function declaration were present.

  2. A better but more obscure option is to insert "a hack" to resolve the compiler's otherwise unresolved external reference. Leave the pure virtual declaration in place, but near the top of the code module that implements the base class, include a definition of the missing __purecall function. (See the coding example below.)

    If an invalid invocation of the virtual function is somehow executed, this will catch the problem, issue an error message, and suspend the task. The compiler's ability to detect incorrectly structured derived classes is preserved, so there is a much better chance that invalid code would be caught at the compile stage.

Here is an example of an implementation function to resolve the missing reference.

  extern "C" int __cdecl _purecall(void) {
    printf("Undefined pure virtual method executed from a base class\r\n");
    while (1) task_switch();
    }

L23878