Saturday, February 11, 2006

 

this pointer Usage

#include 
#include 

using namespace std;

class Buf
 { public:
  Buf( char* s )
   { buffer = new char[ strlen( s ) + 1 ];
   strcpy( buffer, s ); }
  Buf& Buf::operator=( const Buf &otherbuf )
   { if( &otherbuf != this )
    { delete [] buffer;
    buffer = new char[ strlen( otherbuf.buffer ) + 1 ];
    strcpy( buffer, otherbuf.buffer ); }
    return *this; }
  void Display() { cout << buffer << endl; }
 private:
  char* buffer; };

int main()
 { Buf myBuf( "my buffer" );
 Buf yourBuf( "your buffer" );
 myBuf.Display();
 myBuf = yourBuf;
 myBuf.Display(); }

 

Parallel Computing Lectures

平行計算講座,在此內容有提及如何用(&argc, &argv)的方式。

Friday, February 10, 2006

 

Template@C++

http://www.cplusplus.com/doc/tutorial/templates.html

Tuesday, January 24, 2006

 

Linked List usage

http://www.fortunecity.com/skyscraper/false/780/linklist.html

Sunday, January 22, 2006

 

Misc C++ Usage

  1. C++ Misc Usage Examples

 

sstream Usage

Sstream Usage

Tuesday, December 13, 2005

 

C++ Tutorial

  1. C++ Tutorial
  2. C++ StrTok
  3. Gary's Note
  4. Microsoft MSDN developer
  5. Pointer

Pointer功能強大且好用,但在使用之前,先要initialized,不然它會指向任意處,會造成電腦記憶體不預期的問題。

pointer is initialized to point to a specific memory address before it is used. If this was not the case, it could be pointing to anything. This can lead to extremely unpleasant consequences to the computer. You should always initialize pointers before you use them.

#include 
using namespace std;
int main()
        { int x;
        int *p;
        p = &x;
        cin>> x;
        cin.ignore();
        cout<< *p <<"\n";
        cin.get(); }
int *ptr = new int;

It initializes ptr to point to a memory address of size int (because variables have different sizes, number of bytes, this is necessary). The memory that is pointed to becomes unavailable to other programs. This means that the careful coder should free this memory at the end of its usage. Allocate完區塊的memory,在程式執行完後,必須要釋 放memory(就是透過delete ptr;),然後指定此pointer為null pointer

After deleting a pointer, it is a good idea to reset it to point to 0. When 0 is assigned to a pointer, the pointer becomes a null pointer, in other words, it points to nothing.


This page is powered by Blogger. Isn't yours?