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

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.

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

Dave

This question has already been answered. Start a new discussion instead.