Skip to main content

IBM Research


Smart Pointer C++ Interface Primer

The Smart Pointer C++ Interface presents the NAO class library as a C++ class library. If this seems a little strange for a C++ class library to have such an interface, it is because we have avoided using the capabilities provided in C++ for altering syntax (overloaded operators).

There are several issues here. One is that most other languages do not allow operators to be overloaded, so rather than build the capability into the lowest level of the library, we encorporated it at the topmost level. Another issue was the baggage that a really good implementation of overloaded operators carries. This is easily seen in libraries that implement linear algebra, and provide defered execution, and temporary matrices. Rather than clutter the lowest level, this functionality (if it exists) is at the topmost level.

For whatever reasons, the implementation of the NAO library is pretty stark. Few operators are overloaded, no I/O is done in the implementations (except for error messages, and I hope that will change), and memory management is done in an underlying NATopLevel object.

Now, C++ programmers are used to having it much easier than this. So we added these nice features as an interface. The smart pointer interface to NAO overloads the common operators, so that op(f) applies an operator to a function, and f1*f2 multiples two functions, and pt[1] refers to the second coordinate of a point.

The interface also lets you deal with NAO objects as real objects instead of pointers. Abstract objects are only abstract if they are used via pointers. Also, making copies of pointers isn't safe if th object is deleted when it goes out of scope. So the smart pointers are real objects that store a pointer to an NAO object. When the smart pointer is deleted, the stored object is unReferenced. This makes the memory management much simpler.

All interface routines start with the prefix NAS. Some implemented classes have been given thier own smart pointer interfaces, and these smart classes have constructors which correspond to the constructors of the class that they represent. Other classes do not have a direct interface, but may be passed to the constructor of a base class smart pointer. For example,

 NASManifold B(new NABall(3),0);

constructs an NABall, which is a class without its own smart pointer class, and passes it to the constructor for the base class Manifold. The trailing zero is important, since is tells the smart Manifold that the pointer is not being copied. Without this information, the pointer receives an additional reference, and will not be deleted at the proper time. When B is deleted at the end of the program, the ball it points to will be unReferenced. The "new" always returns an object with one reference. If instead, we get the pointer to the ball from somewhere else,
 NASManifold B(List.getBall(153),1);

we use a non-zero flag.


[Comments | NAO Home Page]

[Research home page]

[ IBM home page | Order | Privacy | ContactIBM | Legal ]