Похожие чаты

Hi I'M implementing vector, and I'd like to know if I

can implement this operator:

vec[0] = 5;

I mean, I know how to overload the bracket operator and also assignment operator, but how to overload both of them at the same time ? Is there any way ?

13 ответов

15 просмотров

If you overload the [] operator it will work for both.

Define Copy assignment and operator[]()

Sahand-🏔️ Автор вопроса
Azadi
If you overload the [] operator it will work for b...

Error : lvalue required as left operand of assignment while it works, if I write cout << vec[0];

Sahand 🏔️
Error : lvalue required as left operand of assignm...

Do you have an array pointer inside your vector class?

Sahand-🏔️ Автор вопроса

How to do this // declaration void operator [](); // implementation // ??? But I need to receive two arguments for this, first one as an index and the second one for the new element..

Sahand-🏔️ Автор вопроса

Elements are allocated in a pointer using malloc()

Sahand-🏔️ Автор вопроса
Sahand 🏔️
How to do this // declaration void operator []()...

You can try to make the operator[] return a reference like this int& operator[](int indx){ return elem[indx]; }

Ujjawal Gupta
You can try to make the operator[] return a refere...

elem is a pointer to an array in heap where all the elements are stored

Sahand-🏔️ Автор вопроса

Похожие вопросы

Обсуждают сегодня

Если у меня есть такой класс: Object = {} function Object:new(a_name, a_transform, a_color, a_mesh, a_material, a_shader, a_textures) local private = {} private.n...
Cuarno Vile
4
А кто-то пробовал, уезжая из Эстонии получить э-рез и продолжить вести предпринимательскую деятельность внутри Эстонии, используя свой OÜ?
Lalalashechki Lalala
62
Is Kadena onto something special? No one really knows. Good luck!
Bright Uncle Stephen EqualBet
40
At which price point will BCH, an ASIC mined coin, be succeptible to reorg attacks because of low hashrate? $10? $1? $0.1? $0.01?
Kishniev
18
я не магистр хаскеля, но разве не может лейзи тип конвертнуться в не-лейзи запросив вычисление содержимого прям при инициализации?
deadgnom32 λ madao
100
Гайз, кто-нибудь пробовал запустить probe-rs под камень, которого нет в probe-rs? Мб есть какой-нибудь пример у кого... Через target-gen попробовал сгенерировать chip-descript...
Максим Смирнов
2
зачем же переименовывать ? чтобы кол-во участников возросло или вдруг IBM от этого снова на свифте начнет кодить ? Я не понимаю что страшного в том что свифт гавно, если это т...
Oleh Nerzh
10
I hodl ocean for a long time. Is there new development that I should read?
A33
12
Is there an article I can read for more information?
Lobaka
10
здравствуйте. совершаю вот такую вещь: strcpy(line, (char)current_number); где current number — неподписанный шорт, line — массив чаров. ругань следующая: main.c:29:30: error...
Roberto's Ширгозиев
13
Карта сайта