g++ -std=c++11 -o test test.cpp
五种常规的变量组织方式加上动态内存操作要求编译器操作三种内存:一块存放静态变量、一块存放自动变量、一块用于动态存储!
定位new运算符
#include <new>
struct chaff{
char array[20];
int val;
};
char buffer1[50];
char buffer2[500];
int main()
{
chaff *p1, *p2;
p1 = new (buffer1) chaff;
p2 = new (buffer2) int[20];
}
在特定的地址new申请内存
//定位new运算符
#include <iostream>
#include <new>
using namespace std;
const int BUF = 512;
const int N = 5;
char buffer[BUF];
int main()
{
double *pd1, *pd2;
pd1 = new double[N]; //常规new,申请的内存地址不定
pd2 = new (buffer) double[N]; //定位new运算符,在buffer的特定中申请内存
for(int i=0; i<N; i++)
pd2[i] = pd1[i] = 2.0*i;
cout<<"heap: "<<pd1<<"static: "<<(void *)buffer<<endl;
cout<<"centants:
";
for(int i=0; i<N; i++)
{
cout<<pd1[i]<<" at "<<&pd1[i]<<";
";
cout<<pd2[i]<<" at "<<&pd2[i]<<";
";
}
double *pd3, *pd4;
pd3 = new double[N];
pd4 = new (buffer) double[N];
for(int i=0; i<N; i++)
pd4[i] = pd3[i] = 4.0*i;
cout<<"heap: "<<pd3<<" static: "<<(void *)buffer<<endl;
cout<<"centants:
";
for(int i=0; i<N; i++)
{
cout<<pd3[i]<<" at "<<&pd1[i]<<";
";
cout<<pd4[i]<<" at "<<&pd2[i]<<";
";
}
delete [] pd1;
pd1=new double[N];
pd2=new (buffer + N*sizeof(double)) double[N];
for(int i=0; i<N; i++)
pd2[i] = pd1[i] = 6.0*i;
cout<<"centants:
";
for(int i=0; i<N; i++)
{
cout<<pd1[i]<<" at "<<&pd1[i]<<";
";
cout<<pd2[i]<<" at "<<&pd2[i]<<";
";
}
//delete只能针对常规new的空间
delete [] pd1;
delete [] pd3;
return 0;
}
命名空间
#include <iostream>
using namespace std;
namespace Test{
int val;
}
double val; //全局变量
int main()
{
using Test::val; //将其添加到局部命名空间中,如果不加这句,以下操作的每一个val都是全局val。
//int val; //前面已经声明了一次,编译器会报告重复声明错误!
cin>>val; //局部val
cin>>::val; //全局val
cout<<"Test::val = "<<val<<endl; //局部val
cout<<"global val = "<<::val<<endl; //全局val
return 0;
}
上面的例子是在函数内部使用using声明把名称添加到局部名称空间,如果在函数外部使用,则将名称添加到全局
#include <iostream>
using namespace std;
namespace Test{
int ival;
double dval;
}
using namespace Test; //using声明必须在命名空间定义之后
//using声明如果被放在某个函数内部而不是所有函数外部,则只有那一个函数可以不通过名称限定符使用名称
//例如:如果放在main中,则print函数必须使用Test::ival和Test::dval限定符形式访问二者!
//还有一点是using namespace没有using Test::ival using Test::dval安全
void print();
int main()
{
cin>>ival;
cin>>dval;
print();
return 0;
}
void print()
{
cout<<"ival = "<<ival<<endl;
cout<<"dval = "<<dval<<endl;
}
asd
//namesp.h
#include <string>
namespace Pers{
struct Person{
std::string fname;
std::string lname;
};
void getPerson(Person &);
void showPerson(const Person &);
}
namespace debts{
using namespace Pers;
struct Debt{
Person name;
double amount;
};
void getDebt(Debt &);
void showDebt(const Debt &);
double sumDebts(const Debt ar[], int n);
}
//namesp.cpp
#include <iostream>
#include "namesp.h"
namespace Pers{
using std::cout;
using std::cin;
void getPerson(Person &rp){
cout<<"enter first name:";
cin>>rp.fname;
cout<<"enter last name:";
cin>>rp.lname;
}
void showPerson(const Person &rp)
{
std::cout<<rp.lname<<", "<<rp.fname;
}
}
namespace debts{
void getDebt(Debt &rd){
getPerson(rd.name);
std::cout<<"Enter debt: ";
std::cin >> rd.amount;
}
void showDebt(const Debt &rd)
{
showPerson(rd.name);
std::cout<<": $"<<rd.amount<<std::endl;
}
double sumDebts(const Debt ar[], int n)
{
double total = 0;
for(int i = 0; i < n; i++)
{
total += ar[i].amount;
}
return total;
}
}
//main.cpp
#include <iostream>
#include "namesp.h"
void other();
void another();
int main()
{
using debts::Debt;
using debts::showDebt;
Debt golf={{"Benny","Goat"}, 120.0};
showDebt(golf);
other();
another();
return 0;
}
void other()
{
using std::cout;
using std::endl;
using namespace debts;
Person dg = {"Doodles", "Glister"};
showPerson(dg);
cout<<endl;
Debt zippy[3];
for(int i=0; i<3; i++)
getDebt(zippy[i]);
for(int i=0; i<3; i++)
showDebt(zippy[i]);
cout<<"total debt: $"<<sumDebts(zippy, 3)<<endl;
}
void another(){
using Pers::Person;
Person collector = {"Milo", "RightShift"};
Pers::showPerson(collector);
std::cout<<std::endl;
}