How to…

How to create permanent garbage

int *Ptr;

Ptr = new int;

Ptr = 88;

Ptr = new int;

Ptr = 23;

Delete Ptr;

How to create dangling pointer

int *P1; int *P2;

P1 = new int;

*P1 = 88;

P2 = P1;

Delete P2;

How to declare pointer to function

Void ABC(int a[], int size, int(*compare)(int c, int d)) {}

int ascending (int a, int b);

int main () {

int arr[2] = {1,2};

ABC(arr, 2, ascending);

}

How to point to managed object using native pointer

ref class A{

public    : int x;

}

int main(){

A ^a = gcnew A();

a->x = 89;

interior_ptr<int> p = &a->x;

printf(“%d”, *p);

}

How to use tracking reference

ref class A{ public : int x; }

int main(){

A ^a = gcnew A();

int % p = a->x;

(tracking reference is similar to & in unmanaged C++, the difference is that % keeps track of the object address when garbage collector moves the object to new location)

How to box and unbox

int x = 50;

Object ^ o = x;                // implicit boxing

int y = *reinterpret_cast<int ^>(o);     // unboxing

Boxing    : passing unmanaged type to function that expects managed type

Boxing copies ‘x’ as new object that stores it in ‘o’, hence when ‘x’ value change, ‘o’ value does not change

How to pin

ref class A { public : int x; A(int _x) : x(_x); }

int count (int *p) { return *p; }

int main(){

A ^a = gcnew A(5);

pin_ptr<int> k = &(a->x);

count(k);

}

Pinning    : passing address of managed class to unmanaged function

How to declare managed handle in an unmanaged class

ref class A {

public    : A();

gcroot <String ^> str;

}

Prefer initialization over assignment in constructors

NamedString::NamedString(const string & initName, string initVal) : name(initName) {svalue = initVal;}

In the example above, you will be calling two string functions for the string svalue:

The default construct for the string object svalue

The assignment operator= for the string object svalue

If you use initialization list to initialize svalue then only the copy constructor of string will be called.

How to create copy constructor

ref class A {

public    :

A()         : x(0) {}

A(const A & a)     : x(a.x) {}

Private    : int x;

}

A a1;

A a2(a1);    // copy constructor

How to create assignment operator

ref class A{

public:

A()        : x(0) {y++;}

A() & operator=(const A &a){

x = a.x;

y++;

return *this;

}

}

A a1;

A a2 = a1;

POINTER MEMBERS

Three approaches to managing pointer members in C++ classes:

  • Normal pointer like behavior
    • Only copy or assign the address of the pointer.
    • Just used the default copy and assignment operators.
    • Sometimes called shallow copy.
  • Smart pointer
    • The object to which the pointer points is shared.
    • Class automatically prevents dangling pointers.
  • Value like behavior
    • Each pointer points to a different copy of the pointed object.
    • Sometimes called deep copy.
    • Class A {

      Public :

      int x;

      A(int *_x) : x(new int (*_x)) {}

      A(const A & a) : x(new int (*orig.x) {}

      }

These approaches are used very frequently. Programmers should be very familiar with them. (I’m not familiar at all with these terms until today <- such a fail programmer)

HOW TO PASS ACG EXAM    : study

… 16.5 hour to next exam …

3 thoughts on “How to…

  1. @kur: terima kasih atas bimbingan expressnya. LOL
    @dwika: don’t worry. he said he’ll make you pass rite. hahahaha. -.-

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s