I don't WANT a default constructor.

Started by
23 comments, last by Andre the Giant 21 years, 7 months ago
quote:Original post by cedricl
This, I don''t understand. Could you explain it? Did you mean that abstract base classes should have a protected contructor?

Presumably, he means that polymorphic classes should be created by a factory which has specially privileged access to the ctor. The same argument regarding encapsulation applies to object creation as to anything - how and where the object is created should be transparent to the user of the object. The way to ensure the user does not dictate how the object comes into being is to disable the ctor and make them go via the factory.

For every design heuristic, there is an equal and opposite reaction. In this case, the danger is of premature generalisation.
Advertisement
Yes, I mean polymorphic objects should be constructed by some sort of factory method. The use of new directly means you know what class you are creating, and that is against the grain of polymorphism.
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
quote:Original post by Magmai Kai Holmlor
The use of new directly means you know what class you are creating, and that is against the grain of polymorphism.

True, and theres a very sound technical argument for doing this. However, it is worth sometimes thinking for a while about when and why you want polymorphism - sometimes its appropriate, sometimes not. Too much polymorphism tends toward amorphism, which means there are few focal points within a design. Polymorphism provides a nice symmetry, but that must be offset against a backdrop of asymmetry else chaos will ensue, and nobody will be able to make any sense of the amorphous mass.
quote:Original post by Andre the Giant
private:
Color color;

Also, my Color object has 2 different constructors, each of which accept parameters (so i DON''T have a default constructor).

I keep getting compile errors saying that "there is no appropriate default constructor available" for the Color class. It points to the Ball constructor as the culprit.


If you have two constructors for Color but no default constructor, then you will need to declare the color object with parameter(s). This is most likely the source of your error. Try this:

Color color(x,y,z);

replacing the letters with the actual parameters used to construct a Color object.

Ken Murphy
Yeah, for all of my polymorphic classes I have a "ModularXXXX" (where X is the name of base class). The modular class just has a pointer to a dynamically allocated instance of one of the base class''s children. I have "FreshStart" and "Convert" templated functions for switching between different types. All of the derived classes just have protected constructors. It''s probably good practice to keep them that way, but I don''t think it really makes that much of a difference if you make public constructors.

--------------------
Matthew Calabrese
Realtime 3D Orchestra:
Programmer, Composer,
and 3D Artist/Animator
"I can see the music..."

This topic is closed to new replies.

Advertisement