C++ jargon

Initially I thought of writing a new part in my series of C++ teachings, but I changed my mind. I start reading a C++ FAQ and I kept reading and reading. Interesting stuff out there and I strongly recommend to go through (in any case, if you’re making any search about any C++ topic on Google, this will be displayed among first 4-5 most relevant webpages, so it is hard to miss it).

I don’t know whether or not the title indeed reflects the content of this post, but it seemed to me that this FAQ site was depicting the way a C++ programmer thinks. The fact that this post is commenting some of the topics in the aforementioned web-site and that the manner in which some hints are outlined and some C++ insights are explained guided me to the idea that the author is speaking into a “C++ jargon”.

I decided to go through those items and to highlight those which popped to my attention, just as I did it with the video courses. This won’t be some kind of short overview of the FAQ, but only my view of some topics which I considered to be more interesting.

One of the interesting discussions is at topic no6 – Big Picture Issues

One thing which I totally agree upon is that business perspective should rule. The author asks rhetorically:

Is it important to know the technical definition of “good OO“? Of “good class design“?

And then the answer comes:

Read more of this post


C++ part6

26. Pointers to classes (objects)

First of all, one of the biggest achievements of using pointers is that you can directly access memory locations, which is a very good feature to use in low-level programming. I’m working in the embedded field and I cannot conceive writing my code without using pointers. When programming an embedded system you’re somehow “God” on that system, it is strongly required to have the permission to fully access any memory location.
Besides this pointers are very handy. A program written with pointers will be smaller and more efficient than the pointer-less variant.

If you need to access an element of your class (method or member) you have to choices: either you define an instance of that class, or you define a pointer to that class, to access its elements. If you define an instance this will occupy as much memory as the number of its members, but if you define a pointer this will occupy just a pointer-size memory. Just see the example below:

class Example
int a, b;

int main()
Example ex; // this will occupy 2 int’s, probably 8 bytes
Example *pex; //this will occupy as much as an ordinary pointer, usually 4 bytes (generally pointers
// occupy same size as the machine- on a 32-bit CPU, a pointer will nedd 32 bits)
return 0;

Another big advantage of using pointers in C++ is when you need to pass arguments of a class type. Suppose you have such function:

Read more of this post

How I found interesting things about C and C++ languages while learning Symbian C++

It is interesting to see that while reading Symbian C++ code you discover things related to C or C++ language that are not quite the same as you already knew them.
Due to the limitations that a smart-phone has (mostly in terms of power consumption which, from a software perspective, translates into number of CPU cycles required to do something, and in terms of memory size), its corresponding software has to tackle in a very accurate manner some aspects which could be neglected by a PC desktop programmer.

I remember that while browsing a Symbian documentation I read somewhere that:

C++ introduces a memory overhead compared to C (a program compiled in C++ will be bigger than one compiled in C), so if you decide for writing an operating system in C++ you have to be more careful than when writing in C.

One thing which kept my attention when having first contact with Symbian was the fact that you should always use the appropriate data types. This was not very surprising for me taking into account that I have an embedded programming background and I know that I have to be very careful when using unsigned int and when int.
But this is not everything. Symbian even has dedicated typedef’ed data types for each C built-in type (except char and void), and it is strongly recommended to use those instead of C types.

While looking at some examples in Symbian C++ other strange things, like inline constructors, private copy-constructors, private static methods, overloading the new operator with new(ELeave), macros like TRAP and TRAPD, popped into my attention and revealed hidden aspects of C++ language.

Generally if you know C++ it would be quite simple to start writing software for Symbian smartphones. But if this is true, why I read everywhere about the steep Symbian learning curve?

Read more of this post

50 must-know things in C++ – part 5

It’s been a while since I haven’t wrote anything about C++…

23. Copy constructor – a special type of constructor

It is used to create a new object as a copy passed as parameter or used as a return value from a function. It is called also when an object is thrown or caught (if an exception occurs).

Another important thing to mention, which many beginner programmers usually miss it, is the difference with respect to assignment operator “=”.
Assignment operator is invoked in this case:

Object A;
Object B;
B = A;

Copy constructor is invoked in this case:

Object B = A;

Read more of this post

sizeof() operator – not a trivial thing

sizeof() operator … Remember it?
That one mostly used when dynamically allocating memory:

int *pointer; /*pointer to type int, used to reference our allocated data*/
pointer = malloc(sizeof(int) * 10);

Some things about it:

– it returns the size in bytes of any primitive or user-defined variable
– it always returns something positive, as it is defined it returns an unsigned size_t type
– if a type is used like in this example
its argument needs to be parenthesized
-if just variable’s name is used parenthesis can be omitted

char c;
sizeof c
– in the C standards preceding C99 it was a compile-time operator, meaning that it calculated only sizes defined at compilation, it could not calculate something like this
char c[];

Read more of this post

const keyword in C++

This is somehow the second part of my previous post regarding const keyword. To be honest recently I dealt with some problems in using it and I thought useful to look into some valuable resources to get more insight knowledge.

In my previous post I tried to outline some of const qualifier subtilities and in this post I want to continue and to extend a little bit this attempt.

A good point to start this discussion is to trace one difference between C and C++: in C default linkage type is external and in C++ it is internal.

This means that in C a variable defined in one translation unit (source file + its corresponding headers) is visible across the entire program. If you want to restrict this you have to declare the global variables as static.

In C header files do not contain definitions. This is to prevent having more definitions for the same variable (header files can be included in many source files, so more definitions for a single variable can be seen across the program).

This is not the case in C++ where header files usually contain class definitions and can also contain const definitions. In C++ the default linkage type is internal, you are free to write definitions in header files which will be included in many other sources, but this won’t generate any definition collisions.

Read more of this post

C++.. PART4

Ok … so let’s go further becasue it seems that this was a quite long-time approach. Anyway I don’t want to wander into useless details but on the other hand I don’t want to miss something that could be important.

18. Class vs Struct – this will be just a discussion about C structs and C++ classes, so emphasizing the main aspects of object oriented programming.
C structures are the part of C language which makes the transition to C++ and object oriented programming. In C they were designed to be more some data
containers, that’s why in C structs you cannot add functions (if it is to take into consideration differences between C++ structs and C++ classes only
the default access type will be considered: public for struct, private for class). When defined a C++ class does not take memory storage, it happens
only when it is instantiated.

But how things are really passing concerning classes’ declaration, definition and instantiation? Basically classes’ definitions are kept in header files and their
declarations and instantiations in source files, in other words everything that does not have an active role in programs’ execution is kept in headers and the program itself, the part that is indeed executed is kept in .c files. This is not mandatory, this is not requested by the standard, but is just a guideline.

Also a frequent practice in designing classes is to declare its members as private and corresponding functions, which access them as public. Actually
this is the very nature of encapsulation, classes’ characteristics are not visible outside of the class but can be modified from within by some functions
which are accessible from outside.

Go on further to #19