C++零碎笔记一
这些都是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