関数
int twice(int x);
int twice(int x) {
return x * 2;
}
auto nfunc2(int x) -> decltype(x) {
return x * 100;
}
namespace FunctionauUse {
int add(int x);
void echo(int x);
}
void functionalUse() {
std::function<void(int)> f = FunctionauUse::echo;
std::function<void(int)> f2 = FunctionauUse::add;
f(3);
f2(5);
}
void FunctionauUse::echo(int x) {
}
int FunctionauUse::add(int x) {
return x+1;
}
テンプレート
template <typename R, typename T1, typename T2>
R echo(T1 x, T2 y) {
return static_cast<R>(x+y);
}
構造体
struct Point {
int x;
int y;
int d2();
};
int Point::d2() {
return this->x + this->y;
}
クラス
class Man {
public:
void hello();
Man();
Man(std::string name);
~Man();
bool operator==(const Man& rhs) const;
inline bool operator!=(const Man& rhs) const {
return !(*this == rhs);
}
private:
std::string name;
int age;
void util();
int n = 10;
};
void Man::hello() {
std::cout << "Hello, my name is " << this->name << std::endl;
}
void Man::util() {
std::cout << "util" << std::endl;
}
Man::Man() : name("name"), age(0){
}
Man::Man(std::string name) {
this->name = name;
age = 0;
}
Man::~Man() {
std::cout << "dec" << std::endl;
}
void hello(Man *m) {
m->hello();
}
bool Man::operator==(const Man &rhs) const {
return name == rhs.name;
}
void classUse() {
Man *man = new Man();
man->hello();
delete man;
}
テンプレート使用
template <typename T>
class Poi {
public:
void echo(T x);
};
template <typename T>
void Poi<T>::echo(T x) {
std::cout << "echo" << x << std::endl;
}
enum ColorType1 : int {
GREEN,RED,YELLOW
};
enum struct ColorType2 : int {
GREEN,RED,YELLOW
};
enum class ColorType3 : int {
GREEN,RED,YELLOW
};
namespace
namespace spaceA {
double getValue();
namespace specialB {
double getValue();
}
inline namespace Util {
double getUtilValue();
}
}
namespace {
const int myMAX2 = 100;
}
namespace spaceMAX {
const int myMAX = 1000;
}
void nameSpaceUse() {
auto v = getValue();
auto v2 = spaceA::getValue();
auto v3 = spaceA::specialB::getValue();
auto v4 = spaceA::getUtilValue();
using namespace spaceA;
auto v5 = getUtilValue();
namespace B = spaceA::specialB;
auto b = B::getValue();
std::cout << v << std::endl;
std::cout << v2 << std::endl;
std::cout << v3 << std::endl;
std::cout << v4 << std::endl;
auto max = spaceMAX::myMAX;
auto max2 = myMAX2;
std::cout << max << max2 << std::endl;
}
double getValue() {
return 2.0;
}
namespace spaceA {
double getValue() {
return 20.0;
}
namespace specialB {
double getValue() {
return 30;
}
}
inline namespace Util {
double getUtilValue() {
return 50;
}
}
}