Defining Smart Pointers
You may want to refer to section
“Smart Pointers”. for a reminder of the smart pointer mechanism.
In this section, you will learn how to manipulate smart pointers. To do so, you will first create the class Node and then define a smart pointer to this class. The class Node will remain empty for the moment.
1. Create the file network.h and type the following code:
#include <ilserver/refcount.h>
class Node:
public virtual IlsRefCounted
{
};
The class Node derives from the class IlsRefCounted. This class contains a reference counter as a data member and, therefore, allows you to handle smart pointers.
2. Modify your file network.h, like this:
#include <iostream.h>
#include <ilserver/refcount.h>
class Node:
public virtual IlsRefCounted
{
public:
~Node(){cerr<<"destroyed"<<endl;}
};
typedef IlsSmartPointer<Node> NodeP;
Now the class Node contains a destructor and you have also defined a class of smart pointers to the class Node using the server class template IlsSmartPointer. NodeP is an alias of an instance of the IlsSmartPointer class template. You can use this alias each time you want to manipulate a smart pointer to the class Node.
Note: NodeP is a user-defined notation. This means that you can use the notation that you want for smart pointers. The notation used here is very close to the notation used for an ordinary pointer, that is Node*, in which the asterisk has simply been replaced with a P. |
Now, you are going to test whether an object is automatically destroyed when it is no longer referenced by a smart pointer.
3. Type the following code in a main.cpp file and compile it.
#include <network.h>
main()
{
NodeP n=new Node();
return 0;
}
When you compile this code and run it, the message destroyed appears on your screen. This message indicates that the object node that was allocated in main has been destroyed. When return is executed, C++ automatically destroys the local variable, which is the only smart pointer that points to the node. The reference counter drops to zero and the object is deleted. We have just demonstrated that with smart pointers you do not need to make (and should never make) an explicit call to delete to de-allocate memory.
If you had used an ordinary pointer, the code would have been the following:
#include <network.h>
main()
{
Node* n=new Node();
delete n;
return 0;
}
4. Add the following two lines to the main file to test whether the object is destroyed at runtime.
#include <network.h>
main()
{
NodeP n=new Node();
n=0;
cerr<<"should have been destroyed"<<endl;
return 0;
}
When you compile this code and run it, the messages destroyed and should have been destroyed are displayed on your screen. The node is deleted when the smart pointer is set to zero.
When the newly created node is no longer pointed to by the smart pointer, it is immediately deleted.
Note: You can combine ordinary pointers with smart pointers. However, if you are new to Rogue Wave Server, we strongly advise you not to use ordinary pointers and references to server objects. |
Summary
In this section, you have learned how to define smart pointers for the class Node. You can use this example to declare smart pointers for the other classes that you will be manipulating throughout this tutorial —that is, the classes Network, Domain, and Line, and later on, for the classes of your own applications.
Version 5.7
Copyright © 2013, Rogue Wave Software, Inc. All Rights Reserved.