锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
]]>
]]>
]]>
]]>
1錛夌湅鐪嬩袱涓鍙ワ細
typedef struct _person
{
char m_strName[16];
int m_nAge;
}*TPPerson;
#define DPPerson \
struct _person \
{ \
char m_strName[16];\
int m_nAge; \
}*
鐜板湪鎴戜滑鏈夊涓嬪畾涔夛細TPPerson tp1,tp2; DPPerson dp1,dp2;
tp1 = tp2; dp1 = dp2;
涓婇潰絎竴鏉¤鍙ヤ笉浼氬嚭閿欙紝娌℃湁浠諱綍闂銆傚洜涓簍p1,tp2閮芥槸瀵硅薄鎸囬拡錛屼絾鏄浜屾潯璇彞灝變細鍑虹幇闂錛屽洜涓篸p1鏄竴涓璞℃寚閽堬紝鑰宒p2鏄竴涓璞★紝榪欎釜瀹忛殣钘忕殑閿欒銆?br />2錛塪efine SQUARE(X) (X)*(X)
榪欐槸姹備竴涓暟鐨勫鉤鏂癸紝浣嗘槸鍦ㄧ▼搴忎腑濡傛灉鏈夎繖鏍風殑璇彞錛歩nt x = 5,y; y = SQUARE(++x),榪欐槸涓涓昏緫閿欒鍙堜駭鐢熶簡銆傛眰寰楃殑瀹為檯鍊兼槸42錛岃屼笉鏄?6銆?br />3錛夊瓧絎︿覆瀹氫箟鍜屾爣蹇楃矘璐?br />鐪嬬湅榪欎袱涓畯瀹氫箟錛?br />#define TRACE(X) cout<< #X << " = " << X << endl;
#define MEMBER(A) char* A##_string;int A##_size
絎竴鏉″彲浠ユ墦鍗板嚭浠諱綍涓涓〃杈懼紡鐨勫鹼紝絎簩鏉″彲浠ユ妸涓や釜瀛楃涓插悎騫舵垚涓涓彉閲忥紝鐪嬩笅闈㈢殑渚嬪瓙錛?br />#include
#define TRACE(X) cout<< #X << " = " << X << endl;
#define MEMBER(A) char* A##_string;int A##_size
using namespace std;
int main()
{
for(int i = 0;i < 10;i++)
TRACE((3+i));
MEMBER(one);
one_string = "Hello";
one_size = 4;
cout << one_string << ":" << one_size << endl;
return 0;
}
榪愯緇撴灉鏄細
(3+i) = 3
(3+i) = 4
(3+i) = 5
(3+i) = 6
(3+i) = 7
(3+i) = 8
(3+i) = 9
(3+i) = 10
(3+i) = 11
(3+i) = 12
Hello:4
class聽 Matrix
{
private:
聽聽聽聽int聽 rows,columns;
聽聽聽聽int **pMatrix;
public:聽
聽聽聽聽Matrix(int rows = 3,int columns = 2);
聽聽聽聽Matrix(const Matrix &M);
聽聽聽聽~Matrix();
聽聽聽聽Matrix& operator=(const Matrix& M);
聽聽聽聽int聽 GetRows() const;
聽聽聽聽int聽 GetColumns() const;
聽聽聽聽void聽 SetValue();
聽聽聽聽friend Matrix operator*(const Matrix& a,const Matrix& b);
聽聽聽聽friend ostream& operator<<(ostream& os,const Matrix& M);
};
int聽 Matrix::GetRows() const聽 { return聽 rows;}
int聽 Matrix::GetColumns() const { return聽 columns;}
// 鏋勯犲嚱鏁?
Matrix::Matrix(int x,int y)
{
聽聽聽聽rows = x;
聽聽聽聽columns = y;
聽聽聽聽//鏈夌殑鏃跺欎負浜嗚冭檻鍒涘緩瀵硅薄鐨勬晥鐜囷紝鍦ㄤ嬌鐢ㄧ殑鏃跺欏垎閰嶅瓨鍌ㄧ┖闂達紝鑰屼笉鍦ㄦ瀯閫犲嚱鏁頒腑鍒嗛厤
聽聽聽聽pMatrix = new int* [x];
聽聽聽聽for (int i = 0 ; i < x; i++ )
聽聽聽聽{
聽聽聽聽聽聽聽聽pMatrix[i] = new int[y];
聽聽聽聽聽聽聽聽for(int j = 0;j < y;j++)聽聽 //鍒濆鍖栨瘡涓間負0
聽聽聽聽聽聽聽聽聽聽聽聽pMatrix[i][j] = 0;
聽聽聽聽}
}
// 鏋愭瀯鍑芥暟
Matrix::~Matrix()
{
聽聽聽聽for (int聽 i = 0 ;i < rows;i ++ )
聽聽聽聽聽聽聽聽delete[] pMatrix[i];
聽聽聽聽delete[] pMatrix;
}
// 璧嬪煎嚱鏁?br />Matrix& Matrix::operator=(const Matrix& M)
{
聽if(this != &M)
聽{
聽聽for (int聽 ii = 0 ;ii < rows;ii++ )
聽聽聽if(pMatrix[ii])
聽聽聽聽delete[] pMatrix[ii];
聽聽if(pMatrix)
聽聽聽delete[] pMatrix;
聽聽聽 rows = M.rows;
聽聽聽 columns = M.columns;
聽聽//鍒嗛厤瀛樺偍絀洪棿
聽聽pMatrix = new int* [rows];
聽聽for (int k = 0 ; k < rows; k++ )
聽聽聽pMatrix[k] = new int[columns];
聽聽for ( int聽 i = 0 ; i < rows; i ++ )
聽聽聽for ( int聽 j = 0 ; j < columns; j ++ )
聽聽聽聽pMatrix[i][j] = M.pMatrix[i][j];
聽}
聽return *this;
}
void聽 Matrix::SetValue()
{
聽int聽 i,j,value;
聽for ( i = 0 ; i < rows; i ++ )
聽{
聽聽for ( j = 0 ; j < columns; j ++ )
聽聽{
聽聽聽cout << " 絎?" << i << " 琛?" ;
聽聽聽cout << " 絎?" << j << " 鍒? " ;
聽聽聽cin >> value;
聽聽聽cout << endl;
聽聽聽pMatrix[i][j] = value;
聽聽}
聽}
}
// 鎷瘋礉鏋勯犲嚱鏁?
Matrix::Matrix(const Matrix&聽 M)
{聽
聽rows = M.rows;
聽columns = M.columns;
聽//鍒嗛厤瀛樺偍絀洪棿
聽pMatrix = new int* [rows];
聽for (int k = 0 ; k < rows; k++ )
聽聽pMatrix[k] = new int[columns];
聽for ( int聽 i = 0 ; i < rows; i ++ )
聽聽for ( int聽 j = 0 ; j < columns; j ++ )
聽聽聽pMatrix[i][j] = M.pMatrix[i][j];
}
Matrix operator*(const Matrix& a,const Matrix& b)
{
聽if聽 (a.columns == b.rows)
聽{
聽聽Matrix c(a.rows,b.columns);
聽聽for ( int聽 i = 0 ;i < a.rows;i ++ )
聽聽{
聽聽聽for ( int聽 j = 0 ;j < b.columns;j ++ )聽
聽聽聽{聽聽
聽聽聽聽for ( int聽 columnIndex= 0 ;columnIndex < a.columns;columnIndex++ )
聽聽聽聽聽c.pMatrix[i][j] += a.pMatrix[i][columnIndex] * b.pMatrix[columnIndex][j];
聽聽聽}
聽聽}
聽聽return c;
聽}
聽else
聽聽return Matrix();
}
ostream& operator<<(ostream& os,const Matrix& M)
{
聽for (int i = 0;i < M.rows;i++ )
聽{
聽聽for (int j = 0;j < M.columns;j++ )
聽聽聽os << M.pMatrix[i][j] << "聽 ";
聽聽os << endl;
聽}
聽return (os << endl);
}
// 涓誨嚱鏁?
void聽 main()
{
聽Matrix Ma(3,2),Mb(2,2);
聽Ma.SetValue();
聽Mb.SetValue();
聽cout << Ma << endl;
聽cout << Mb << endl;
聽
聽Matrix Mc = Ma * Mb;//鎷瘋礉鏋勯犲嚱鏁?br />聽cout << Mc << endl;
聽Mc = Mb;聽聽聽聽聽//=榪愮畻絎︼紝鍗寵祴鍊煎嚱鏁?br />聽cout << Mb << endl;
}
int main()
{
聽聽聽聽const float f = max<float>(1.5f,2.5f);
聽聽聽 return 0;
}
榪欓噷const float f = max<float>(1.5f,2.5f);榪欏彞鎬庝箞鍦ㄧ紪璇戠殑鏃跺欎笉鑳介氳繃鍛?鎴戠殑騫沖彴鏄痝cc version 3.2.2 20030222 (Red Hat Linux 3.2.2-5),甯屾湜鑳藉寰楀埌澶у鐨勫府鍔?
class B
{
public:
聽void fun(A *a);
};
class A
{
private:
聽int i;
public:
聽void initialize();
聽class D
{
聽private:
聽聽聽 A *h;
聽public:
聽聽聽聽void initialize();
聽}
聽friend void g(A *a,int i);
聽friend void B::fun(A *a);
聽friend class C;
聽friend void h();
聽friend A::D;
};
void X::initialize()
{
聽i = 0;
}
void g(A *a,int i)//鍏ㄥ眬鍑芥暟g鐩存帴璁塊棶浜嗙被A鐨勭鏈夋垚鍛?br />{
聽a->i = i;
}
void B::fun(A *a)//綾籅鐨勬垚鍛樺嚱鏁扮洿鎺ヨ闂簡綾籄鐨勭鏈夋垚鍛?br />{
聽a->i = 47;
}
class C
{
private:
聽int j;
public:
聽void initialize()
聽void g(A *a);
};
void C::initialize()
{
聽j = 99;
}
void C::g(A *a)//綾籆鐨勬垚鍛樺嚱鏁癵鐩存帴璁塊棶浜嗙被A鐨勭鏈夋垚鍛?br />{
聽a->i = j;
}
void h()//鍏ㄥ眬鍑芥暟h鐩存帴璁塊棶浜嗙被A鐨勭鏈夋垚鍛?br />{
聽A a;
聽a.i = 100;//
}
void A::D::initialize()//璁塊棶A鐨勭鏈夊彉閲廼
{
聽聽聽h->i = 10;
}
void main()
{
聽A a;
聽C c;
聽c.g(&a);
}
閫氳繃涓婇潰鐨勪緥瀛愶紝鎴戜滑鍙互鐪嬪埌鐩存帴璁塊棶綾籄鐨勭鏈夋垚鍛榠鐨勬柟娉曘備絾鏄紝鐢辨鎴戞兂鍒幫紝
褰撴垜浠彂甯冩垜浠嚜宸辯殑C++搴撶殑鏃跺欙紝姣斿鎴戜滑鏈夎繖鏍蜂竴涓簱錛堝惈鏈塼ools.h,tools.lib,tools.dll涓変釜鏂囦歡錛?br />澶存枃浠跺畾涔夛細
class CTools
{
private:
聽int m_nId;
聽int m_nSize;
聽...
public:
聽...
};
鎴戜滑鐨勭洰鐨勬兂璁╁儚m_nId,m_nSize榪欐牱鐨勭鏈夊彉閲忛殣钘忚搗鏉ワ紝浣嗘槸鏈変竴涓敤鎴峰湪浣跨敤鎴戣繖涓簱鐨勬椂鍊欙紝
鍙浠栫敤friend鍏抽敭瀛楃◢浣滀慨鏀瑰氨鍙互鐩存帴璁塊棶鎴戜滑鐨勭鏈夊彉閲忎簡銆傚涓嬶細
class CTools
{
聽聽聽聽friend class CUser;
聽private:
聽聽聽聽int m_nId;
聽聽聽聽int m_nSize;
聽聽聽聽...
public:
聽聽聽聽...
};
榪欐牱鐨勮瘽錛宖riend鍏抽敭瀛楁槸涓嶆槸璺熸垜浠渶鍒濇兂鎶婁竴浜涗笢瑗垮皝瑁呭拰闅愯棌璧鋒潵鐩哥煕鐩懼憿錛?/p>