Returning a pointer as a reference

Hi,

Consider the class:

1
2
3
4
5
6
7
8
9
10
class MyClass
{
public:
    MyClass() : myPointer(0) {}

    const int& getMyPointer() const { return *myPointer; }

private:
    const int* myPointer;
};


I am wondering if I should return const int* in the method MyClass::getMyPointer()? Is there any problem in returning const int& as I did? I find the latter more convenient when I need to use the object pointed by myPointer.

Thanks.
Allan
There isn't really a difference except with the const int* version you would have to use '->' instead of '.'. Go ahead and use whichever one seems better in your case.
@firedraco

Wonderful.

Thanks.

A.
Topic archived. No new replies allowed.