Setting members of base class through a constructor of a derived class

I have an abstract base class with a protected member vector<MatrixXd*> pS;. However, whether the contents of this member are viable will be determined in the derived classes. Also, I want to check the viability of the contents in the constructors of the derived classes and throw an exception if the contents do not satisfy certain criteria.
Now, I know that I can do it without any problems. However, I am wondering if this is considered to be a good style and I should not really rethink the whole structure of the system I am trying to build while it is still in the early development stage. Do you think that there is a better way of approaching this issue? This question is really due to the fact that I do not have almost any formal background in computer programming.

In short, the question is: is it OK to modify a protected member variable of a base class in a constructor (or some methods) of a derived class?

To give you a better idea of what I am trying to do

Default constructor of the base class
1
2
CVisualiser::CVisualiser(void){	
}


Beginning of the CVisualiser code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class CVisualiser {

	protected:
		/** data array containing pointers to matrices that need to be visualised */
		vector<MatrixXd*> pS;
		
		/**
 		* Standard exception class for all classes derived from CVisualiser
 		*	@param exception_str - string containing the exception message
 		*/
  	class VisualiserException: public exception	{
  		public:
  			const char* exception_str;
  		VisualiserException(const char* c_exception_str) {
  				exception_str=c_exception_str;
  		}
  		virtual const char* what() const throw() {
  			return exception_str;  			 
  		} 
  	};
		
	public:
		.....


Some random constructor of the derived class. As you can see I rely on the default constructor of the CVisualiser and then force pS value trough this constructor of the derived class (please note that this is just a simplified example that I produced for this topic)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
CVisualiser1D::CVisualiser1D(vector<MatrixXd*>& c_pS, MatrixXd& c_Colours) {
	if ((!(c_Colours.cols()==3))||(c_Colours.maxCoeff()>1)||(c_Colours.minCoeff()<0)) {
		const char* c="Colours matrix specified incorrectly";
		VisualiserException Expt(c);
		throw Expt;
	} 
	else {
		if (!((*c_pS[0])(0,0)==1)) {
			cout << "Visualiser1D constructor called" << endl;
			Colours=c_Colours;
			pS=c_pS;
		}
		else {
			const char* c="matrix specified incorrectly";
			VisualiserException Expt(c);
			throw Expt;
		}
	}		
	
}




Last edited on
To your questions:
It is perfectly legal to modify a non-private inherited data member in any functions of the derived class.

However, I don't think it is perfect to do this on a conceptual level.

One important characteristic of OOP is encapsulation. It makes more sense to me to modify the inherited data member by calling relevant functions in the base class. However, it can be quite cumbersome from time to time. Honestly, I don't always following the encapsulation rule strictly.
Topic archived. No new replies allowed.