-- JuanCastillo - 25 Mar 2008

Important links

callback

Callbacks are useful when you need to pass a function (or another piece of executable code) as a argument of another function. Imagine that you need to give the function as an argument to the constructor of an object Object.

   ...
   void foo1(int x);            //prototype 1
   void foo2(int x);            //prototype 2
   ...
   typedef void (*f_call)(int);   //typedef for the callback (more confortable)
   ...
   void Object( f_call _f, ...);  //constructor

Then you can create an object Object that will work with different functions. In our case:

   Object Object1(foo1,...);  // Object created with foo1 
   Object Object2(foo2,...);  // Object created with foo2   

A declaration is also a function. Since you have a typedef, you can create an array of callback functions like this:

   f_call callbackarray[10] = {NULL};  // array of 10 f_call 
   callbackarray[0] = &foo1;          //element 0 pointing to function foo1
   callbackarray[1] = &foo2;          //element 1 pointing to function foo2
   ...

chars and strings

TIP TIP: Use string unless you can't avoid to use char

Adding to a string

Two ways: whit append and with "+=".
Below you have both:

  string str;
  string str2="Writing ";
  string str3=" 10 ";
  str.append(str2);     // "Writing "
  str.append(str3);     // "10 "
  cout << str << endl;  // "Writting 10"

  string str;
  str  +="Writing ";  //but it's a c-string
  str  +=" 10 ";
  cout << str << endl;  // "Writting 10"

Adding to a char

Sometimes you need a char, directly. It's faster wink
  int part1 = 100;
  char bufferone[1024]; //message buffer
  sprintf(bufferone,"events = %i",part1);
  cout << bufferone << endl;

This will print events = 100 onto your screen.More help: sprintf documentation

String to const char

Basic: just attach c_str() to the string and you will have it. Using the str string above:

     const char *charpointer = str.c_str(); // OK !!

const char to char, string to char

Using the already defined variables:
We cast from a char to a const char.

  char *Mychar = const_cast<char*> ( str.c_str() );  //dangerous: memory leak
  char *Mychar = const_cast<char*> (charpointer);   //equivalent 

Namespaces

  • Definition: In the header file MyTrigFunctions

    namespace MyTrigFunctions
    {
        double cos(double degrees);
        double sin(double degrees);
    }

  • Use (1): In our main:

    #include <MyTrigFunctions>
    int main(){
        cout << "The cosine of 60 degrees is: " <<
                MyTrigFunctions::cos(60) << endl;
    }

  • Use (2): Using "using".

 
    using MyTrigFunctions::cos;
    ...
    cout << cos(60)   << endl;        // uses MyTrigFunctions::cos()
    cout << ::cos(1.5) << endl;      // uses the standard cos() function           

  • Use (3): Using "using namespace".

    using namespace MyTrigFunctions;  //it can be outside your "main"
    ...
    cout << cos(60)   << endl;        // uses MyTrigFunctions::cos()
    cout << sin(60)   << endl;        // uses MyTrigFunctions::sin()

ALERT! ALERT: if you "use namespace MyTrigFunctions", each time you write "cos" you will call to MyTrigFunctions::cos, not to the "standtart" cos function.

Structures

If we define it inside a class, it must be in the header (*.h) file. We can do it in several ways:

    • 1 . Without declaring it as a type:

    struct Person {
        char name[80],
        char address[80];
        void print();
    };
    ...
    void Person::print(){
        cout << "Name:      " << name << endl
                "Address:   " << address<< endl;
    }

    • 2 . Declaring it as a type.

    typedef struct{
        char name[80],
        char address[80];
        void print();
    }Person;

Templates

Template class

    template<typename T>
    class Base
    {
        T const &t;

        public:
            Base(T const &t);
    };

Template function for classes

  • In the header:

    ....
    template<typename Type>
    Type findMaximum(Type *arrIn);  // member template function for class MyArray
    ...

  • In the code:

    template <typename Type>
    Type MyArray::findMaximum(Type *arrIn){
    ...  //code
    }

More info in the C++ Annotations Member templates

Check also :

(tips are from here):

Vectors

The vectors are like complicated arrays. They support random and fast access to elements, constant time insertion and removal of elements at the end (it's not depending on the size of the vector), and linear time insertion and removal of elements (so the time you need to insert or remove is lineally dependent on the size of the vector).
The number of elements in a vector may vary dynamically; memory management is automatic. Some web sources:

Vector of ints

Declaration: vector <int> Num(5);

Filling: There are several ways to fill a vector (to make it grow).

  • A. Like an usual array: Num[0] = 15;
  • B. Adding the element at the end of the "vector line": Num.push_back(15);

ALERT! NOTE: For the first case the "size" ( Num.size()) of our vector could not be defined. Test it wink

Vector or vectors

Equivalent to a matrix of int. We see both and one initilization.

     vector <vector <int>>;
     mat(3,vector<int> 5 );   // matrix of 3 x 5
     for (int i =0; i < mat.size(); i++) {
         for (int j =0; j < mat[i].size(); j++){
             mat[i][j] = 0;
         }
     }

  • Resize. Two ways.
    • To change the colums, mat 3 x 5 --> mat 3 x 7 : mat[i].resize(7);
    • To change both, mat 3 x 5 --> mat 7 x 7 : mat.resize(7);

Vector of objects and operations with it

Let's call our object class Obj. We will make an instance for a a vector of 200 Obj, and we will fill one member Info of each Obj with one value.

    vector<Obj> *Vec = new  vector<Obj>(200);  //vector of 200 Obj
    int In = 1020;                               //value to store in Obj
    for (int it=0; it < 200; ++it) {
        Obj *local = &Vec->operator[](it);   
        local->Info = In;  //store in local->Info the value In
        In += 1;           //new In value
    }

Now we read the Vec in this way:

    for(vector<CDataStructure>::iterator p = Vec->begin(); p != Vec->end(); ++p) {
        Obj *local = &(*p);
        cout << "read :"  << local->Info << endl;
    }

Topic revision: r5 - 2008-08-26, JuanCastillo
 
This site is powered by FoswikiCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding GSI Wiki? Send feedback
Imprint (in German)
Privacy Policy (in German)