21 July, 2009

Single colon

You can use the colon to initialize a member when a method is called. It's usually used to initialize constants in the constructor.

Example:


#include

class object {
const int m_member;
public:
object(const int member);
int getMember();
};

object::object(const int member) : m_member(member) {
}

int object::getMember() {
return m_member;
}

int main(int argc, char *argv[]) {
object o(10);
std::cout << o.getMember() << std::endl;
return 0;
}

typedef

typedef и с чем его едят

typedef
int Apple;
typedef int Orange;
Apple coxes;
Orange jaffa;
...
coxes++;
...
if (jaffa == 10)
...

Static

  • On the difference between static global and static

Static global has file scope and it can't be accessedoutside of the file. While global has program scope and can be accessed outside of file in which it has been defined using extern keyword. A program can be spread across two or more files... so, a global variable can be accessed by all the files.. where as, a static global variable can be accessed only bythe file in which it is declared...static means permanent and hence the value in static global variable is shared by all the functions in that file...

  • Static class variable

    #include
    using namespace std;
    class shared {
    static int a;
    int b;
    public:
    void set(int i, int j) {
    a=i;

    b=j;
    }
    void show();
    } ;
    int shared::a;
    void shared::show()
    {
    cout << "This is static a: " <<>
    cout << "\nThis is non-static b: " <<>
    cout << "\n"; } int main() { shared x, y; x.set(1, 1); x.show(); y.set(2, 2); y.show(); x.show(); return 0; }


Usage and effect of a static data member

  • static variable in a function

#include

int func1(){
static long lfunc=1;

printf("%s : lfunc = %d\n ",__func__,lfunc);

lfunc++;
return 0;
}


int main(){

func1();
func1();
func1();

return 0;
}


Output:
make static && ./static
g++ static.cpp -o static
func1 : lfunc = 1
func1 : lfunc = 2
func1 : lfunc = 3


  • Static methods
A static method defined in a class does not need an object in order to be called.

20 July, 2009

Useful C++ tricks

from http://prokutfaq.byethost15.com

Odd or even?


bool isOdd(int num){
return num&1?true:false;
}


If the number is odd then it's least significant bit (LSB) is set i.e. it is 1. If it is even then it is 0. When you bitwise and (&) this number with 1, the result would be either 1 if the number is odd or zero if it is even.

Passing 2 dimensional array

Example:
/* This program demonstrates passing a 2 dimensional array of size 4 * 4 */

void myFunction(int(*)[4]);

int main()
{
int a[4][4];
myFunction(a);
return 0;
}

void myFunction(int (*myArray)[4])
{
/* Here you can access the content of the argument passed like any other 2-D array */
/* Example: myArray[2][2]=10; */
}


Passing 3 dimensional array

Example:
/* This program demonstrates passing a 3 dimensional array of size 4 * 4 * 4 */

void myFunction(int(*)[4]);

int main()
{
int a[4][4][4];
myFunction(a);
return 0;
}

void myFunction(int (*myArray)[4][4])
{
/* Here you can access the content of the argument passed like any other 3-D array */
/* Example: myArray[2][2][2]=10; */
}

And an inetresting bit about changing particular bits