0

I would like to ask about my approach to using pointers raw pointers without allocating any memory using pointers. I am working on an application, that is simulating classical cashdesk. So I have a class CashDesk, which is containing vectors of Items and vector of Orders, which are classes to represent items and orders. Furthermore, I want the Order class to contain a vector, which would be a vector of pointers to Item – I don't want to store the object multiple times in different orders, because it makes no sense to me. Through the pointers in Order, I only want to be able to access properties of the class Item, there is no allocating of memory using the pointers.

Simplified code:

class CashDesk {
 vector<Item> items;
 vector<Order> orders;
}
class Order {
 vector<Item*> ItemsInOrder;
}

Class Item containing only structured data – information about the Item.

I create all objects at the level of the CashDesk class – create instance of Item when needed and push it to items vector.

I have been told that I should avoid using raw pointers unless there is no another option. The important thing is that I don't use any memory allocation using pointers – really using the pointer in terms of pointing at the object and accessing it's properties. Should I rather use something like unique_ptr, or completely different approach?

Thanks for any response.

7
  • Creating an object involves memory allocation. Commented Feb 18, 2022 at 13:40
  • Right, I meant that I don't allocate any memory using pointers – only create the object as described. Commented Feb 18, 2022 at 13:43
  • Why does the CashDesk own the items that the Order references? This code design seems odd to me and there can easily be lots of dangling pointer issues croping up (e.g.: when you modify the items vector, invalidating all pointers to those items). There is most likely a better design choice Commented Feb 18, 2022 at 13:45
  • 3
    Reference the elements of vectors by their index, not by pointers. Commented Feb 18, 2022 at 13:46
  • Just have Order be an vector of index values in CashDesk::items instead of a vector of pointers. That is vector<size_t> ItemsInOrder Commented Feb 18, 2022 at 13:47

1 Answer 1

2

I have been told that I should avoid using raw pointers unless there is no another option.

You have been told something subtly wrong. You should avoid owning raw pointers, but non-owning raw pointers are perfectly fine.

You will have to ensure that the elements of Order::itemsInOrder aren't invalidated by operations on CashDesk::items, but that co-ordination should be within the private parts of CashDesk.

You could be more explicit about the lack of ownership semantic, by using std::vector<Item>::iterator in place of Item *, but that doesn't change any behaviour (a conforming implementation may implement std::vector<Item>::iterator as an alias of Item *)

Sign up to request clarification or add additional context in comments.

1 Comment

Usual warning when keeping references to vector elements: beware of reallocations!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.