競馬鹿ねっと
 
since
2000.1.1〜
ヤエノムテキ
http://www.keibaka.net
メールはこちら メールはアイコンをクリック!

競馬
 ┣ トップページ
 ┣ 開発日誌とか♪
 ┗ 熱い競馬漫画
  SouKaSILa
  NKV

 
Developer
 ◆ 過去の遺物
 ◆ KBN_IPAT5
 ◆ KBN_IPAT4 08/06/01
 ◆ PHP5
 ┣ PHPの優位性
 ┣ ガベージコレクタ
 ┣ 値渡しと参照渡し
 ┃┣ Cサンプル
 ┃┗ PHPサンプル
 ◆ オープンソース
 ┣ KBN_AIBO
 ┃┗ document
 ┗ IIS_AUTH_ADO
 ◆ 無償サポート
 
その他
 ◆ 映画とか 06/05/10
 ◆ 北斗の拳 06/04/04
 
PC
 ◆ SOTEC WL7160A
 
ウォッチリンク
 ◆ スラッシュドット
 ◆ ボトムズ
 
利用させて頂いた順に並んでいます。
XREA
競馬鹿ねっとはXREAを利用して運営されています。
Yuko's Gallery
Yuko's Gallery様のフリー画像を利用させて頂いてます。
Aomusi'sWorld
Aomusi'sWorld様のフリー画像を利用させて頂いてます。
 
PHP参考用Cソース 最終更新日:2005.12.10



  eclipse+CDT+MinGWの環境を作成してg++でコンパイルしました。

test.h
#ifndef TEST_H_
    
#define TEST_H_

#define BUF_SIZE_ 100

class testClass
{
public:
//construct
    testClass(char*);
    testClass(testClass&);
//destruct
    ~testClass();
//operator
    testClass& operator = (const testClass&);
    testClass& operator == (const testClass&);
//getter
    char*    get_name(void);
    char*    get_value(void);
//setter
    void    set_value(char*);
private:
    char*    _name;
    char*    _cp_value;
    char*    _pt_value;
};

//construct
testClass::testClass(char* value)
{
    //name
    this->_name = new char[BUF_SIZE_];
    strcpy(this->_name, value);

    //cp_value
    this->_cp_value = new char[BUF_SIZE_];
    strcpy(this->_cp_value, value);
//    *this->_cp_value = '\0';
    
    //pt_value
    this->_pt_value = new char[BUF_SIZE_];
    strcpy(this->_pt_value, value);

    //message out
    printf("construct: %s\n", this->_name);
}
//copy construct
testClass::testClass(testClass& src_obj)
{
    //コピー
    //PHPではオブジェクト型以外はコピーされる
    //name
    this->_name = new char[BUF_SIZE_];
    strcpy(this->_name, src_obj._name);
        
    //cp_value
    this->_cp_value = new char[BUF_SIZE_];
    strcpy(this->_cp_value, src_obj._cp_value);
    
    
    //アドレスを参照する
    //PHPではオブジェクト型は参照される
    //pt_value
    this->_pt_value = src_obj._pt_value;

    //message out
    printf("copy construct: %s\n", this->_name);
}

//destruct
testClass::~testClass()
{
    //message out
    printf("destruct: %s [%s:%s]\n", this->_name, this->_cp_value, this->_pt_value);
    delete [] this->_name;
    delete [] this->_cp_value;
    delete [] this->_pt_value;
}

//operator
testClass& testClass::operator = (const testClass& src_obj)
{
    //コピーする演算子として作成
    strcpy(this->_name, src_obj._name);
    strcpy(this->_cp_value, src_obj._cp_value);
    strcpy(this->_pt_value, src_obj._pt_value);

    return *this;
}
testClass& testClass::operator == (const testClass& src_obj)
{
    //アドレスを参照する演算子として作成
    
    //name
    if (this->_name != NULL)
        delete [] this->_name;
    this->_name = src_obj._name;
    
    //cp_value
    if (this->_cp_value != NULL)
        delete [] this->_cp_value;
    this->_cp_value = src_obj._cp_value;
    
    //pt_value
    if (this->_pt_value != NULL)
        delete [] this->_pt_value;
    this->_pt_value = src_obj._pt_value;

    return *this;
}

//getter
char* testClass::get_name(void)
{
    return this->_name;
}
char* testClass::get_value(void)
{
    //copy:point書式で返す
    char* result = new char [BUF_SIZE_ * 3];
    sprintf(result, "%s:%s", this->_cp_value, this->_pt_value);
    
    return result;
}

//setter
void testClass::set_value(char* value)
{
    printf("set before =====> %s <= %s\n", this->get_value(), value);
    
    strcpy(this->_cp_value, value);
    strcpy(this->_pt_value, value);
    
    printf("set after =====> %s\n", this->get_value());
}

#endif /*TEST_H_*/

