delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2002/01/22/21:46:19

X-Authentication-Warning: delorie.com: mailnull set sender to djgpp-bounces using -f
From: "Traveler" <traveler AT netti DOT fi>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: C or C++?
Date: Wed, 23 Jan 2002 04:40:31 +0200
Organization: SAUNALAHDEN asiakas
Lines: 97
Message-ID: <a2l7cr$cv8$1@tron.sci.fi>
References: <a2l20p$qdv$1 AT rivage DOT news DOT be DOT easynet DOT net>
NNTP-Posting-Host: xciv.jdyn.saunalahti.fi
X-Trace: tron.sci.fi 1011753180 13288 195.74.6.94 (23 Jan 2002 02:33:00 GMT)
X-Complaints-To: newsmaster AT saunalahti DOT fi
NNTP-Posting-Date: 23 Jan 2002 02:33:00 GMT
X-Newsreader: Microsoft Outlook Express 4.72.3110.5
X-MimeOLE: Produced By Microsoft MimeOLE V4.72.3110.3
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

>A friend told me NOT to use C++  if I was not going to use Classes and
>Objects... but just use C...
You definetely should use C++. Although classes and objects are little scary
at first there are lot of neat features in the language.
For example: The function overloading (functions with same names)

int    ThisFunctionDoesSomething();
int    ThisFunctionDoesSomething(int x);
int    ThisFunctionDoesSomething(char* string);

With traditional C you will have to do this:
int    ThisFunctionDoesSomething();
int    ThisFunctionDoesSomethingAlso(int x);
int    ThisFunctionDoesSomethingAgain(char* string);

The only catch with function overloading is that:
A)    function argument list must have different number and/or types of
arguments for each new version
B)    you cannot overload an function that differs only with the return type
    int            getSomething();
    double    getSomething();


You can also give default arguments to function arguments:
For example:
int    CountNumberOfCharacters(char* string,char delimeter = '\n');

This imaginary function could count the number of character´s from the
string until it founds the delimeter character.

Now, if you want that the function should read only up to the first ';' then
you just call the function with:
CountNumberOfCharacters(buffer,';');

The catch in this is that default arguments must start from right to left so
you cannot have something like
"int    function(int x, int y = 10,int z)"
"int function(int x,int y = 10,int z = 15)" would be correct.

And, finally, last but not least: The Templates !!!!

Imagine that you are writing an function that does something to different
kinds of data-types.
Allright, thanks to the function overloading you start writing...

int    function(short x);
int    function(int x);
int    function(double x);
int    function(float x);
int    function(long x);
int    function(char x);
int    function(unsigned char* p)
etc...

Now, you begin to frustate and are beginning to give up..."There must be an
easier way" you might think.
There is.

If you notice that your overloaded functions have identical code for all
cases except the "float","double" & "unsigned char*" then you could shorten
the list and write something like this:

template <typename ThisIsJustAnAlias>
int    function(ThisIsJustAnAlias x);

int    function(double x);
int    function(float x);
int    function(unsigned char* p);

There are lot of other things that you can do with the templates, but I am
not going to describe them here.
Besides, the clock is 4:29 am of local time...
All that you have to keep in mind is that using template is like playing
games with normal variables except that these "variables"  hold data TYPES,
not data VALUES.


Lastly there is that thing called STL (Standard Template Library) with lots
of stuff you can play with.
IMHO very cumbersome and not elegant at all (sorry Delorie !) but that´s
what you have untill somebody makes a better one (like me :=) )

Good Luck on your road of coding !
Its a long and hard one but the reward will be much sweeter when you have
that "I made this" feeling...

Traveler
traveler AT netti DOT fi








- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019