Re: C++ Coding Style

Andreas Pokorny <[email protected]> Mon, 22 Dec 2003 14:09:09 +0100
Newsgroups gmane.linux.zynot.general
Message-ID <20031222130909.GA24408__46843.880453571$1072144274@durix>
--===============0984102731==
Content-Type: multipart/signed; micalg=pgp-sha1;
	protocol="application/pgp-signature"; boundary="8t9RHnE3ZwKMSgU+"
Content-Disposition: inline


--8t9RHnE3ZwKMSgU+
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

Hi,
> Prefer inline functions or templates to macros for code.
> Prefer macros to inline functions or templates for simple expressions.
Instead of that I would suggest:
Use macros only if you want to achieve an effect that can not be done=20
with inline template functions.
=09
> ~    (int *)malloc(sizeof(int[128]));
Never use malloc in C++ code, and never use C casts. Use new and delete.=20
Overload new and delete if a different allocation scheme makes sense.

> Always use new-style casts:
good
>=20
> ~    static_cast<int *>malloc(sizeof(int[128]));
static_cast<int *>(new int [128]);
the new casts "act" like template functions .. so you need ( ) around
the parameter.

> ~    dynamic_cast<Object>anUnknownObject; // Surround with try catch!
You cannot cast from Object to DerivedObject, but you can cast
=66rom Object & to DerivedObject & or Object * to DerivedObject *.=20
In the case of references you need the try catch(bad_cast &e) block, when=
=20
dynamically casting pointers you need to check the result against 0.=20
A failing dynamic cast on pointers will never throw that exception.
I doubt that basic c++ knowledge should be part of coding conventions.

> ~    reinterpret_cast<unsigned int>aFloat; // Avoid! But a good way to
> access the representation of an object in memory.
reinterpret_cast is a save cast, for example you can not cast away const
qualifiers, and you can only cast pointers. It acts different than
casts like static_cast, and it is not as dangerous as a C cast. I do not=20
see any reason to officially avoid it.

What about template Parameters?

1.template<typename ParamT, typename AllocT>
2.template<typename Param, typename Alloc>
3.template<typename param, typename alloc>
4.template<typename param_t, typename alloc_t>
=2E. I prefer #1

My additions to coding conventions are:

const correctness:
~    double getLengthInCM() const ;
~    double getLengthInInches() const;
~    Length getLength() const=20
~    {return *new Length(this->getLengthInCM(), Length::cm); }

	=20
Declare your destructor non virtual if you want to express, that
one may not inherit from that class.

Never use global variables, unless you need an item with external
linkage, but then you should declare them in an annoymous namespace.

to be continued...=20

Andreas Pokorny

--8t9RHnE3ZwKMSgU+
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (GNU/Linux)

iD8DBQE/5uz1URwbI8A03OsRAtHQAKCvPeWC+tnz+YfzNU8LW3fclcGXCQCcDGLr
k24Kp2blTc38znRNI4+jP6o=
=qWfJ
-----END PGP SIGNATURE-----

--8t9RHnE3ZwKMSgU+--

--===============0984102731==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
Xeta mailing list
[email protected]
http://lists.zynot.org/mailman/listinfo/xeta

--===============0984102731==--