-1

I'm not new to C++, though I am fairly inexperienced, so I always wonder if the new habits showing up are bad or good. Today I realized one that I've had quite a lot lately, and couldn't really decide on my own this time.

So whenever I make a new class, I tend to delete the copy constructor, enforcing whoever is using that class to keep a reference to the original instance or using pointers.

To give an example, here's a simple example with just constructors and destructors

class Foo
{
public:
    Foo();
    Foo(const Foo &other) = delete;
    ~Foo();
};

As you can see in the above code example, I have deleted the copy constructor, resulting in the person using Foo would have to use pointers or references if they want to carry it around.

So to my question, I tend to always do this whenever I make a new struct or class. Always. And should I ever need to have a copy constructor, then I implement one. Is doing stuff like this a bad habit, or is it good practice?

Dennis Rönn
  • 101
  • 10
  • 2
    *"person using Foo would have to use pointers or references"* Yeah, but what if you need to actually copy an object? Note that most classes from the standard library don't have their copy constructors deleted: `vector`, `string`, ... – HolyBlackCat Sep 08 '19 at 19:32
  • Personally I've had only good experiences by always deleting it. You can always restore it later if it becomes necessary. – Chris Rollins Sep 08 '19 at 19:32
  • you have to follow 3-5-0 rule: https://en.cppreference.com/w/cpp/language/rule_of_three – Oblivion Sep 08 '19 at 19:33
  • "enforcing whoever is using that class to keep a reference to the original instance or using pointers." - why are those good things to do? –  Sep 08 '19 at 19:34
  • 1
    Some classes naturally have value semantics. Others are move-only. Others still have reference semantics (neither movable nor copyable but shareable). Enforcing either behaviour uniformly is simply wrong. – n. m. could be an AI Sep 08 '19 at 19:36
  • short answer: no – skeller Sep 08 '19 at 19:40

2 Answers2

1

should I ever need to have a copy constructor, then I implement one.

This is a bad idea, because the default copy constructor probably does exactly what you want for struct-style types. If you implement it yourself, then you stand a good chance of getting the implementation wrong, and of not updating it when you update the struct or class. Mostly, you do want a copy constructor and an assignment operator (and if you delete one you should delete the other) for structs. For business-oriented classes such a BankAccount you probably wouldn't want either, and you would also want to delete the default constructor.

1

No, it is not good practice, especially as the only thing to do!

Usually this is done for C++ classes implementing Singleton pattern.

But, there is not much point deleting only the copy constructor - you certainly have to delete atleast and the copy assign operator:

Foo& operator = (const Foo &other) = delete;

Otherwise Foo a(b); and Foo a = b; declarations will be forbidden, but Foo a; Foo b; b=a; not.

Also constructor and destructor (new,delete) should also be private and limited being called only once. But this is much better covered in the topics about Singleton [see].

Baj Mile
  • 750
  • 1
  • 8
  • 17