Rodhos Soft

備忘録を兼ねた技術的なメモです。Rofhos SoftではiOSアプリ開発を中心としてAndroid, Webサービス等の開発を承っております。まずはご相談下さい。

チュートリアル的な記述

関数

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
class Man {
    
public:
    void hello();
    
    Man(); // default constructer
    Man(std::string name);
    
    ~Man();
    
    //    static int commonNumber;
    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){
    //    commonNumber++;
}

Man::Man(std::string name) {
    this->name = name;
    age = 0;
    //    commonNumber++;
}

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

enum ColorType1 : int {
    GREEN,RED,YELLOW
};

enum struct ColorType2 : int {
    GREEN,RED,YELLOW
};

/// enum structとenum class は同じ
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();
    
    /// alias
    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;
        }
    }
    
}