gpt4 book ai didi

c++ - 从不同的类访问对象 - 设计

转载 作者:行者123 更新时间:2023-11-28 01:10:34 24 4
gpt4 key购买 nike

我有三个类,TImageProcessingEngineTImageTProcessing

TImageProcessingEngine 是我用来向世界展示我所有方法的工具。

TImage 是我计划使用通用图像读取和图像写入功能的那个。

TProcessing 包含将执行成像操作的方法。

class TImageProcessingEngine
{
public:
TImage* mpImageProcessingEngine;

};

class TImage
{
public:
int ReadImage();
int WriteImage();

private:
//a two dimensional array holding the pixel values
tImageMatrix* mpImageMatrix;
};

class TProcessing
{
public:
int ConvertToBinary();
int ConvertToGrayScale();
};

我的问题是如何访问 TProcessing 类中的对象 mpImageMatrix 以便我的调用应用程序可以使用以下内容

TImageProcessingEngine* vEngine = new TImageProcessingEngine;

//Converts an input gray scsale image to binary image
vEngine->ReadImage().ConvertToBinary();

//Write the converted image to disk
vEngine->WriteImage();

delete vEngine;
vEngine = NULL;

//During this whole processing internally,
//the image is read in to `mpImageMatrix`
//and will also be holding the binarised image data,
//till writing the image to disk.

或者您是否推荐我的类设计的任何其他方法?

最佳答案

我当然会推荐一个不同的实现,但让我们先检查一下设计。

我不太明白TImageProcessingEngine的附加值,它没有带来任何功能。

我的建议其实很简单:

  • Image 类,用于保存值
  • Processing 类(接口(interface)),应用操作
  • EncoderDecoder 类(接口(interface)),用于读取和写入不同的格式

Processing 类访问内部图像确实有意义,前提是您可以从中获得效率(这很可能),在这种情况下,您可以简单地制作 Processing friend 并让它解压派生的值

class Image
{
public:
Image();

void Accept(Processing& p);
void Encode(Encoder& e) const; // Image is not modified by encoding

void Decode(Decoder& d); // This actually resets the image content

private:
friend class Processing;

size_t mHeight;
size_t mWidth;
std::vector<Pixel> mPixels; // 2D array of Pixels
};

class Processing
{
public:
void apply(Image& image)
{
this->applyImpl(image.mHeight, image.mWidth, image.mPixels);
}

private:
virtual void applyImpl(size_t h, size_t w, std::vector<Pixel>& pixels) = 0;
};

EncoderDecoder 遵循同样的原则。

请注意我从来不需要显式指针,以及由此产生的保证正确性。

关于c++ - 从不同的类访问对象 - 设计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3616000/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com