🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: chorus.os.chorus
Article #3

How can I report an error when constructor parameters are wrong.

#3
From: Eric Valette
Date: Mon, 25 Mar 1991 10:40
115 lines
3551 bytes
/*
 *
 *	I would like to have opinions about the best way to check constructor
 *	parameters validity and report error to caller: I think that something
 *	is missing in the way you can report error to user using Cfront 2.0
 *	
 *	I order to introduce the problem, you can read the small following
 *	example. If I'm missing something let me know...
 */
			C++ example
//----------------------------------------------------------------------
#include <stdio.h>

class born_meter {
	
	int lowBound;
	int highBound;
	int curValue;
public:
	born_meter(int, int, int);
	~born_meter() {}
};

born_meter:: born_meter(int low, int high, int cur ) {
	
	if ( cur < low || cur > high || low > high ) {
		printf("bad initial values\n");
		delete this;	// must release memory already allocated
		return;
	}
	else {
		lowBound   = low;
		highBound = high;
		curValue = cur;
	}
		
	
}


main () {
	
	born_meter * ex1;
	born_meter * ex2;
	
	if ( (ex1 = new born_meter (1,10,5) ) == NULL) { // should work if I
		printf("allocation error\n");		 // I can get memory
	}

	if ( (ex2 = new born_meter (1,5,10) ) == NULL) { // sould produce an error
			printf("allocation error\n");    // that the user can get
	}

}

//--------------------------------------------------------------------

/* 				PROBLEM
 *				-------
 *	
 * Using a Cfront 2.0 compiler I get the "bad initial values" message
 * but not the "allocation error" message because I CAN'T RETURN NULL
 * IN THE CONSTRUCTOR (I cannot set a return value if I don't want to 
 * assign this (might be forbidden in the future release, do not want
 * to always write my own allocator) and "delete this" do not set this 
 * to zero).
 *
 * I Have already imagine some solutions :
 * ---------------------------------------
 *	
 * 1) I can create a new operator that check the parameter validity before
 * calling the global new operator.
 *	
 * 2) I can add a new parameter to the constructor and check the value
 * after the call.
 *	
 * They all have some draw back
 * ----------------------------
 *	
 * Problem with 1)
 * 
 *	- I must pass the parameter to the new operator AND the constructor
 *	- You cannot apply this solution to all the cases (sometimes it is
 *	not possible to do all the check in the new operator).
 *	- I must supply an operator delete if I want no warning.
 *
 *	
 * Problem with 2)
 * 
 *	- I must alway check the return value for new AND the value of
 *	the error flag (if global new operator returns null, the variable
 *	will not be set). I could also initialize this flag with an allocation
 *	error flag.
 *	- must add this error flag in all constructors.
 *	- memory will be allocated and then destroyed
 */
 
 
/* any better idea or comment would be appreciated.


   __                 
  /  `                   	Eric Valette
 /--   __  o _.          	Chorus Systemes
(___, / (_(_(__         	6 avenue Gustave Eiffel
				F-78182, St-Quentin-en-Yvelines-Cedex

Tel: +33 (1) 30 64 82 00	Fax: +33 (1) 30 57 00 66
E-mail: ev@chorus.fr		or ev%chorus.fr@mcsun.EU.net


*/


 
Technical Details
Message-ID: <8862@chorus.fr>
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!swrinde!cs.utexas.edu!uunet!mcsun!corton!chorus!ev

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!swrinde!cs.utexas.edu!uunet!mcsun!corton!chorus!ev
From: ev@chorus.fr (Eric Valette)
Newsgroups: chorus.os.chorus,comp.lang.c++,comp.std.c++
Subject: How can I report an error when constructor parameters are wrong.
Message-ID: <8862@chorus.fr>
Date: 25 Mar 91 10:40:45 GMT
Sender: news@chorus.fr
Lines: 115