main | links

C++ links

tutorials and references

Last change: Nov 5 2009 AD

Index


Links

Various

Tutorials

References and nearly-like-reference tutorials

Note: [5] = the best, [4] = quite good, [3] = not so bad

STL links

 

Articles, papers etc.

Other resources and tools

Sources


Notes

USE g++ INSTEAD OF gcc!!!

Troubleshooting

"Undefined reference to operator new ..."
I got this error during linking of a simple c++ (.cc) file.
Solution: use g++ instead of gcc.
ifstream::eof()
Problem: eof() returned false one pass later than I expected.
Solution: eof() doesn't return false when you got to the end of the file, but later, after the first failed reading from the file. Thus to realize that you are just in the end, you must read something first.

Conversion int to string

(Thanks to Martin Vejmelka for this)
C++:

                                 #include <sstream>
                                int to_be_converted;
                                 stringstream s;
                                 s << to_be_converted;
                                 string converted_string( s.str() );

C:

                                #include <stdio.h>
                                 int to_be_converted;
                                 char converted_string[20];
                                 snprintf( tmp, 20, "%d", to_be_converted );
 

                                 Voilà! I reccomend to use the function snprintf(), it's safer.
                                 (you set the max. number of chars => you can't overwrite memory)
 
The other way round: e.g. atoi() ...

Dynamic Array of Pointers

MyClass** myPtrArray;              // Define array of pointers to instances of MyClass.
myPtrArray    = new MyClass*[4];   // Allocate array of 4 pointers.
myPtrArray[0] = new MyClass();     // Set the first element of the array.
myPtrArray[0]->name = "Aja";       // Set a field of the instance of MyClass.

Various 

1) NESTED CLASS

class List {
class ListCellule; //declare existence of the nested class, needed for the next 
line
ListCellule* tete;
ListCellule* findCellule(const Record* quoi) const;
public:

private:
//************************************************************* LIST::listCellule
class ListCellule {
public:
ListCellule* precedent;
};//ListCellule

}; //List

List::ListCellule* List::findCellule(const Record* quoi) const{ /*code*/ }

2) CONST X POINTEUR

MyClass * const ptr; //constant pointer to an "objet variable"
MyClass const * ptr; //pointer to a constant object
const MyClass * ptr; //pointer to a constant obj.

Summary:

If 'const' is before '*' it relates to the type and you get a pointer to a constant object.
If 'const' follows '*' it relates to the pointer variable itself and you get a constant pointer to a mutable object.

3) POINTEUR SUR FONCTION MEMBER

class MyObjet{
public:
const int i;
int (MyObjet::*pget)(); //ptr sur une fonction: int fonc(){...}
int get1();
MyObjet(int val) : i(val) {
pget = &get1;
cout << ">Constr.: i = " << i << endl;
(this->*pget)(); //!!!! n'oublie pas les premiers '()': (...)();
};
};//MyObjet

int main (int argc, char* argv[]) {
MyObjet obj(3);
int (MyObjet::* ptrFunc)() = & MyObjet::get2;
obj.*ptrFunc(); //erreur: pointer to member function called, but not in class 
scope
(obj.*ptrFunc)(); //OK, maintenant sans problems
}

4) SURCHARGEMENT DES OPERATEURS

class X{... operator int& () {return this->entier;}} //cast op.qui retourne une 
reference => pour les fonc. qui changent leur arg.
// ca marche aussi pour convertir en int

- redef. op. *: 1) operator void* () { return ptr; }
2) T& operator*(){ if(!ptr) {throw new InvalidPointer();} else { 
return *ptr;} };
(les deux sont different)
// Delete: 1.Doit etre static 2.Prend un parametre de type void* 3. '::delete' est 
le delete global
static void operator delete (void* sr) { ::delete ((SmartPointer<T>*)sr)->ptr; 
(...)sr->ptr = 0; }

SURCH. << + TEMPLATE:
template <typename T> class X {... friend ostream& operator<< <T> (ostream&, 
SmartPointer<T>&); }//X
template <typename T> ostream& operator<< (ostream& os, SmartPointer<T>& sp){ ... }

5) Executing another program

1. execvp (v = use argv[], p = search PATH for the executable)

The last element of arguments[] must be NULL! 

#include <process.h>
int main(int argc, char* argv[]){
  char** arguments = new char*[ 2 + argc ];
  for (int i=1; i < argc; ++i){ arguments [2+(i-1)] = argv[i]; } // skip the name of our program
  arguments[0] = "program2run.exe";
  arguments[1] = "myArgument";
  arguments[2 + (argc-1)] = NULL; // the last element must be null
  if ( execvp( arguments [0], arguments ) == -1 ){/*an error*/}}

 


Jakub Holy 2002 AD