test.cpp
#include <stdio.h>
#include <string.h>
#include "test.h"

//宣言部
void cp_func(testClass, char*);
void pt_func(testClass&, char*);

int main(int argc, char *argv[])
{
    testClass cpBase("cpBase");
    testClass ptBase("ptBase");
    testClass* cpBase2 = new testClass("cpBase2");
    testClass* ptBase2 = new testClass("ptBase2");
    printf("\n");
    
    //コピーのテスト
    printf("copy start\n");
    printf("----------------------------------------\n");
    printf("cpBase->_value =>%s\n", cpBase.get_value());
    cp_func(cpBase, "cpBase_hoge");
    printf("cpBase->_value =>%s\n\n", cpBase.get_value());
    
    //参照のテスト
    printf("pointer start\n");
    printf("----------------------------------------\n");
    printf("ptBase->_value =>%s\n", ptBase.get_value());
    pt_func(ptBase, "ptBase_hoge");
    printf("ptBase->_value =>%s\n\n", ptBase.get_value());
    
    //コピーのテスト2
    printf("copy2 start\n");
    printf("----------------------------------------\n");
    printf("cpBase2->_value =>%s\n", cpBase2->get_value());
    cp_func(*cpBase2, "cpBase2_hoge");
    printf("cpBase2->_value =>%s\n\n", cpBase2->get_value());

    //参照のテスト2
    printf("pointer2 start\n");
    printf("----------------------------------------\n");
    printf("ptBase2->_value =>%s\n", ptBase2->get_value());
    pt_func(*ptBase2, "ptBase2_hoge");
    printf("ptBase2->_value =>%s\n\n", ptBase2->get_value());
    
    //プログラム終了
    printf("destruct cpBase2\n");
    delete cpBase2;
    printf("destruct ptBase2\n");
    delete ptBase2;
    printf("destruct other!!\n");
    
    return 0;
}

//■ コピー用関数
void cp_func(testClass obj, char* value)
{
    obj.set_value(value);
}

//■ 参照用関数
void pt_func(testClass& obj, char* value)
{
    obj.set_value(value);
}

実行結果と説明
↓コピー参照元となるオブジェクトを4つ準備している
construct: cpBase ローカル変数
construct: ptBase 上に同じ
construct: cpBase2 newしてるのでdeleteで明示的にコンストラクタを呼べる
construct: ptBase2 上に同じ

copy start ・・・パターン1 コピー
----------------------------------------
cpBase->_value =>cpBase:cpBase コピー前は名前がセットされている
copy construct: cpBase コピーなので関数に渡った時にコピーコンストラクタが実行
set before =====> cpBase:cpBase <= cpBase_hoge
set after =====> cpBase_hoge:cpBase_hoge
destruct: cpBase [cpBase_hoge:cpBase_hoge] ローカルのコピー変数が不要な時点でデストラクタ
cpBase->_value =>cpBase:@MO 参照(pt_value)側は↑のデストラクタで既に開放されてしまっている

pointer start ・・・パターン2 参照
----------------------------------------
ptBase->_value =>ptBase:ptBase
set before =====> ptBase:ptBase <= ptBase_hoge 参照なので関数に渡った時にコピーコンストラクタは動作しない
set after =====> ptBase_hoge:ptBase_hoge
ptBase->_value =>ptBase_hoge:ptBase_hoge 参照なのでローカルの参照変数が不要でもデストラクタは動作しない

copy2 start ・・・パターン3 コピー
----------------------------------------
cpBase2->_value =>cpBase2:cpBase2
copy construct: cpBase2 コピーなのでコピーコンストラクタ
set before =====> cpBase2:cpBase2 <= cpBase2_hoge
set after =====> cpBase2_hoge:cpBase2_hoge
destruct: cpBase2 [cpBase2_hoge:cpBase2_hoge] コピーなのでデストラクタ
cpBase2->_value =>cpBase2:・O ↑デストラクタで参照(pt_value)は開放済み

pointer2 start ・・・パターン4 参照
----------------------------------------
ptBase2->_value =>ptBase2:ptBase2
set before =====> ptBase2:ptBase2 <= ptBase2_hoge
set after =====> ptBase2_hoge:ptBase2_hoge
ptBase2->_value =>ptBase2_hoge:ptBase2_hoge

destruct cpBase2 cpBase2をこれからdeleteします
destruct: cpBase2 [cpBase2:クMO] pt_valueは既にデストラクタで開放済み
destruct ptBase2 ptBase2をこれからdeleteします
destruct: ptBase2 [ptBase2_hoge:ptBase2_hoge] 参照なので値は保持されたまま
destruct other!! プログラムはここまで、ローカル変数は以降でデストラクタが実行される
destruct: ptBase [ptBase_hoge:ptBase_hoge]
destruct: cpBase [cpBase:@MO] pt_valueは既にデストラクタで開放済み



Copyright 競馬鹿ねっと