揮発性のメモ2

http://d.hatena.ne.jp/iww/

デフォルトコンストラクタが呼ばれない

#include <iostream>
using namespace std;

class hoge {
public:
    hoge(){ 
        cout << "hello";
    }
    void run(char *text){
        cout << text;
    }
};

class piyo {
public:
    piyo(int a){ 
        cout << a << "world";
    }
    void run(char *text){
        cout << text;
    }
};

int main() {
    hoge a();       // helloが出ない
//  a.run("unko");  // ★aが無いというコンパイルエラー

    hoge b;         // helloが出る
    b.run("unko");  // unkoも出る
    
    piyo c(9);      // 9worldが出る
    c.run("unko");  // unkoも出る

    return 0;
}

()をつけるとデフォルトコンストラクタが呼ばれない。というか、aという変数はつくられない。
よく分からない仕様