C++のコンストラクタ

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
アバター
みけCAT
記事: 6734
登録日時: 14年前
住所: 千葉県
連絡を取る:

C++のコンストラクタ

#1

投稿記事 by みけCAT » 12年前

IdeoneでC++の実験をしていて、気になったことがあるので質問します。

コード:

#include <cstdio>

class test {
    public:
        test();
        test(int a);
        test(int a,int b);
};

test::test() {
    puts("test()");
}

test::test(int a) {
    puts("test(int a)");
}

test::test(int a,int b) {
    puts("test(int a,int b)");
}

int main(void) {
    test a();
    test b(123);
    test c(123,456);
    return 0;
}
このコードを実行すると、

コード:

test(int a)
test(int a,int b)
と出力されました。
すなわち、引数が0個のコンストラクタは呼ばれていないということです。

また、

コード:

#include <cstdio>

class test {
    public:
        test();
        test(int a);
        test(int a,int b);
};

test::test() {
    puts("test()");
}

test::test(int a) {
    puts("test(int a)");
}

test::test(int a,int b) {
    puts("test(int a,int b)");
}

int main(void) {
    test a=test();
    test b=test(123);
    test c=test(123,456);
    return 0;
}
このコードを実行すると、

コード:

test()
test(int a)
test(int a,int b)
と表示され、きちんと引数が0個のコンストラクタも呼ばれていることがわかります。

なぜ最初の書き方では、引数が0個のコンストラクタは呼ばれないのでしょうか?

[hr]
という質問文を書きながらさらに実験をしたところ、つぎのことがわかりました。

コード:

#include <cstdio>

class test {
    public:
        test();
        test(int a);
        test(int a,int b);
        void mikisin();
};

test::test() {
    puts("test()");
}

test::test(int a) {
    puts("test(int a)");
}

test::test(int a,int b) {
    puts("test(int a,int b)");
}

void test::mikisin() {
    puts("Hizikata mikisin");
}

int main(void) {
    test a();
    test b(123);
    test c(123,456);
    a.mikisin();
    return 0;
}
このコードを実行しようとしたところ、

コード:

prog.cpp: In function ‘int main()’:
prog.cpp:31: error: request for member ‘mikisin’ in ‘a’, which is of non-class type ‘test ()()’
というエラーが出ました。
すなわち、

コード:

test a();
は何らかの別の型として認識されていると考えられます。
もしかして、これは関数ポインタになっているということでしょうか?
‘test ()()’とは何でしょうか?
複雑な問題?マシンの性能を上げてOpenMPで殴ればいい!(死亡フラグ)

アバター
h2so5
副管理人
記事: 2212
登録日時: 14年前
住所: 東京
連絡を取る:

Re: C++のコンストラクタ

#2

投稿記事 by h2so5 » 12年前

コード:

test a();
はtest型の値を返し引数を取らない関数aのプロトタイプ宣言として解釈されるようです。

アバター
みけCAT
記事: 6734
登録日時: 14年前
住所: 千葉県
連絡を取る:

Re: C++のコンストラクタ

#3

投稿記事 by みけCAT » 12年前

なるほど。ありがとうございます。
引き続き「‘test ()()’とは何でしょうか?」
複雑な問題?マシンの性能を上げてOpenMPで殴ればいい!(死亡フラグ)

beatle
記事: 1281
登録日時: 13年前
住所: 埼玉
連絡を取る:

Re: C++のコンストラクタ

#4

投稿記事 by beatle » 12年前

aが関数名だと解釈されたため、a.mikisin()が無効なので変なエラーになっています。
aは関数名、つまり式の中でのaの型が
test ()()
です。

ちなみに、Aを返し、Bを引数に取る関数へのポインタpは次のように宣言します。

コード:

A (*p)(B);

とっち
記事: 56
登録日時: 13年前
住所: 岡山

Re: C++のコンストラクタ

#5

投稿記事 by とっち » 12年前

私も興味をもったのでいろいろ実験したところ、
以下のコードは実行できました。

コード:

#include <iostream>
using namespace std;

class CTest{
public:
    void print(){cout<<"start"<<endl;}
};

int main(void){
    CTest a();
    a().print();
    cout<<"end"<<endl;
}

CTest a(){
    CTest t;
    cout<<"a"<<endl;
    return t;
}
実行結果は
a
start
end

となりました。

アバター
みけCAT
記事: 6734
登録日時: 14年前
住所: 千葉県
連絡を取る:

Re: C++のコンストラクタ

#6

投稿記事 by みけCAT » 12年前

なるほど。
ありがとうございました。
複雑な問題?マシンの性能を上げてOpenMPで殴ればいい!(死亡フラグ)

閉鎖

“C言語何でも質問掲示板” へ戻る