Vector front(), Vector back()

My functions begin() and end() both work and would return something similar to front() and back(), however I understand I would return a reference to these values with front() and back(). Currently I have
data is defined in private area as:

int * data;

my functions are as follows
int& front() const
{
return data;
}

int& back() const
{
return data + my_size;
}

The errors I am getting are:

IntVector.cpp: In member function ‘int& IntVector::front() const’:
IntVector.cpp:33:9: error: invalid initialization of reference of type ‘int&’ from expression of type ‘int* const’
IntVector.cpp: In member function ‘int& IntVector::back() const’:
IntVector.cpp:38:14: error: invalid initialization of non-const reference of type ‘int&’ from an rvalue of type ‘int*’
1
2
3
4
5
6
7
8
9
const int&  front() const
{
    return *data;
}

const int& back() const
{
    return *(data + (my_size - 1));
}
Last edited on
You can use int as return type instead of const int&.
Last edited on
Maybie he will not use int as data later? And copies might be undesireable.
Last edited on
Topic archived. No new replies allowed.