Jump to content
otai_g

Reserve Words C++ - Operator

Recommended Posts

tlng lah sakit kepala otak aku memikirkan bnd ni.

operator char * ( void );

[\code]

apakah maksud 'operator'?operator ni adalah reserve words c++.huhu anybody can xplain.help me plz..

Edited by otai_g

Share this post


Link to post
Share on other sites

keyword operator adalah kelebihan c++ utk membuat operator overloading. Bahasa lain takder lagi overloading camni. Function overloading diorang ada la.

Contoh function overloading

int tambah( int x, int y ); <--- Prototaip utk fungsi penambahan

int tambah( int x, int y, int z ): <--- Fungsi menggunakan nama yang sama, ttp berbeza parameter.

contoh tadi boleh digunakan dalam bahasa C++, Java, C# dan ... (err apa language lagi yek?).

Dalam C++ operator macam campur (+), tolak (-), kali (*), bahagi pun boleh di-overloadkan. camni:

int operator+(Date dt, int n)

{

return n + dt.day;

}

maknanya, kalau kita ada struct/class Date camni

struct Date

{

int day;

int month;

int year;

}

kita boleh buat kod ranggi lagi mengancam

Date harini = {15,7,2008};

int minggudepan = harini + 7; <-- Nampak tak? tak perlu guna 'harini.day' utk penambahan

Kesimpulannyer:

operator char*(void);

adalah overloading utk casting kepada data type char*. biasa kod camni sepatutnya dok dalam class string, tapi setau aku rupa dia sebenar

operator const char*(). return type dia takder, so akan guna return type const char*.

Share this post


Link to post
Share on other sites

tq di atas penerangan pnjg lebar saudara.sy akan cuba memahaminya walaupun sekali baca rs x phm apa2.mungkin basic c++ sy masih lemah.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...