0
class Example
{
    private:
        ...
        Example();
    public:
        ...
};

Sometimes, when you look at other people's source code, there is much code in the class that specify the constructor like Example(), but does not implement it. I am curious about the reason to write it down if it is the purpose not to implement it. Why..?

Additionally, it is noted that even if other types of constructors are defined, they are written down.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
hochan
  • 220
  • 3
  • 10
  • I read the duplicates, but I couldn't get a clear answer. Can I think of it as the following purpose? ```The hidden constructor (declared private) ensures that the class can never be instantiated from outside the class.``` – hochan Jan 17 '21 at 09:36
  • However, I think even this is not enough for explicit reasons other than the implementation of the default constructor. – hochan Jan 17 '21 at 09:38
  • 1
    The accepted answer gives four very specific reasons (and there may be others). What isn't clear about them? The idea is really that only *certain* other parts of the program can access the constructor. – idmean Jan 17 '21 at 09:40
  • However, the constructors are ```A () {}``` or ```A (const int x): x_(x) {}```, ```A (const B& b): A(b. x_) It is implemented like {}```. Example(); was not just specified this way. Is the meaning of ```Example();``` and ```Example(){};``` the same? – hochan Jan 17 '21 at 09:45
  • You can define a constructor out-of-line or inline, as with any other class member. Is your question about the constructor being private or being defined out-of-line? These are two separate, mostly unrelated things. – idmean Jan 17 '21 at 09:46
  • If you write it like Example();, it will be defined in the outline. However, even though it is not defined in the outline, Example(); Likewise, I am curious about the fact that there is a lot of code. – hochan Jan 17 '21 at 09:50
  • 1
    A private constructor which is only declared and never defined doesn't make any sense, unless all class members are static. (And it's bad style anyway.) Otherwise this class (or all non-static members) would be pointless. Do you have an example of such a class? – idmean Jan 17 '21 at 09:53
  • https://github.com/yechoi42/CPP/tree/master/08/ex01 Oh I See! This person will be an example, but I asked because the code of the other 4 people I saw was written that way. Thank you for your kind answer to my bad question. – hochan Jan 17 '21 at 10:00
  • 1
    In your example, the constructor is indeed not defined. The author chose to make this constructor private because he actually didn't want a default constructor. There are better and more modern ways to achieve this in C++ nowadays. But I'm glad if your question is answered now. – idmean Jan 17 '21 at 11:22
  • 1
    Knowing the background, the real answer to your question is: The authors of theses classes don't know modern C++, i.e. `Example() = delete;`. See also https://en.cppreference.com/w/cpp/language/default_constructor – idmean Jan 17 '21 at 11:26
  • Thank you very much. This is a clear answer to my question. Thanks for taking the time. Have a nice day :) – hochan Jan 17 '21 at 12:37

0 Answers0