這些都是06年時候在學校的時候寫的,當時放在CSDN的blog上面,現整理一下搬過來。
1,關於static
類static資料成員是全域變數,但其作用範圍是類範圍,static成員只在檔案範圍類初始化依次,即使類沒有一個對象,static成員也是存在的。
如果允許的話,static成員可以被任何對象訪問,也可以用二元範圍運算子通過對象訪問。
在C++中,當對類對象使用static時,將使所有的類對象共用成員的唯一一個副本, 但static資料成員必須要在檔案範圍內初始化.
e.g
class classname
{...
private:
static int x;
};
int classname::x = 10;
static函數沒有this指標,因為它不屬於任何一個對象。 不能將static成員函式宣告為const。 static成員函數不能訪問非static成員資料和成員函數。
本文來自CSDN部落格,轉載請標明出處:http://blog.csdn.net/chusky/archive/2006/02/22/606376.aspx
2.several useful macros:
//macro.cpp
#include <iostream>
#include <string.h>
using namespace std;
#define DEBUG(x) cout << #x " = " << x << "\n"
#define TRACE(s) cerr << #s "\n", s
#define FIELD(a) char* a##_string; int a##_size
void print(char c)
{
cout << c << "\n";
}
int main()
{
FIELD(one);
one_string = "Hello";
one_size = strlen(aone_string);
DEBUG(aone_string);
for(int i = 0; i < aone_size; i++)
TRACE(print(aone_string[i]))
return 0;
}
本文來自CSDN部落格,轉載請標明出處:http://blog.csdn.net/chusky/archive/2006/02/22/606366.aspx
3.When do we need a handle class?
If we want to hide the whole information about a class, we need to define a handle
class to wrap it. We just declare the class in the handle class, and implement it in another
file.
Merits:
1, hide the implemetation
2, when we modified Cheshire, what we need to do is to recompile handle.cpp and link
the object file to the project. If we don't use this mechnism, we would to build all the file
that include the class's head file, which we have modified;
Just as follow:
//handle.h
//Handle classes
#ifndef HANDLE_H
#define HANDLE_H
class Handle
{
struct Cheshire; //class declaration only
Cheshire *smile;
public:
void initialize();
void cleanup;
int read();
void change(int);
};
#endif //HANDLE_H
//handle.cpp
//Handle implementation
#include "handle.h"
//define Handle's implemetation:
struct Handle::Cheshire
{
int i;
};
void Handle::initialize()
{
smile = new Cheshire;
smile->i = 0;
}
void Handle::cleanup()
{
delete smile;
}
int Handle::read()
{
return smile->i;
}
void Handle::change(int x)
{
smile->i = x;
}
本文來自CSDN部落格,轉載請標明出處:http://blog.csdn.net/chusky/archive/2006/02/22/606361.aspx
4.用引用實現多態的奇怪現象
#include<iostream>
using namespace std;
class Shape
{
public:
virtual print() const = 0;
};
class Point : public Shape
{
public:
virtual print() const { cout << "Point" << endl; }
};
class Circle : public Shape
{
public:
virtual print() const { cout << "Circle " << endl;}
};
int main()
{
Point p;
Circle c;
Shape& s = p;
s.print();
s = c;
s.print();
return 0;
}
為什麼兩次列印的都是"Point"(執行環境VC6.0)?
對於你們可能是小問題,但對於卻是困擾我的大問題
如有哪位知道請指教,非常感謝!!!
回答:
當然都是列印"Point",引用只在定義階段有效!不能改變指向其他位置,它與指標的區別就在此,並且沒有給它在棧分配空間!不過沒有報錯比較奇怪!
本文來自CSDN部落格,轉載請標明出處:http://blog.csdn.net/chusky/archive/2006/02/22/606356.aspx
5.變長參數應用舉例
先得聲明一個變長參數的變數va_list list
在使用前要先用va_start(list, last_param)對list進行初始化,last_param為最右邊的已知參數,表示list
從last_param的下一個參數開始
va_arg(list, 類型)
最後不要忘了用va_end(list)
eg1:
#include<iostream>
#include<iomanip>
#include<stdarg.h>
using namespace std;
double average(int, ...);
int main()
{
double w = 37.5, x = 22.5, y = 1.7, z = 10.2;
cout << setiosflags(ios::fixed | ios::showpoint)
<< setprecision(1) << "w = " << w << "\nx = " << x
<< "\ny = " << y << "\nz = " << z << endl;
cout << average(2, w, x) << endl;
cout << average(3, w, x, y) << endl;
cout << average(4, w, x, y, z) << endl;
return 0;
}
double average(int i, ...)
{
double total = 0;
va_list ap;
va_start(ap, i);
for(int j = 1; j <= i; j++)
{
total += va_arg(ap, double);
}
va_end( ap );
return total/i;
}
eg2:
#include<iostream.h>
#include <stdlib.h>
#include <stdarg.h>
void error(const char*format...);
void main()
{
int a;
char c='d';
char s[100];
error("Enter a string:"); //輸入一個字串
cin>>s;
error("Enter an integer:"); //輸入一整數
cin>>a;
error("%s\n%d\n%c\n",s,a,c); //列印輸出
}
void error(const char*format...) //實現像printf函數一樣的列印輸出功能
{
int i;
int j=0;
va_list ap;
va_start(ap,format);
for(i=0;*(format+i)!=0;)
{
int in;
char* pc;
char d;
if(*(format+i)=='%')
{
switch(*(format+i+1))
{
case'd':in=va_arg(ap,int);cout<<in;i=i+2;break;
case's':pc=va_arg(ap,char*);cout<<pc;i=i+2;break;
case'c':d=va_arg(ap,char);cout<<d;i=i+2;break;
default:cout<<'%';i=i+1;break;
}
}
else
{
cout<<*(format+i);
i++;
}
}
}
本文來自CSDN部落格,轉載請標明出處:http://blog.csdn.net/chusky/archive/2006/02/22/606346.aspx