0

Recently I have seen a design of a colleague. In such design he came up with a class that looks something like this:

class A {
    A() = delete;
    A (const std::string &strA, const std::string &strB) noexcept;
}

The thing that itches me is: Does it really make sense here to explicitly delete the 'A()' constructor?

To try to figure out if there where any implications, in the case that such constructor were not explicitly deleted (but it were simply absent), I opened Eclipse and made a test, and it turned out that (in my case) you can completely dismiss the first constructor (keeping the second one), and the compiler will complain if anybody tries to instantiate an object of 'A' without giving the required list of parameters (i.e. "A myA;" or "A myA = A();" ).

Therefore: For which reason(s) is it desirable to explicitly delete the 'A()' constructor, when you will have a constructor (or several constructors) that takes parameters?

EDIT

I have already seen this post. But IMHO my question extends the question asked in that post. I am/was curios to understand why could it be necessary to explicitly delete a constructor when there are other constructor(s) which take(s) parameters. It didn't make sense to me, but I wanted to check if there were any technical reasons for doing so. What is explained in this post was already cleared to me. That post explains the effects of explicitly deleting a constructor and does not give any further examples of valid use cases or reasons that state when and why it is desirable to do so (one answer explains the effects of deleting the constructor and the other answer explicitly states, that it is an assumption of @gybacsi on a very specific piece of source code).

eindemwort
  • 123
  • 1
  • 1
  • 8
  • 2
    If a user-declared constructor exists (which it does in this case) then no default constructor is declared or defined. In other words, there's no need to defined the default constructor as deleted in this specific case. – Some programmer dude Aug 24 '17 at 15:57
  • Hello @bakaDev I saw also that post you refer to, but I consider that to be a different question. My concrete question inquires about the reasons for explicitly deleting a constructor in the particular case I presented lines above (or any other similar case), not about the general usage or the meaning of explicitly deleting a constructor. – eindemwort Aug 24 '17 at 16:14
  • 1
    As I said in my comment, in this particular case it's not needed. You have to ask your colleague about it. – Some programmer dude Aug 24 '17 at 16:16
  • @Someprogrammerdude That's also my understanding. Seems to me that this guy has more experience with the language than I do and therefore I got confused after seeing that in his design. – eindemwort Aug 24 '17 at 16:17
  • 2
    what is implicit is less clear than what is explicit. the primary purpose is clarity. well designed class declarations are self documenting. additionally, making it explicit communicates that it is not an oversight. never underestimate the importance of clarity, readability or trivially verifiable correctness (TVC), you'll thank yourself when you try to read your own code a few years down the line. – sp2danny Aug 24 '17 at 19:16

0 Answers0