用C ++定义ref类

时间:2013-06-20 15:41:16

标签: class header c++-cli

我在我的代码中创建了一个名为Picture的代码,类里面有Bitmap^ Sytem::String path等属性。我想创建一个新属性来指示图片的形状。

我想要三个不同的形状类,一个是正方形,一个是水平矩形,最后一个是垂直矩形。通过查看BitMap像素的宽度和高度,我希望能够更改my Picture的属性以匹配三个类中的一个。问题是我不知道我会怎么做。举个例子,假设我有这个矩形:

 _____________________
|                     |
|                     |
|_____________________|

Picture->TemplateType = HORIZONTALRECTANGLE

到目前为止,我的图片类看起来像这样:

    public ref class Picture{
public: 
    System::String^ path;   
    BitMap^ image;  
    PosOnSlide *PositionAtributes;
    bool EmptyPic;
    bool PlacedOnSlide; 
};

我总是可以这样做

    public ref class Picture{
public: 
    System::String^ path;   
    BitMap^ image;  
    PosOnSlide *PositionAtributes;
    bool EmptyPic;
    bool PlacedOnSlide;
            int TemplateType // 0 = square, 1 = vertical, 2 = horrizontal   
};

但我认为代码维护更容易#define一个类或枚举,或者具有三种不同属性的东西。

我有什么选择?

1 个答案:

答案 0 :(得分:1)

是的,您使用枚举是正确的,这可能是一个简单的解决方案:

enum class PictureType
{
    Square,
    HorizontalRectangle,
    VerticalRectangle
};

public ref class Picture
{
public: 
    System::String^ path;   
    BitMap^ image;  
    PosOnSlide *PositionAtributes;
    bool EmptyPic;
    bool PlacedOnSlide;
    PictureType Type;
};

int main(array<System::String ^> ^args)
{
    Picture^ picture = gcnew Picture();
    picture->Type = PictureType::Square;

    return 0;
}

但您可能希望分离您的类型并根据位图属性生成不同的实例

public ref class Picture
{
public: 
    System::String^ path;   
    BitMap^ image;  
    PosOnSlide *PositionAtributes;
    bool EmptyPic;
    bool PlacedOnSlide;
    Picture(System::String^ path, BitMap^ bitmap)
    {
        this->path = path;
        this->image = bitmap;
    }
};

public ref class Square : Picture
{
public:
    Square(System::String^ path, BitMap^ bitmap)
        : Picture(path, bitmap)
    {
    }
};

public ref class HorizontalRectangle : Picture
{
public:
    HorizontalRectangle(System::String^ path, BitMap^ bitmap)
        : Picture(path, bitmap)
    {
    }
};

public ref class VerticalRectangle : Picture
{
public:
    VerticalRectangle(System::String^ path, BitMap^ bitmap)
        : Picture(path, bitmap)
    {
    }
};

public ref class PictureFactory
{
public:
    static Picture^ GetPicture(System::String^ path, BitMap^ bitmap)
    {
        Picture^ picture = nullptr;
        if (bitmap->Height == bitmap->Width)
        {
            picture = gcnew Square(path, bitmap);
        }
        else if (bitmap->Height < bitmap->Width)
        {
            picture = gcnew HorizontalRectangle(path, bitmap);
        }
        else if (bitmap->Height > bitmap->Width)
        {
            picture = gcnew VerticalRectangle(path, bitmap);
        }

        return picture;
    }
};

int main(array<System::String ^> ^args)
{
    Picture^ square = PictureFactory::GetPicture("image.jpg", gcnew BitMap(100, 100));
    Picture^ hrect = PictureFactory::GetPicture("image.jpg", gcnew BitMap(100, 10));
    Picture^ vrect = PictureFactory::GetPicture("image.jpg", gcnew BitMap(10, 100));

    System::Console::WriteLine(square->GetType());
    System::Console::WriteLine(hrect->GetType());
    System::Console::WriteLine(vrect->GetType());

    return 0;
}

这取决于你如何使用对象,越简单越好。 :)