I often have this situation

class OrientedPoint
{
	private:
		Point P;
		Vector N;
		Color C;
		bool valid;
		
	public:
	
		//////////// Constructors //////////
		OrientedPoint() {}
		OrientedPoint(const Point &Coord);
		OrientedPoint(const Point &Coord, const Vector &Normal);
		OrientedPoint(const Point &Coord, const Color &C);
		OrientedPoint(const Point &Coord, const Vector &Normal, const Color &C);

where there are many, many constructors to initialize the class when different quantities are available at the time of construction. What I want to do is always set valid to false, without having to say "valid = false" in every constructor, or putting valid(false) in each constructor's initialization list. The idea is that since this code is very fluid, I don't want to change the same thing in 10 places. Is there anyway to do something that would be like

class OrientedPoint
{
	private:
		Point P;
		Vector N;
		Color C;
		bool valid = false; //always init this variable to a certain value!

All ideas welcome!

Thanks,
Dave

Dani AI

Generated

Short answer: there are three practical ways to avoid repeating the same initialization in many constructors.

If you can use modern C++ (the C++0x/C++11+ you mentioned), prefer a default member initializer and, when needed, delegating constructors or the optional type to represent “maybe present” fields. A default initializer declared where the member is declared gives every constructor a sensible baseline without touching each initializer list. Delegating constructors let one constructor do all the real initialization and the others forward to it, avoiding duplication. Use std::optional (or boost::optional for older toolchains) for fields that truly may be absent so you can ask whether a value was supplied instead of guessing from sentinel values.

If you must stay on older C++98/03, two common approaches work: a small single base class whose constructor sets the common state, or a private init() helper called from each constructor. The base-class trick centralizes the logic because base construction runs before the derived constructors; note it introduces an implementation-detail base (prefer private inheritance or a clearly documented “pimpl-like” helper). The init() helper is simpler but happens after member default construction, so it cannot initialize const or reference members and costs an extra assignment for types that default construct.

A few practical tips not shown in the thread: avoid creating a separate derived class per constructor (that was ’s general base-class idea, not a suggestion to multiply types). If you are worried about “junk” values (), prefer explicit presence indicators (std::optional or per-member bool flags) or a small enum/bitmask that records which attributes were set. For API clarity consider named factory functions or a builder object rather than many overloaded constructors — this reduces overload explosion and makes intent explicit (as hinted).

Recommended Answers

All 7 Replies

class WonderPoint
{
public:
    bool isValid() const { return valid; }
protected:
    WonderPoint():valid(false) {}
    bool valid;
};
class OrientedPoint:// Oriented POINT- that's cool!
      public StrangePoint // Fields Medal!!!
{
public:
    OrientedPoint()...
    ...
};

So you're recommending that I derive classes for each of the constructors I currently have?

So you're recommending that I derive classes for each of the constructors I currently have?

???!
You derive a class, not a constructor. Base class constructor executes when a derived class object is created by any its constructor.
Yet another method: define (as usually, private) member function - common initializator. Call it in all your constructors. This method does not work if you must initialize class members via constructors. Alas, in that case you must repeat member initialization list in all constructors.
I think too many constructors is a sympthom of a bad class design. Consider yet another approach: define 1-2-3 constructors then assign a specialization to class objects via special member functions like set, bind, attach etc...

I was talking about "should I make a derived class INSTEAD of each of the constructors I currently have".

I have tried the "make an init() function and call it from all the constructors", but that is almost equally annoying. What is this 1-2-3 constructor you speak of? So you are saying just make a class

class OrientedPoint
{
	private:
		Point P;
                Vector N;
		Color C;
public:
OrientedPoint(const Point &p){P = p;}
void setColor(const Color &c){C = c;}
void setNormal(const Vector &n){N = n;}

};

And call those set() function when you need more things? But then I have no way of knowing if those things have been set and they will end up with junk values. Thats why I wanted to initialize them to 0 or something so I will at least be able to tell if they have been set. See what I mean?

where there are many, many constructors to initialize the class when different quantities are available at the time of construction.

Maybe you should reconsider this. Instead use static functions or external functions that call only a limited set of constructors.

What? What would a static function do here?

It turns out this is an added feature in c++0x. I'm glad the experts agree that this would be helpful!

Dave

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.