views:

100

answers:

3

I am writing a sparse matrix class. I need to have a node class, which will be a template for its contents. My issue in writing this class is:

How do I store the contents?

I want to store the contents by value. If I stored it by pointer and it should be destroyed, then I'd have trouble. How can I safely perform a copy in the setContents method? Does C++ offer any guarantees that a class that should be placed into my node container has the capability to clone itself?

I've looked into the copy constructor, but I have some qualms. What if the contained class does not implement a copy constructor? Then passing it to the node by reference would not be wise, since that could lead to a dangling reference if the original object should be deleted or go out of scope.

What is the sort of "standard C++" way of doing this?

+3  A: 

The standard C++ approach is to mandate that the type(s) used by your container class must be copyable (and perhaps assignable). It is a very reasonable requirement and is used by all of the container class templates in the standard library.

For built-in types and simple POD-types, a user-declared copy constructor typically isn't needed. A writer of a class that isn't as simple but needs to have value sematics will typically have had to provide a suitable copy constructor in any case.

Charles Bailey
+1  A: 

I am writing a sparse matrix class. I need to have a node class, which will be a template for its contents.

If you copy the nodes in your template implementation this will simply lead to the requirement that nodes are copyable. If somebody tries to instantiate the template with a class that has no public copy constructor he will get compiler errors.

When instantiating a template the compiler checks that all used methods/... are available for the types that are used to instantiate the template. So for your template it will be ensured that all classes that use it have a public copy constructor.

To have a public copy constructor is also a reasonable requirement: The containers in the standard library also require their elements to be copyable. also the compiler automatically generates copy constructors for new types if there is no custom one defined, so most classes have a copy constructor that can be used.

Nevertheless you should document that your template requires copying of nodes with the usual semantics, so that users of the template are aware of it.

sth
A: 

In my industry BOOST libraries have become very popular. Boost.org

In boost there is a class called smart pointer. Basically, it is a wrapper class for a pointer that implements a reference count. When no one is watching the pointer any more the smart pointer deletes itself. It solves many problems including dangling reference, memory leak, and others.

Also, I would also as another option recommend simply implementing a better copy constructor. You could always write a wrapper class for any class that has a better copy constructor if you don't want to add more libraries to your program, if the class you are writing a template for aren't custom and you can just over write the default copy constructor.

Steve H.