1、单件模式
class Singleton
{
public:
~Singleton()
{
printf("析构函数");
}
static Singleton* getInstance()
{
if (NULL == pInstance)
{
pInstance = new Singleton;
}
return pInstance;
}
private:
static Singleton* pInstance;
Singleton(){}
};
Singleton* Singleton::pInstance = NULL;
int main()
{
Singleton *p = Singleton::getInstance();
}
上面程序的缺点:就是在getInstance()函数用new产生的Singleton对象在程序结束的时候,不会调用西沟函数,也就是说delete函数没调用,这样会产生内存泄漏。
把上面的程序改为:
class Singleton
{
public:
~Singleton()
{
printf("析构函数/n");
}
static Singleton* getInstance()
{
if (NULL == pInstance.get())
{
pInstance = auto_ptr<Singleton>(new Singleton);
}
return pInstance.get();
}
private:
static auto_ptr<Singleton> pInstance;
Singleton(){}
};
auto_ptr<Singleton> Singleton::pInstance(NULL);
int main()
{
Singleton *p1 = Singleton::getInstance();
Singleton *p2 = Singleton::getInstance();
if (p1 == p2)
{
printf("相等/n");
}
}
这样通过智能指针可以避免内存泄漏。
另外的实现方法:
#pragma once
class CSingle
{
public:
CSingle()
{
if (NULL == pInstace)
{
pInstace = this;
printf("构造函数/n");
}
}
static CSingle* getInstace()
{
return pInstace;
}
~CSingle()
{
printf("析构函数/n");
}
private:
CSingle(const CSingle& oth);
static CSingle *pInstace;
};
CSingle* CSingle::pInstace = NULL;
int main()
{
CSingle instace; //创建CSingle类的唯一实例
CSingle *p;
for (int i=0; i<10; i++)
{
p = CSingle::getInstace();//获取创建的唯一实例的指针
}
}
网上其它的实现方法:
http://blog.csdn.net/shark0001/archive/2010/05/31/5638009.aspx
2、FALSE/TRUE与false/true的区别:
false/true是标准C++语言里新增的关键字,而FALSE/TRUE是通过#define,这要用途是解决程序在C与C++中环境的差异,以下是FALSE/TRUE在windef.h的定义:
#ifndef FALSE
#define FALSE 0
#endif
#ifndef TRUE
#define TRUE 1
#endif
也就是说FALSE/TRUE是int类型,而false/true是bool类型;所以两者不一样的,只不过我们在使用中没有这种感觉,因为C++会帮你做隐式转换。
2.bool的大小与BOOL的区别:
bool在C++里是占用1字节,而BOOL是int类型,int类型的大小是视具体环境而定的;所以来说:false/true只占用1个字节,而TRUE/FALSE视具体环境而言,以下是BOOL在windef.h中的定义:typedef int BOOL;
3、
http://topic.csdn.net/u/20101024/21/94EC718E-0F61-4CE2-98BD-C4945494FF18.html
4、C++中static的强力总结
http://blog.csdn.net/shandaliuyan/archive/2010/10/08/5927077.aspx
5、C++中const的强力总结
http://blog.csdn.net/shandaliuyan/archive/2010/10/08/5927083.aspx
pair<set<int>::iterator, bool> result1, result2, result3;
s.insert(1);
if (1 == s.size())
{
s.insert(first);
s.insert(second);
s.insert(third);
return true;
}
else if (4 == s.size())
{
if (s.insert(first).second && s.insert(second).second && s.insert(third).second)
{
return true;
}
}
else if (7 == s.size())
{
if (s.insert(first).second && s.insert(second).second && s.insert(third).second)
{
return true;
}
}
return false;
}
int main()
{
for (int i=200; i<500; i++)
{
for (int j=500; j<999; j++)
{
set<int> s;
if (check(s, i) && check(s,j) && (i+j-1000 > 100) && check(s, (i+j-1000)))
{
printf("%d + %d = %d/n", i, j, i+j);
}
s.clear();
}
}
}
int main()
{
int result , a, b, c;
a = 9;
b = 1;
c = 2;
result = c-- + c; //加法为右结合,所以result = 4.
printf("%d/n", result);
return 0;
}
/*
避免重复包含的方法:
(1)#pragma once
(2)#ifndef _CLASSNAME_
#define _CLASSNAME_
这里书写类的代码
#endif
*/
/*
操作内存:memcpy和memset
*/
/*
字符串操作
strcpy、strncpy、strlen、strcat、strncat、strcmp、strncmp、strchr、strstr、atoi、atof、atol、sprintf、strftime
一般情况下,使用strncpy时,建议将n置为dest串长度(除非你将多个src串都复制到dest数组,并且从dest尾部反向操作),
复制完毕后,为保险起见,将dest串最后一字符置NULL,避免发生在第2)种情况下的输出乱码问题。
*/
#pragma once
class Point
{
public:
Point(){}
Point(int m_x, int m_y);
//注意下面两个重载运算符的不同
Point operator + (const Point &oth);//重载加法运算符
Point& operator += (const Point &oth);//重载加法赋值运算符
friend ostream& operator << (ostream& out, const Point& Point);
private:
int x;
int y;
};
int main()
{
Point p1(3, 4);
Point p2(4, 5);
cout<<"point1 is "<<p1<<endl;
cout<<"point2 is "<<p2<<endl;
Point p3 = p1 + p2;
cout<<"point3 is "<<p3<<endl;
p3 += p2;
cout<<"point3 is "<<p3<<endl;
}
Point::Point(int m_x, int m_y)
{
x = m_x;
y = m_y;
}
Point Point::operator +(const Point &oth)
{
return Point(this->x + oth.x, this->y + oth.y);
}
Point& Point::operator +=(const Point &oth)
{
x += oth.x;
y += oth.y;
return *this;
}
ostream& operator <<(ostream& out, const Point& point)
{
out<<"X="<<point.x<<" Y="<<point.y<<endl;
return out;
}