0

ポインタと値の変換と割り当てについてアドバイスをお願いします。

私はこの修正クラス定義を持っています:(gsoapによって生成されます)

class LibClass
{
public:
    std::string *email;    // pointer 
    int landId;                // no pointer
    // (....) much more
}

別の関数で、データベース(informix)から上記のクラスメンバーにデータを割り当てます。

(...) // just database connection
// The following is informix interface related stuff
ITRow *row;
// define one time this conversions interface
ITConversions *c;
// placeholder for all string types, temporary data container
ITString its("");
ITString colname;

// read result from executed query 
while ( row = query.NextRow() ) {
    LibClass *ki = new LibClass;

     ki->email      = new (string);
     //ki->landId   = new (int);       // obviously : error: invalid conversion from 'int*' to 'int'

    // I tried : 
    // invent a new instance 
    LibClass rki;

     //rki = &ki;
     //rki.x        = 9;

     // this is okay but how to bring ki and rki together,
     int *a = new int;
     rki.x          = *a;

    // And for understanding, here's what comes next -  It seams as i have to hardcode all 30 DB fields... but thats okay for now.

    colname="email";
    ITValue *v = row->Column( colname );
    v->QueryInterface(ITConversionsIID, (void **) &c);
    c->ConvertTo( its );
    *( ki->email ) = string(its.Data());                   // THE DATA TRANSFER - assignment
    v->Release();

   } // while end

編集私はこれを続けることができなかったので、提案を承認することはできませんが、ここで閉じたいので、最も詳細な回答を受け入れます。thxすべて。

4

3 に答える 3

1

これが私がとるようなアプローチです。

...
// read result from executed query 
while ( row = query.NextRow() ) {
    LibClass *ki = new LibClass;

     ki->email      = new (string); //Needed to create storage
     // That's all the setup you need on your object

     //Here's what I'd do differently
     ki->email = get_column_data(ki->email, "email");
     ki->landId = get_column_data(ki->landId, "landId");
     ...
}

template <typename T> 
void get_column_data(T target, string column_name){
    //Code to grab column data based on target type and column name
}
...
于 2011-01-17T15:44:52.083 に答える
1

rki と ki で何をしているのかを正確に理解するのは難しいですが、

rki = *ki // (1)

それ以外の

rki = &ki // (2)

行 (1) は、クラス インスタンスへのポインターを逆参照し、クラス インスタンスを残します。

行 (2) は、クラス インスタンスへのポインターへのポインターを提供しますが、rki は型 (LibClass **) ではありません。

于 2011-01-17T15:46:36.560 に答える
0

まず第一に、正当std::stringな理由がない限り、LibClass は a へのポインタを持つべきではありません。 std::string内部ですべての割り当てを処理します。

class LibClass
{
public:
    std::string email;         // shouldn't be a pointer 
    int landId;                // no pointer
    // (....) much more
}

そして、whileループ内で初期化する必要がなくなり、必要なemail値を割り当てることができますlandId

LibClass ki;
// ki.email is initialized to "" already
ki.landId = 9;

// ... other code ....
ki.email = string(its.Data());

または、LibClassが何らかの理由で割り当てられたヒープでなければならない場合(つまり、関数から渡す必要がある場合):

LibClass *ki = new LibClass();
// ki->email is initialized to "" already
ki->landId = 9;

// ... other code ....
ki->email = string(its.Data());

// IMPORTANT: somewhere else in your program you muse delete the allocated space
delete ki;
于 2011-01-17T15:58:58.203 に答える