作用于限定符 ::
名称空间
名称空间是基于某个文件下的作用域,可以直接配合作用域限定符使用,也可以用using
#include <iostream>
//using namespace std;
int a = 2;
namespace space{
int a = 3;
int b = 4;
}
using namespace space;
int main()
{
std::cout << "Hello World" << std::endl;
int a = 1;
std::cout << a << std::endl; //1
//匿名名称空间
std::cout << ::a << std::endl; //2
std::cout << space::a << std::endl; //3
std::cout << space::b << std::endl; //4
std::cout << b << std::endl; //4
return 0;
}
std是标准名称空间,绝大多数C++标准库中的类型都会在此名称空间下
this指针
this指针指针在类内部,是一种特殊的指针,指向这个类在外部的对象
class Test
{
privatr:
string s = "ABCDE";
public:
void fun1()
{
cout << this.s << endl;//编译器自动添加this指针调用
}
void fun2()
{
this->fun1();//编译器自动添加this指针调用
}
};
1.在类内调用成员时,编译器会自动添加this指针,因为成员必须通过对象来调用
2.区分重名的局部变量与成员变量
#include <iostream>
using namespace std;
class Test
{
private:
string s = "ABC";
public:
Test(string s)
{
// 区分重名的局部变量与成员变量
this->s = s;
}
string get_s()
{
return s;
}
};
int main()
{
Test t("aaa");
cout << t.get_s() << endl;
return 0;
}
3. *this表示类的对象本身,通常作为返回值来支持链式调用
但当遇到这种情况时,返回值必须是当前类的引用
class Value
{
private:
double value;
public:
Value(double value):value(value){}
double get_value()
{
return value;
}
Value& add(double d)
{
value += d;
return *this;
}
};
int main()
{
Value v(1.222);
// 链式调用
v.add(2).add(3).add(-4);
cout << v.get_value() << endl; // v+2+3+-4=2.222
return 0;
}
static关键字
静态局部变量
使用static修饰的局部变量就是静态局部变量。静态局部变量第一次调用时,会开辟空间,后面每次调用都使用这一份变量,即这个类所有对象共用这一个变量
普通的局部变量每次调用都会生成一个新的临时变量,在所在函数执行完成后销毁,不同对象使用的对象并不是同一份
class Test
{
public:
void method1()
{
static int a = 1;//静态局部变量
cout << a++ << endl;
}
void method2()
{
int a = 1;//静态变量
cout << a++ << endl;
}
};
int main()
{
Test t1;
t1.method1();//第一次调用时还是1
t1.method2();//1
Test t2;
t2.method1();//第二次调用因为经过第一次的'++',地址中变量变为2
t2.method2();1
//后面继续定义t3,t4会使method1一直++
return 0;
}
静态成员变量
使用static关键字修饰成员变量就是静态成员变量。非const的静态成员变量必须类内声明,类外初始化
一个类所有对象共用一份静态成员变量,静态变量可直接使用类名直接调用
与静态局部变量区别:静态成员变量在程序运行时就开辟空间,哪怕类中没有对象
//定义局部变量
class Test2
{
public:
int a = 1;//非静态成员变量
static int b;//静态成员变量,类内声明
};
//类外初始化
int Test2::b = 1;
main函数中:
// 没有任何一个对象也能调用静态成员变量
cout << Test2::b << endl;//1
Test2 s1;
cout << s1.a++ << endl; // 1
cout << s1.b++ << endl; // 1
Test2 s2;
cout << s2.a++ << endl; // 1
cout << s2.b++ << endl; // 2
//作用域限定符调用静态成员更为简便
cout << Test2::b << endl;
静态成员函数
使用static修饰的普通成员函数就是静态成员函数
在同一个类中,静态成员函数无法访问非静态成员(包括成员变量与成员函数),只能访问静态成员,因为非静态成员需要使用this调用
这说明静态成员函数是没有this指针的,但是成员函数可以访问静态成员
如果静态成员函数与定义分离,static关键字只需修饰声明处即可
class Test3
{
public:
string s1 = "非静态成员";
static string s2;
void func1()
{
cout << s1 << endl;
cout << s2 << endl;
cout << "非静态成员函数" << endl;
}
static void func2()
{
cout << s2 << endl;//只能调用静态成员,也就是只能调用s2
cout << "静态成员函数" << endl;
}
static void func3();
};
string Test3::s2 = "静态成员";
void Test3::func3()
{
cout << "静态成员函数" endl;
}
静态成员函数可以通过对象访问,但类名访问更简易
设计模式-单例模式
(这里对初学者作用不大,可以暂时忽略)
设计模式是一套被反复使用、多数人知晓、经过分类的代码设计经验
其中单例模式是设计模式中最基础的模式之一,此模式主要靠static关键字来实现。
单例模式即单实例模式,用这种模式设计的类可以保证在整个项目下只有一个对象
static加指针 例:
#include <iostream>
using namespace std;
class Singleton
{
// 屏蔽构造函数
private:
Singleton(){}
~Singleton(){}
Singleton(const Singleton&){}
static Singleton* instance;
public:
static Singleton* get_instance() // 获得单实例对象
{
if(instance == NULL)
instance = new Singleton;
return instance;
}
};
Singleton* Singleton::instance = NULL;
int main()
{
Singleton* s1 = Singleton::get_instance();
Singleton* s2 = Singleton::get_instance();
cout << s1 << " " << s2 << endl;
return 0;
}
引用加static关键字 例:
#include <iostream>
using namespace std;
class Singleton
{
// 屏蔽构造函数
private:
Singleton(){}
~Singleton(){}
Singleton(const Singleton&){}
public:
static Singleton& get_instance()
{
// 一个栈内存的静态局部变量对象
static Singleton instance;
return instance;
}
};
int main()
{
Singleton& s1 = Singleton::get_instance();
Singleton& s2 = Singleton::get_instance();
cout << &s1 << " " << &s2 << endl;
return 0;
}
如果哪里有错误欢迎指出,谢谢(*^__^*)