Похожие чаты

Hi guys, i defined my own class that is basically

a (private) vector of my own objects + some additional variables, how do i make the object instances of that class have the same methods as a vector has? for example:

MyCont cont = {my objects here}
and i want to do things like:
cont[2]
cont.push_back()
cont.end()
etc.

i see 2 solutions: return the vector and do stuff on it:
// in the class:
std::vector<Obj> vector(){
return m_vector;
}
// in the main file:
cont.vector()[2]
cont.vector().push_back()

or, possibly, somehow adding all of the methods of the standard vector class and using them as a sort of an interface:
//in the class:
void push_back(Obj& obj){
m_vector.push_back(obj);
}
// and the same for each method

but that seems very error-prone and just overall unstable, and i wonder if there's a more elegant workaround to what i'm looking for / how the same problem is solved in more professional apps

2 ответов

8 просмотров

Hey, take a look at this stackoverflow Question, which adds another option to your list, and also discusses the problems of that option. Hope you will find it useful: https://stackoverflow.com/questions/4353203/thou-shalt-not-inherit-from-stdvector

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

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

Здравствуйте! Есть вопросы по ПК: 1.1. Безопасно ли запускать программы, которым я не доверяю, в виртуальной машине под KVM+qemu? (x86_64) 1.2. То же самое при использовании ...
Pavel
5
Hi everyone! I have a two-folded message to share today. First: updates and news, as we are being asked for those. We still want to go like we did the last 6 years: we w...
Marten | Unibright.io
31
Я тут пытаюсь переработать архитектуру подсистемы памяти ядра во что-то осмысленное. Есть pmm, который создает набор range’ей(пока что только для ядра, потом для юзерспейса), ...
Evg Resh
9
Комрады, хотел уточнить. Проперть в OnDestroy юнита-хозяина по-прежнему доступна? И еще уточнение: finalization юнита наступает раньше или позже OnDestroy?
Ed Doc
38
У меня вопрос, а какой уровень оптимизации ставить при сборке на i5 4 ядра 16G ddr3 ОЗУ и видюха Geforce GTX 970?
Sir Jiga
18
Guys, what’s the most expensive part of your budget for your business/startup/app/website?
Ice Kream
18
Скажите, можно ли как-то "переместить" динамический массив из одной переменной в другую? Скажем, переместить из TList<> в TArray<>. Именно переместить, а не скопировать. Если ...
Eugene Krasnikov (ᴊɪɴ x)
37
Oh.... Admins at the main TG is already muted me. How many months Sir you muted me? Or forever if you are here @Janevietani @JohnnySonic
Charles Dar
28
Happy Friday and happy CartesiWeekly! This week on the list: 🪲 Bug Buster Update 🎥 Erick de Moura’s interview with Ornella from Web3TV 👽 Thriving Cartesi alien 🇧🇷 SBRC hackat...
Nida | Cartesi - Never DM first or ask for funds!
1
комрады, че-та лыжы не едут var tmpFont: TFont; begin tmpFont:= TFont.Create; try case rgFontColor.ItemIndex of 0: tmpFont.Color:= clWindowText; 1: tmpFo...
Ed Doc
34
Карта сайта