0

基本クラスのコピーを取得し、基本クラスと同じアドレス空間を参照するサブクラスのインスタンスを作成する方法を見つけようとしています。

たとえば、基本クラスに多数のメンバーがFooあり、サブクラスにいくつかの追加メンバーがありBarます。で変更しても で変更されるFooようなものからバーを作成するにはどうすればよいですか。xBarxFoo

元)

struct Foo{
    int x;
    Foo(){
        x = 0;
    }
}

struct Bar : Foo{
    int z;
    //?? what to do here
}

int main(){
   Foo foo();
   Bar bar(foo); //??
   bar.x = 7;
   assert(bar.x == foo.x);
}

私はこれが奇妙な質問であることを知っています、そして私はそれをうまく表現していません. 誰かが答えを知っている場合、または私がばかげていて、これを見つけることができないという答えがstackoverflowにある場合、私は非常に感謝しています。お時間をいただきありがとうございます。

4

4 に答える 4

1

Bar を初期化すると、現在のコードは Foo の新しいコピーを作成します。これは、Bar の構造内に個別の値の独自のセットが格納されていることを意味します。Foo と Bar が値を共有できるようにするには、値をポインターまたは参照に変更する必要があります。また、引数として Foo を取る Bar のコンストラクターを指定する必要があります。

X の値がグローバルとして格納されているため、明らかに理想的ではありません。最終的には、それを格納する場所が必要になり、正確な場所はニーズによって異なります。

int x_storage;

struct Foo{
    int& x;
    Foo(): x(x_storage){
        x = 0;
    }
};

struct Bar : Foo{
    int z;
    Bar(Foo& f)
    {
        x = f.x;
    }
};

int main(){
   Foo f = Foo();
   Bar b = Bar(f); //??
   b.x = 7;
   assert(b.x == f.x);
};

編集:あなたのコメントから判断すると、おそらく inherentence はあなたが何をしているのかではないかもしれません.

struct Foo{
    int x;
    Foo(){
        x = 0;
    }
};

struct Bar{
    Foo& myFoo;
    int z;
    Bar(Foo& f): myFoo(f){
    }
};

int main(){
   Foo f = Foo();
   Bar b = Bar(f); //??
   b.myFoo.x = 7;
   assert(b.myFoo.x == f.x);
};
于 2013-01-23T23:14:32.283 に答える
1

まだ誰も言及していないもう 1 つのオプションですが、基本クラスとして Foo を持つすべてのオブジェクトに対して単一の変数のみをメモリに割り当てたい場合は、静的メンバー変数を使用できます。

struct Foo{
    static int x;
    Foo() {
        x = 0;
    }
};

int Foo::x = 0;

struct Bar : Foo {
    int z;
};

int main(){
   Foo foo();
   Bar bar();
   bar.x = 7;
   assert(&bar.x == &foo.x); // now using the same memory address
}

そして、メモとして、 static キーワードは C++ 言語で多くの意味を持っているので、慣れていない場合は残りを調べることをお勧めします (これは、スターターとしてC++ static キーワードに関する msdn ページ msdn ページです) 私は言うことができません私は static キーワードのすべての意味を頻繁に使用しますが (特に関数スコープの static 変数)、正しい状況が発生した場合にツール ボックスに入れておくと便利なツールです。

于 2013-01-24T02:32:56.650 に答える
1

そうですね、オブジェクトを作成する能力もあると仮定します。

class Foo  // Base class. 
{
   public:
     int x;
};


class Bar
{
   public:
     int z;
}

いくつかの機能:

void frobb(Foo *fooPtr)
{
    if(fooPtr->x != 7)
    {
        cout << "Bad value of x\n"; 
    }
    ... 
}

....

// some of your code (in a different file probably.

Bar b;
... do stuff with b. 
b.x = 7;
frobb(&b);

API に何か問題がある場合を除き、Foo から Bar を作成したり、Bar から Foo を作成したりする必要はありません。それはちょうど悪い設計です。

編集:

コメントに記載されている場合:

一部のコードは Bar オブジェクトを作成します。

Bar b;

extern void some_generic_api(Foo *fptr);

... do stuff with b, including setting your own variables. 

b.x = 7;
some_generic_api(&b);   // WOrks like a "Foo" object without problem. 

... some other bit of code ... 

void frobb(Foo *fptr)
{
    // Note: Don't do this unless you are SURE it's a Bar object you actually have! 
    Bar *bptr = reinterpret_cast<Bar*>(fptr);   

    .. do stuff that requires Bar object using bptr;
    bptr->x = 19; 

    some_generic_api(bptr);   // This will work fine. 
}
于 2013-01-23T23:15:20.550 に答える
1

これにどのようにアプローチしたいか、どのようにアプローチできるかに応じて、2 つのオプションがあります。

1) 間接性

struct Bar
{
private:
    Bar() = delete; //eradicate the default constructor
public:
    //Foo member references
    int &x;

    //Bar members
    int z;

    Bar(Foo& f): x(f.x) { } //of course, you may initialize z as well
};

使用法:

Foo foo_obj;

//new scope begins (can be a function call, for example)
{
    Bar bar_obj(foo_obj);

    //do stuff with bar_obj
    //...
    //work done

} //scope ends; bar_obj is destroyed

//magic! foo_obj has been updated with the correct values all this time

2) ポリモーフィズム

struct Bar: public Foo
{        
    //Bar members
    int z;

    Bar(): Foo() { }
};

使用法:

Foo foo_obj;

//new scope begins (can be a function call, for example)
{
    Bar bar_obj;
    static_cast<Foo&>(bar_obj) = foo_obj; //we'll use Foo's default (or user-defined) assignment operator
    //note that you need to cast to reference

    //do stuff with bar_obj
    //...
    //work done

    foo_obj = bar_obj; //you will need to copy your data back at this point
    //also note foo_obj has not been updated during this time, which may not be desirable
} //scope ends
于 2013-01-23T23:09:29.027 に答える