c++特性实例
02 Jan 2015
|
宏
__FILE__ 宏在预编译的时候替换为当前文件的源文件名
__LINE__ 宏在预编译的时候替换为当前的行号
__FUNCTION__ 宏在预编译的时候替换为当前的函数名称
命名空间
#include <iostream>
#include <string>
using namespace std;
namespace pbase {
class Hello {
public:
void print(){cout << "hello" << endl;}
};
} //namespace pbase
int main()
{
using namespace pbase;
Hello h;
h.print();
}
c++11可变参数
#include <iostream>
template<typename T>
void print_args(T t)
{
std::cout << t;
}
template<typename T, typename... Rest>
void print_args(T t, Rest... rest)
{
print_args(t);
print_args(rest...);
std::cout << std::endl;
}
int main()
{
print_args("abc:", "123");
}
静态编译
//log.h
#include <iostream>
#include <string>
using namespace std;
namespace pbase {
class Logger {
public:
Logger();
private:
string filename;
};
} //namespace pbase
//log.cpp
#include "log.h"
namespace pbase {
Logger::Logger() {
filename = "a.log";
cout << "log file:" << filename << endl;
}
} //namespace pbase
//testlog.cpp
#include "log.h"
#include <iostream>
int main()
{
pbase::Logger l;
return 0;
}
# g++ -c log.cpp
# ar crv libpbase.a log.o
# g++ -o testlog testlog.cpp libpbase.a
# ./testlog
log file:a.log