0

I'm using a class called Pointer, which is some kind of a wrapper around a real pointer I guess. I think this line of code in this class enables me to get the real pointer:

 operator const T* () const;

What does this mean exactly? How can I call this?

Suppose myPointer is a Pointer<int16_t> object. I should be able to get the int_16* object which wraps this pointer, by using the operator overloading above, but I don't know how.

Edit

Based on the answers belo, I now know I can do this:

const int16_t* myRealPointer = myPointer;

Now suppose I need to call a function which expects a int16_t* parameter (so without the const). What can I do to pass this myRealPointer object to that function?

3
  • 2
    The keywords you are looking for are "implicit conversion operators"; you can trigger it by casting myPointer to int_16* (assuming there's also a non-const version of that operator), by assigning it to an object of type int_16*, by passing it to a function that expects an object of type int_16* and in some other circumstances I don't remember. In practice, an implicit conversion operator enables the object to decay to the target type at the tiniest hint you may need such conversion. Jun 10, 2015 at 10:13
  • maybe the question title is misleading, also the tags
    – Wolf
    Jun 10, 2015 at 10:30
  • @Wolf don't hesitate to adjust it
    – Fortega
    Jun 10, 2015 at 10:40

2 Answers 2

6

This is a conversion operator. For example, you could use it to convert a Pointer<T> to a real T* and, additionally, use it everywhere a T* is expected:

Pointer<float> p(new float);
const float* p2 = p;

In this case, the operator is only defined for the conversion to const raw pointers, so float* p2 = p; would not work. (Also there might be a similar operator for that case.)

3
  • The only true answer.
    – Wolf
    Jun 10, 2015 at 10:24
  • Thanks. Now suppose I need to call a function which expects a int16_t* parameter, and I have a const int16_t* object. What can I do to pass this object to that function?
    – Fortega
    Jun 10, 2015 at 10:25
  • @Fortega declare an int16_t variable and pass the address of it to the function call, another option is casting the const away, but this could be dangerous.
    – Wolf
    Jun 10, 2015 at 10:57
2

This is not operator () as you wrote in the title of the question. It is an implicit conversion operator that converts an object of type Pointer<T>to an object of type T *. So everywhere where an object of type T * is expected you may use an object of type Pointer<T>.

For example

Pointer<int16_t> myPointer;

/* initialization of myPointer */

if ( myPointer != NULL ) std::cout << myPointer << std::endl;

In this code snippet the operator is called twice: in the if condition and in the output statement.

If you do not want such an implicit conversion you could declare the operator with function specifier explicit. For example

explicit operator const T* () const;

If you want indeed to write operator () that is named like the function call operator then it can look the following way

const T * operator ()() const;

In this case the above code snippet would look differently

Pointer<int16_t> myPointer;

/* initialization of myPointer */

if ( myPointer() != NULL ) std::cout << myPointer() << std::endl;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.