3

I have a class Item that has a non-default constructor and doesn't have a default constructor. There is no default constructor because I don't want to have Item objects with some strange default values around.

class Item {
 int a;
public:
 Item(int a) : a(a) {}
}

Another class User uses Item class and has a method GetItem(Item * item) that returns an Item object by pointer.

class User {
 Item item;
public: 
 bool SetItem(Item item) {
 this->item = item;
 return true;
 }
 bool GetItem(Item * item) { // Can't do "Item * GetItem()"
 *item = this->item;
 return true;
 }
}

The problem is when I try to GetItem() from somewhere in my program, I still need to allocate memory for an Item object by using some random values, for example Item anotherItem(999);

User user;
Item item(5);
user.SetItem(item);
// Here is the problem
Item anotherItem(999); // any random value
user.GetItem(&anotherItem);

What is the best practice to handle such situations? Is it better to have a default constructor?

Thank you for your help in advance.

asked Feb 16, 2017 at 7:42

2 Answers 2

3

In a situation like yours, there are only a few real options

  1. Allow creation of a default-constructed Item. This means adding a default constructor
  2. Return a (pointer to a) Item from GetItem
  3. Take a Item** or Item*& and let the passed Item pointer refer to the internal Item of User.

None of the options is inherently better than any of the others, although the third one has the big drawback that it allows modification of User content without going through a method of the User class. That opens up the door for violating the constrants that User tries to uphold.

answered Feb 16, 2017 at 8:07
3
  • Thank you for your reply! Could you please explain the second option? Commented Feb 16, 2017 at 9:09
  • 1
    @Konstantin Second options means changing the GetItem method of the User class to Item* GetItem() const { return &item; }. I added the const qualifier as a convention because the call does not modify the User object. Commented Feb 16, 2017 at 9:11
  • @Bart van Ingen Schenau Thank you for the clarification. Commented Feb 16, 2017 at 9:14
0

You can use a pointer for the anotherValue Item. You don't have to call the constructor, you can pass it to the getItem function, you don't have to manage it, Because clearly you don't have the ownership of it.

answered Feb 16, 2017 at 7:53
3
  • 1
    Thank you for the reply, but I'm not sure that I completely understand it. Commented Feb 16, 2017 at 7:59
  • You can declare anotherValue that now is a Item as a Item* (Item pointer) in this way you can skip the constructor call. Commented Feb 16, 2017 at 8:00
  • Can you provide a code sample, please? Commented Feb 16, 2017 at 9:05

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.