Default Constructor question.

Let's say I have a parametized constructor, and it's the ONLY constructor that should be used, should I =delete the default constuctor or still use =default?


1
2
3
4
5
6
class Foo
{
public:
    Foo()=delete;
    Foo(int x, int y, int w, int h);
};


So in this case and in my code. Foo should NEVER be called without parameters. So what is the "proper" way of telling the caller "Hey, here is an object Foo, but you NEED to provide it some info"?

Should that default constructor still =default or is =delete the proper way to do things here when you're trying to force the caller to create an object a certain way

setting it to default would still allow them to use that constructor and default values are not good enough for this object, it needs user input. So =delete seemed like the right thing to do here to force them to use the parametized constructor.
Last edited on
If the default constructor should never be called, it's nonsense to provide one.

Then the question is between deleting it explicitly and not declaring it at all, which I suppose is preference. If you wanted to emphasize that a default constructor must never be used, then you could explicitly delete it. But if the function to be deleted isn't provided for "obvious" reasons, then an explicit deletion might just be boilerplate.

Often when you see = delete in real life, there's a comment right above which explains why. So if you have a compelling or unusual reason to delete the function, that's your opportunity to explain yourself to someone who might want to add a definition in the future.

1
2
3
4
5
6
7
class Foo
{ 
public:
  // Foo() = delete;

  Foo(int x, int y, int w, int h);
}
Last edited on
Well I'll give you what I think is a compelling reason to not use one. Lets say I have a pong ball class. We'll call it Ball. That ball will always have a width, and a height, which I pass in the constructor. There is no place where it would make sense to create an object of Ball that is 0 in height and 0 in width, so to me it makes sense to just flat out not allow it. On top of that I'd probably even assert the passed width and height to also make sure it's != 0
Last edited on
What I was trying to say was
"If there's no reason to call a default constructor, then you shouldn't have a default constructor."

Note also that the compiler will only generate a default constructor for a class if
a.) there are no other user-declared constructors for the class; or
b.) the user explicitly requests a compiler-generated default constructor with = default.

For example, the following class is not default constructible, because it has a non-default user-declared constructor:
1
2
3
4
5
6
7
class Foo
{ 
public:
  Foo(int x, int y, int w, int h);
};

Foo f; // error 


Therefore the difference between not mentioning a default constructor at all and defining one as deleted:
1
2
3
4
5
6
class Foo
{ 
public: 
  Foo() = delete;
  Foo(int x, int y, int w, int h);
};

is often just a matter of preference.

By the way, I was missing a public in my previous post. Sorry if that was misleading.
Last edited on
Nope not misleading. I appreciate you clarifying for me. I sort of figured it was a personal preference thing, but figured I would ask because sometimes their are two ways of doing something, though one might have performance benefits over the other, so I figured I'd ask!
Topic archived. No new replies allowed.