代码改变世界

Visual C++ 2011-5-6

2011-05-09 22:17  Clingingboy  阅读(504)  评论(0编辑  收藏  举报

一.C++友元

1.友元函数

#include <iostream>

using namespace std;
class Point
{
    friend void ChangePrivate( Point & );
public:
    Point( void ) : m_i(0) {}
    void PrintPrivate( void ){cout << m_i << endl; }

private:
    int m_i;
};

void ChangePrivate ( Point &i ) { i.m_i++; }

int main()
{
    Point sPoint;
    sPoint.PrintPrivate();
    ChangePrivate(sPoint);
    sPoint.PrintPrivate();
}

2.友元类

#include <iostream>

using namespace std;
class YourClass {
    friend class YourOtherClass;  // Declare a friend class
public:
    YourClass() : topSecret(0){}
    void printMember() { cout << topSecret << endl; }
private:
    int topSecret;
};

class YourOtherClass {
public:
    void change( YourClass& yc, int x ){yc.topSecret = x;}
};

int main() {
    YourClass yc1;
    YourOtherClass yoc1;
    yc1.printMember();
    yoc1.change( yc1, 5 );
    yc1.printMember();
}

二.C++局部类和嵌套类

http://www.cppblog.com/mzty/archive/2007/05/24/24766.html

三.operator new

参见这篇文章的内部链接

http://www.cppblog.com/Solstice/archive/2011/02/22/140410.html

http://www.cppblog.com/woaidongmao/archive/2010/06/29/118940.html

四.前置声明和头文件

http://blog.csdn.net/fallStones/archive/2011/03/22/6266632.aspx

五.匿名命名空间

http://blog.sina.com.cn/s/blog_48a4b1680100gdul.html

六.函数指针回调方法

犹如c#的委托

#include <string>
#include <iostream>
using namespace std;

typedef void (*action)(const string &);

void SayHello(const string &name)
{
 cout << "hello,"+name <<endl;
}

int main() 
{
action pf2 = SayHello;
pf2(string("terry"));

return 0;
}

参考:C++ Primer  7.9 指向函数的指针

http://wenku.baidu.com/view/b2b24d37ee06eff9aef80771.html

七.关于c++ 枚举

http://wenku.baidu.com/view/8c28fd4de518964bcf847cce.html

很不习惯,不可以重复,其实就是static常量