I can C++ clearly now…

26Nov06

I was working on my thesis program which forces me to resort back to C++ for speed reason. Before that, i did it in python. It’s done. It’s running. It’s correct. It disappoints. The C++ alternative gains light. Alas, C++ (nor C) never been my favourite.

My problem in using C or C++ is, being practically native of Java (the programming language, as well as the island), I have never fully understand how to harness the power of pointers. Those asterisks (*) and ampersands (&) haunts me! Can never fully figure out why (fundamentally speaking) they are there. Books says lots of things : ‘pointers’, ‘pass-by reference’, ‘pointer arithmatics’. Those concepts will never make any sense before I have a solid ground of what in bob’s name those asterisks and ampersands are there for.

Now these few points have shed some lights over this, long been benighted, overworked, short of sleep, programmer (a.k.a me).

Now these few points have shed some lights over this, long been benighted, overworked, short of sleep, programmer (a.k.a me).

NOTE: After reading this, you might do a good round of ‘well…duh!’. I’d say, (re) read the tag-line of this blog. Some might have realize these earlier, some others (like me) might not.

Anyhow here they are…

ONE

Asterisks (‘*’) in variable declaration, and asterisks in statements serves for (related but) different purpose.

Anchovy* x;  //a variable declaration

Is a pointer to type Anchovy. That is, it can be seen as a modifier. It modifies x‘s type. x is not a Anchovy, instead it contains an unsigned int that store the location info of the real Anchovy.

while

cout << *pVar << endl;   //a statement that reads pVar;

is a dereference operator. That is, it tells run-time to grab the content pointed by pVar (pVar being an unsigned int containing memory address).

TWO

Symetrically, ampersands (‘&’)  in variable declaration and in statements serves for (related but) different purpose.

T& var1 = var2  // creates alias for var2.

Alias is not a pointer to, nor a copy of a var. Alias are reference, other names. It can also be seen as modifier. Here it modifies var1. Without it, var1 will contain a copy of var2.
while &var is a reference operator. That is, to obtain an address (i.e. of type unsigned int) of a variable.

Coding style to help differentiate both :
when ‘#’ (‘# being either ‘&’ or ‘*’) is used as modifier, it should be placed closer to the type
[while] when ‘#’ is used as operator, it should be placed closer to the variable.

e.g.

int a = 10
// '*' placed closer to int means b is a pointer
// '&'
placed closer to a means do 'get address' operation on a
int* b = &a;
// '&'
placed closer to int mean c is not an independent int var
// instead, it is an alias of a
int& c = a
c++; //will increment a (is it??)
//
'*' placed closer to b to do 'get content of' operation
cout << *b << endl;

THREE

If a class want’s to participate in the making of array, it MUST have default constructor.

FOUR

About default constructor. In Java defconst are const without params. Well, that’s not the case in C++. Constructors are not only those dull plain no-args function with the same name with the class. Au contrarie. Well not entirely au contrarie, or…umm..whatever…

A default constructor is a constructor that can be called without args supplied (remember default value for function params? go figure!). As for the identifier, yes it must be the same as the class name.

More to come…

Links

Todd A. Gibson’s Tutorial on Selected C++ topics : Pointer
Marshal Cline’s C++ FAQ : Pointer

Advertisements


3 Responses to “I can C++ clearly now…”


  1. 1 bloggplatsen.se
  2. 2 achievements via web site
  3. 3 Famdamnly.net/profile/Clintmcdan

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: