0

What i discovered

I discover that C++ vector::at() is an assignable function, how is it possible?

I mean I can call at() function to get the value of vector at certain position, and i can assign a value to a certain position of the vector.

    std::vector<int> vec;
    vec.push_back(4);
    vec.push_back(5);
    vec.push_back(6);

    //get val at position
    cout << vec.at(0)<<endl; //it print 4

    //set val at position
    vec.at(0) = 89;
    std::cout << vec.at(0)<<endl; //it print 89

What I want to do

I want to reimplement in my own vector class without inherit it or copy the original class; how can i do it?

class myIntVector
{
    int arraye[50] = {0};
    
    //other class function and constructor
public:
    int at(int index)
    {
        //some code
    }
};
VLN
  • 13
  • 5
  • 6
    it returns `int&` – apple apple Sep 29 '22 at 15:59
  • 2
    It is possible because `at()` returns a reference (to the value_type). – ichramm Sep 29 '22 at 16:00
  • 1
    `vector.at(foo)` is the same as `vector[foo]`, it's just that `at` will throw an exception if `foo >= size()` while `[]` wont. – NathanOliver Sep 29 '22 at 16:02
  • 1
    @NathanOliver Actually, `vector[foo]` with `foo` out of range is undefined so it _may_ throw an exception in some implementations. – ichramm Sep 29 '22 at 16:06
  • You'll need to adjust your return type slightly to return a _reference_ to the int that you would want to be modified by doing `vec.at(0) = 89;` Make this change: `int& at(int index)` note the `&`. and you can `return arraye[index];` You'll want to learn about _lvalues_ and _rvalues_. `arraye[index]` is an _lvalue_ because it refers to an object but an _rvalue_ like `89` is just a value. – Wyck Sep 29 '22 at 17:08

1 Answers1

2

I want to reimplement in my own vector class without inherit it or copy the original class; how can i do it?

Not trying to sound too snarky but you could read the documentation:

Return value

Reference to the requested element.

Therefore something like this:

class myIntVector
{
    int arraye[50] = {0};
    
    //other class function and constructor
public:
    int& at(int index)
    {
        return arraye[index];
    }
};

Also your indices should be std::size_t, and std::array is preferred to C-style arrays.

Quimby
  • 17,735
  • 4
  • 35
  • 55
  • Does it work because the function become a pointer to what it returns or because who wrote the c++ compiler has defined that to make a function assignable you have to add the reference operator? – VLN Sep 29 '22 at 16:34
  • 1
    @VLN I think you need a [good introductory book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/) on C++. What references are and how they work should be explained in all of them. The function doesn't become anything. There are no pointers involved. A function cannot be assignable. It is the result of the function call that can be assigned to (because it is a lvalue/reference which was initialized to refer to the vector element). This is also not compiler-dependent, but part of the standardized C++ language. – user17732522 Sep 29 '22 at 16:45
  • Perfectly the answer I was expecting. Thanks – VLN Sep 29 '22 at 17:00