Solved

Handle class and dereferencing operator

Posted on 2014-01-13
5
406 Views
Last Modified: 2014-01-13
I have the following Handle (smart pointer) class, written as an exercise.

template <class T> class Handle
{
public:
	Handle() : p(0) { }
	Handle(T* t) : p(t)	{ }
	// copy constructor
	Handle(const Handle& h) : p(0)
	{
		if (h.p) // check if the handle points to something, otherwise there is nothing to clone
			p = h.p->clone();  // T must have a clone() method.
	}
	Handle& operator=(const Handle&); // assignment operator
	~Handle() { delete p; }
	// need to use comparison with NULL in order to avoid warning in VS 2010 and above
	operator bool() const { return (p != NULL); } // conversion operator from Handle to bool. Uses implicit conversion from pointer to bool
	T& operator*() const;
	T* operator->() const;
private:
	T* p;
};

Open in new window

The class appears to work perfectly fine. However, I have noticed that some books suggest that the * and -> operators should be overloaded in both const and non-const versions, e.g.

T& operator*();
const T& operator*() const

Open in new window

What is the advantage of doing that (if any)?
0
Comment
Question by:Rothbard
  • 2
  • 2
5 Comments
 
LVL 33

Expert Comment

by:sarabande
ID: 39776618
if using the following function

int f(Handle<int> & hx, const Handle<int> &hcx)
{
      int &  x = *hx;  
      const int &  cx = *hcx;  

      return 1111 + cx + x;
}

Open in new window


the first dereference expression would use the T& operator* while the second would use the const T& operator*. if both have the same implementation, it makes no difference, but you could use a different implementation, for example return a reference to a const static member if the pointer is 0 in the const operator while you throw an exception in the non-const operator.

Sara
0
 

Author Comment

by:Rothbard
ID: 39776645
Thanks for your comment. In my Handle class above, the * operator is implemented as follows

template <class T> T& Handle<T>::operator*() const
{
	if (p)
		return *p;
	else
		throw runtime_error("Attempting to dereference unbound Handle.");
}

Open in new window

I can see that your code for the f function runs fine with my definition of *, so I am still not sure why two separate versions are needed. The fact that the method operator*() is const allows it to work on const Handle objects, but it works equally well with non-const objects.
0
 
LVL 33

Assisted Solution

by:sarabande
sarabande earned 100 total points
ID: 39776679
I used the following implementation:

T& operator*()  
 { 
      return *p; 
 }
 const T& operator*() const 
 { 
      return *p; 
 }

Open in new window


and could see that the compiler used a different operator depending on the constness of the variables and arguments. because of that you could have a different implementation for read-only operations what might be useful sometimes.

Sara
0
 
LVL 40

Accepted Solution

by:
evilrix earned 250 total points
ID: 39776774
It's only necessary to implement a const operator. There is no need to implement a non-const operator because constness of the smart pointer container does not reflect constness of the pointer it is managing.

The C++11 standard shared pointer only implements const operators, both return pointers to type T. T will define what the characteristics are of the pointer being managed. In other words T may or may not be const.

http://en.cppreference.com/w/cpp/memory/shared_ptr/operator*
0
 

Author Closing Comment

by:Rothbard
ID: 39776876
Thanks!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Focus not getting shifted out of  editbox 2 57
how to convert c++ code to Android App 3 83
Getting IP address 8 71
C++ to C# code conversion issue 4 100
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now