Re: C++ Coding Style

Andreas Pokorny <[email protected]> Mon, 22 Dec 2003 15:00:15 +0100
Newsgroups gmane.linux.zynot.general,gmane.linux.zynot.devel
Message-ID <20031222140015.GA27640@durix>
There are four issues i noticed after sending the last mail:
I know that these lines are just formating examples, but the
exception problem leads directly to coding conventions. 

> ~    throw *new ArrayIndexOutOfBoundsException;
If you create your exception with new then throw a pointer to your
exception, to tell the 'catcher' that it has to be deallocated. 
Better: 
Never create the exception with new!
do that, instead:
throw ArrayIndexOutOfBoundsException;

and then:
catch ( ArrayIndexOutOfBoundsException & exception )
{}
catch ( BaseException & base  )
{}


> ~         if (o.isa(Class(String)) cout << "I got a String!" << endl;
> ~    else if (o.isa(Class(Number)) cout << "I got a Number!" << endl;
> ~    else if (o.isa(Class(Object)) cout << "I got an Object!" << endl;
> ~    else cout << "I got something wierd." << endl;
> 
> Note that the 'if' lines up. When feasible, line up code to be neater:
> 
> ~       int i = 0;
> ~      char c = '\0';
> ~    double d = 0.0;
> ~     void* p = 0;

    int     i = 0;
	char    c = '\0';
	double  d = 0.0;
	void *  p = 0;


> Local variable declaration at the top of the function body, split into
> groups, seperate groups by a blank line:
no, 
local variables should be as local as possible. 

>~    length getLength() { return *new Length(this->getLengthInCM(),Length::cm); }
Avoid Memory leaks:
You create a Length object using new, then the pointer get
dereferenced, and you return an Object of type Length. So the return
statement will copy that object again. So you created 2 Objects .. thats
ok if you return by value. But the first object has been created on the
heap and will not neither be destroyed nor deallocated when the method 
returns.

Andreas Pokorny