Rodhos Soft

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

チュートリアル的な記述2

参照

// class
class CPYCost {
    
public:
    CPYCost();
    ~CPYCost();

    /// オブジェクトのコピー用の代入演算子
    CPYCost& operator=(const CPYCost& rhs);
    int x;
    
private:
};
void refAdd(int &x);
void pAdd(int *x);


void refUse() {
    
    // 参照
    int x = 1;
    int& r = x;// xの参照を取得
    r = 2; // xは2になる。
    // 参照はNULLを代入できない。
    
    // ポインタ
    int z = 1;
    int* p = &z;
    *p = 2; // zは2になる。
    
    int t = 10;
    refAdd(t); // tが11になる。
    pAdd(&t); // tが12になる。
    
    CPYCost *c = new CPYCost();
    c->x = 1000;
    
    CPYCost d;
    d.x = 2500;
    CPYCost &dd = d;
    CPYCost ddd = d;
    
    std::cout << ddd.x << std::endl;

    
         
}

void refAdd(int &x) {
    x++;
}

void pAdd(int *x) {
    (*x)++;
}

ラムダ式

void lambdaUse() {
    auto l = [](int x)->int {
        return x+3;
    };
    int x = l(5);
    
    // NG
//    auto l2 = [](int a)->int {
//        return x+10;
//    };
    
    // xをキャプチャ
    auto l2 = [x](int a)->int {
        return x+10;
    };
    
    // すべてキャプチャ
    auto l4 = [=](int a)->int {
        return x+10;
    };

    std::cout << x << std::endl;

}