4.6. Implementing Static Methods

Chapter 2 provides important background material on implementing static methods and on doAction(). If you are creating a new node class, the static methods are typically included in the new node class. If you are creating a new action class, the static methods are included in the new action class. In the end, the new class (whether node or action) registers these static methods with the action class.

First, for SoNode( C++ | Java | .NET ), you will usually want to register the nullAction() method (defined on SoAction( C++ | Java | .NET )). This method will be used for any node classes that do not have a specific method registered for them.

For the node classes that actually do something for your action, you need to implement a static method. You'll probably choose one of two approaches:

In our example class, the GetVolumeAction registers the nullAction() method for SoNode( C++ | Java | .NET ). This method, defined on SoAction( C++ | Java | .NET ), will be inherited by nodes that do not implement their own GetVolumeAction:


C++
SO_ACTION_ADD_METHOD(SoNode, nullAction);

GetVolumeAction implements specific methods for the SoCube( C++ | Java | .NET ) and SoSphere( C++ | Java | .NET ) classes. For purposes of example, only the cube and sphere classes implement the GetVolumeAction. If you were actually creating and using such an action, you would probably want to be able to obtain the volume of any shape.

For GetVolumeAction, the group and relevant property classes (such as the coordinate, transformation, and complexity classes) all call doAction(), since they perform their “typical” action behavior:


C++
void
GetVolumeAction::callDoAction(SoAction *action, SoNode *node)
{
   node->doAction(action);
}