How many constructors can be defined in a single class




















Default constructors typically have no parameters, but they can have parameters with default values. Default constructors are one of the special member functions.

If no constructors are declared in a class, the compiler provides an implicit inline default constructor. If you rely on an implicit default constructor, be sure to initialize members in the class definition, as shown in the previous example. Without those initializers, the members would be uninitialized and the Volume call would produce a garbage value. In general, it is good practice to initialize members in this way even when not relying on an implicit default constructor.

You can prevent the compiler from generating an implicit default constructor by defining it as deleted :. A compiler-generated default constructor will be defined as deleted if any class members are not default-constructible. For example, all members of class type, and their class-type members, must have a default constructor and destructors that are accessible. All data members of reference type, as well as const members must have a default member initializer. When you call a compiler-generated default constructor and try to use parentheses, a warning is issued:.

This is an example of the Most Vexing Parse problem. For more information, see Most Vexing Parse. If any non-default constructors are declared, the compiler does not provide a default constructor:. If a class has no default constructor, an array of objects of that class cannot be constructed by using square-bracket syntax alone.

For example, given the previous code block, an array of Boxes cannot be declared like this:. For more information, see Initializers. A copy constructor initializes an object by copying the member values from an object of the same type.

If your class members are all simple types such as scalar values, the compiler-generated copy constructor is sufficient and you do not need to define your own. If your class requires more complex initialization, then you need to implement a custom copy constructor. For example, if a class member is a pointer then you need to define a copy constructor to allocate new memory and copy the values from the other's pointed-to object.

The compiler-generated copy constructor simply copies the pointer, so that the new pointer still points to the other's memory location. For more information, see Assignment and Copy constructors and copy assignment operators. Attempting to copy the object produces error C attempting to reference a deleted function. A move constructor is a special member function that moves ownership of an existing object's data to a new variable without copying the original data.

It takes an rvalue reference as its first parameter, and any additional parameters must have default values. Move constructors can significantly increase your program's efficiency when passing around large objects. The compiler chooses a move constructor in certain situations where the object is being initialized by another object of the same type that is about to be destroyed and no longer needs its resources. The following example shows one case when a move constructor is selected by overload resolution.

It is not assigned to any variable and is therefore about to go out of scope. To provide motivation for this example, let's give Box a large vector of strings that represent its contents. Rather than copying the vector and its strings, the move constructor "steals" it from the expiring value "box" so that the vector now belongs to the new object. The call to std::move is all that's needed because both vector and string classes implement their own move constructors.

If a class does not define a move constructor, the compiler generates an implicit one if there is no user-declared copy constructor, copy assignment operator, move assignment operator, or destructor. The real problem is that the initial design was all static methods. This is really a special case of the classes being too tightly coupled. The now-failing classes are bound to the idea that the functions are static.

There isn't much you can do about that from the class in question. If you want to make this class non-static, you'll have to undo all that coupling that was written into the code by others. Modify the class to be non-static and then update all of the callers to instantiate a class first or get one from a singleton. One way to find all of the callers is to make the functions private and let the compiler tell you.

At lines, the class is not very cohesive. It's probably trying to do too much. In a perfect world you would refactor the class and those calling it into several smaller classes.

Enough to do its task, but remember the Single Responsibility Principle, which states that a class should only have a single responsibility. With that in mind there are probably very few cases where it makes sense to have 9 constructors. I limit my class to only have one real constructor. I define the real constructor as the one that has a body. I then have other constructors that just delegate to the real one depending on their parameters.

Basically, I'm chaining my constructors. The first one is the one that you've added. The second one is similar to the last two but there is a conditional. The last two constructors are very similar, except for the type of parameter. I would try to find a way to create just one real constructor, making either the 3rd constructor delegate to the 4th or the other way around.

I'm not really sure if the first constructor can even fit in as it is doing something quite different than the old constructors. If you are interested in this approach, try to find a copy of the Refactoring to Patterns book and then go to the Chain Constructors page. Surely a class should have as many constructors as are required by the class Class design should be that a constructor creates a valid object after is has finished. If you can do that with 1 param or 10 params then so be it!

It seems to me that this class is used to do way, way to much. I think you really should refactor the class and split it into several more specialized classes. Then you can get rid of all these constructors and have a cleaner, more flexible, more maintainable and more readable code.

This was not at direct answer to your question, but i do believe that if it is necessary for a class to have more than constructors its a sign that it probably should be refactored into several classes. The only "legit" case I can see from you code is if half of them are using an obsolete type that you are working to remove from the code.

When I work like this I frequently have double sets of constructors, where half of them are marked Deprecated or Obsolete. But your code seems to be way beyond that stage I generally have one, which may have some default parameters. The constructor will only do the minimum setup of the object so it's valid by the time it's been created. If I need more, I'll create static factory methods.

Kind of like this:. Okay that's not actually a very good example, I'll see if I can think of a better one and edit it in. Instat of a constructors you add more values to your slots members then in other language. You can add a "init-keyword". Then if you make a instance you can set the slot to the value you want. I think that a class that has more than one constructor has more than one responsibility.

Would be nice to be convinced about the opposite however. A constructor should have only those arguments which are mandatory for creating the instance of that class. All other instance variables should have corresponding getter and setter methods. This will make your code flexible if you plan to add new instance variables in the future. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams?

Collectives on Stack Overflow. Learn more. How many constructors should a class have? Compiler identifies a given member function is a constructor by its name and the return type. Constructor has the same name as that of the class and it does not have any return type. If a class is not required to initialize its data member or does not contain data member, there is no need to write empty constructor explicitly. The arguments to a function are values that can be passed to the function to be used as input information.

The 'return value' is a value that the function returns. For example, in the call to the function square 10 , the value 10 is an argument to the function square. Can a constructor be private? Constructors, like regular methods, can also be declared as private. You may wonder why we need a private constructor since it is only accessible from its own class. When a class needs to prevent the caller from creating objects. Private constructors are suitable.

Can constructor be static? Java does not permit to declare a constructor as static. A constructor always belongs to some object. If a constructor is static, an object of subclass cannot access.

Do constructors have a return type? Constructors in Java do not return anything explicitly.



0コメント

  • 1000 / 1000