Last change: Nov 5 2009 AD
USE g++ INSTEAD OF gcc!!!
#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() ...
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.
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*/
}
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.
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
}
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){ ... }
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*/}}