0

Is this considered as bad style (doing new in the header outside of a construcor)?

class ClassName : public QWidget
{
    Q_OBJECT
public:
    explicit ClassName(QWidget* parent = 0);

    CustomList<Identifier*>*const identifier_list = new CustomList<Identifier*>("someString");

};

What I want to achieve by this is to not have to write if (identifier_list == NULL) everywhere. Like this the identifier_list cannot be NULL.

orde
  • 5,233
  • 6
  • 31
  • 33

1 Answers1

0

That will of course only work in C++11 and above, due to the default member initializer. However, by writing this, you are now required to provide (or =delete) your copy/move constructors and assignment operators. Not to mention a destructor to delete this memory.

Which means you must decide on what copying behavior you want to have. Do you want to allow copying at all (and if not, you must use =delete to signify that)? If so, what should a copy do? Since it's a pointer-const, you can't copy the pointer itself. At least, not in the copy-assignment case. So if you're copying, you now must copy the pointed-to object.

And thanks to your pointer-const, movement is flat-out impossible (without a const_cast). So your move constructor/assignment will have to move the CustomList<Identifier*> object, not merely the pointer to it.

Also, because of all of these user-defined operations, your ClassName will no longer be considered a trivially copyable type. Though given that it stores a linked list, it probably wouldn't be trivially copyable either way.

So is this considered bad style? All things being equal, yes. Unless you have a genuine, well-considered reason to use a dynamic allocation, it's better and ultimately much safer programming style to just make it a value rather than a pointer:

CustomList<Identifier*> identifier_list("someString");
Community
  • 1
  • 1
Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • And if you really want it to be a pointer, at least use a smart pointer wrapper, like `std::unique_ptr`. – Remy Lebeau May 06 '16 at 20:23
  • we decided in our project to create objects by new, cause a friend told me if not doing so the programm can crash if too much data is in objects which are not created on the heap. –  May 06 '16 at 20:27
  • 1
    @Ini: "*the programm can crash if too much data is in objects which are not created by new.*" ... wat?! – Nicol Bolas May 06 '16 at 20:28
  • 2
    @NicolBolas presumably a misguided phobia of the stack. – kfsone May 06 '16 at 20:31
  • @Ini: A better question would be "why would it be true"? Like kfsone said, maybe if you managed to make a 1MB stack object it could cause a problem. But do you have any idea how big 1MB really is, in terms of data structure size? – Nicol Bolas May 06 '16 at 20:35
  • not really, can yoi give me some information about it? I have like 100 000 objects in the list and there are like 10 lists. –  May 06 '16 at 20:36
  • @Ini: The items *in the linked list* will be heap allocated. The list *itself*, the object that manages it, doesn't have to be. `sizeof(CustomList<>)` will be what, 32 bytes, max? The object itself will take up the same space no matter how much stuff is in the list. – Nicol Bolas May 06 '16 at 20:39
  • and how do i write if I want it to be const but on the stack? pass parameter to construcor is not working for me in header when creating a object on stack –  May 06 '16 at 20:43
  • @Ini: It's a member variable; it's not on the stack or the heap. That is determined by how people use your type. What it is is a *value* rather than a pointer. And if it were `const`, you wouldn't be able to add anything to it. – Nicol Bolas May 06 '16 at 20:44
  • yeah u right so not const but how can I create the object with passing something to the constructor? this is not working in the header-file CustomList identifier_list("someString"); –  May 06 '16 at 20:47