-1

I am trying to understand a C++ code. The Following is written in the *.h file

Sys(int w, int h, Eigen::Matrix3f K, bool enableSys = true);
Sys(const Sys&) = delete;
Sys& operator=(const Sys&) = delete;
~Sys();

What is the interpretation of Line 2 and Line 3?

Are those destructors?
Why are they needed?
Is this a good practice?

vyi
  • 1,078
  • 1
  • 19
  • 46
  • `~Sys();` is the destructor. – tkausl Aug 28 '19 at 20:16
  • 3
    You may want to pick up [a good book on modern C++ (C++11 and up)](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). To help you search: from top to bottom, you have a constructor, copy constructor, copy assignment operator and destructor. Copy operations are [deleted](https://en.cppreference.com/w/cpp/language/function#Deleted_functions) - they would normally be generated by the compiler, this keyword prevents it and disallows copying of the object in any way. – Yksisarvinen Aug 28 '19 at 20:17
  • @Yksisarvinen Yeah, I realize its about time to do that. Had been trying to rush through some code (that I didn't write). Thanks! – vyi Aug 28 '19 at 20:21
  • @Yksisarvinen Is it ok to say that the object will be a `Singleton` with only one instance? – vyi Aug 28 '19 at 20:38
  • @vvy Nope. Given the code you've shown, you can create as many objects as you like using it's constructor. But you cannot pass them around by value - you'd have to move them. – Yksisarvinen Aug 28 '19 at 20:42
  • @Yksisarvinen I see, `copy` is prohibited but a new instance can be created. What purpose does it serve, can you suggest a use case for `copy` prohibition? – vyi Aug 28 '19 at 20:46
  • There are quite a few objects in standard library that you cannot copy, for example standard streams (like `std::fstream` or `std::cout`). They hold a direct handle to some system resource - what would be the meaning of "copy"? Should they share that handle? What if they both try to use it at the same time? What if one of them returns the resource to the sytem, but the other tries to use it? Same situation is with `std::unique_ptr` - as the name suggests, it is the only owner of object allocated on the heap. There can be no other `unique_ptr` pointing to the same object. – Yksisarvinen Aug 28 '19 at 20:53
  • @Yksisarvinen the deleted copy constructor / assignment suppresses the default moves too – Caleth Aug 28 '19 at 22:59

1 Answers1

0

Line 2 is a copy constructor, and setting it to = delete means it will not be available. Effectively, the object cannot be copied.

Line 3 is the copy assignment operator, same effect - the object is not allowed to be copy-assigned.

If this is unclear to you, you should read a good book about C++.

Aganju
  • 6,295
  • 1
  • 12
  • 23