CPP01

Main topics

Let's go over the main topics that are present on the first page of the subject :

Memory allocation, pointers to members, references, switch statement

Memory allocation

Remember that C++ basically is an extension of C, yeah so memory allocation also exists in C++. It's mainly not done in the same way as in C.

In C we have malloc and free, in C++ we have new and delete. Let's take an example comparing C and C++ since you already know C memory management pretty well.

C Code
#include <stdlib.h>
#include <stdio.h>

int main(void)
{
    int *ptr; // declare the pointer
    
    ptr = malloc(sizeof(int)); // allocate the memory
    *ptr = 45; // assign a value
    printf("ptr value: %d\n", *ptr); // print the value
    free(ptr); // deallocate the memory
    return (0);
}

In this case, both codes are exactly the same, now let's look at how you would do that for a dynamically allocated array.

Pointers to members

First of all, what are pointers to members ?

Remember that in C, we have pointers to variable and pointers to functions. These also exists in C++ but there's also 2 new pointer types in C++ to introduce the class concept into pointers.

I'll put down here the code example from the intra video on pointers to members.

.* operator

Refers to line 16 in the above example

We set Sample::*p to point to a member foo in the Sample class. The thing is, we could have multiple instances of the Sample class, so we have to specify on which instance we want to use the pointer by using the following syntax: instance.*pointerToMemberAttribute.

This is because the foo member attribute can exist in multiple class instances.

- >* operator

Refers to line 19 in the above example

Here we are not using the Sample instance directly anymore, we have to use the ->* operator instead of the .* operator. It works exactly the same way but it adds a level of dereference in between.

This syntax: instancePtr->*pointerToMemberAttribute where instancePtr points to an instance of the Sample class, does the same thing as the .* operator used on instance variable directly.

Call member function from pointer to member function

Refers to line 24 and 25 in the above example

For pointers to member functions, it works in the same way as pointer to member attributes.

We first set to which member function we want the pointer to point, and when we want to call the function, we have to specify on which instance of the class we'll call the function.

In the same way as pointer to member attributes, we have the two operators, .* and ->*.

The .* operator is used when we use the instance variable directly, and the ->* is used when we have a pointer to an instance variable.

References

References are a new concept, that does not exist in C, it's close as what the pointers do in C.

The more accurate definition of a reference is the following:

A reference is a pointer that is constant and is always dereferenced, it can never be void\

Let's take the first example from the intra video

With just the definition given above, can you guess the output of the above code ?

Code result

Now, we'll take the same example, but this time, I'll comment it so that we can understand what is being done in this code.

Things to note about references in C++

  • A reference is somehow like a dereferenced pointer

  • Once it's defined it will always be referencing the same value

  • You cannot declare a reference without assigning it a value directly

  • References are constant, you can't change what it references after the declaration

  • A reference cannot be void, unlike a pointer that can be void

Some things of the above list in the form of code, as it can be better visualize by some:

Switch statement

There's already something about the switch statement that you can read here.

Last updated

Was this helpful?