厄介な質問があります。それは不可能だと思いますが、確かに知る必要があります。少し奇妙な要求ですが、親クラスからの同じピクセルベクトルを共有するために子クラスが必要です。
基本的に、Imageクラスのインスタンスを作成したいと思います。そのImageクラスはビットマップとPngクラスの両方のピクセルを保持するため、ビットマップからPNGに、またはその逆に変換する必要がある場合、ビットマップとPNGクラスの両方を作成するのではなく、同じベクトルを使用します。
class Image
{
private:
std::vector<RGB> Pixels;
};
class Bitmap : Image
{
public:
Bitmap() : Image() {};
};
class Png : Image
{
public:
Png() : Image() {};
};
私がするときそのような:
int main()
{
Image Img();
Img.GetBitmapPixels(); //This
Img.GetPngPixels(); //And this, return the same Pixels Vector.
Bitmap Foo = Img.ToPng();
Png Moo = Img.ToBitmap();
//Such that both Foo and Moo have the exact same underlying Pixels Vector.
}
現在、私のクラスは次のようになっています。
class Bitmap
{
private:
std::vector<RGB> Pixels;
public:
Bitmap();
std::vector<RGB> GetPixels() {return Pixels;}
void SetPixels(std::vector<RGB> Pixels) {this->Pixels = Pixels;}
};
class Png
{
private:
std::vector<RGB> Pixels;
public:
Png();
std::vector<RGB> GetPixels() {return Pixels;}
void SetPixels(std::vector<RGB> Pixels) {this->Pixels = Pixels;}
};
そして、一方から他方に変換するには、次のことを行う必要があります。
int main()
{
Bitmap Bmp();
Png PNG();
PNG.SetPixels(BMP.GetPixels); //BMP has to COPY PNG's Pixels and vice-versa..
}
それは一種のばかげた質問です。ピクセルをコピーしたくないだけです。両方のクラスがstd::vector Pixelsメンバーを保持し、データが同じように配置されるため、コピーせずに2つのクラス間で変換できるようにしたいだけです。
私はできるようにしようとしていると思います:PNG.SaveAsBitmap(...); またはBMP.SaveAsPNG(...); 他の新しいインスタンスを作成せずに。
変換先の他のクラスの新しいインスタンスをコピー/作成して作成することを回避するにはどうすればよいですか?相互に継承するクラスを持つことはできますか?