从不同的类访问对象 - 设计
我有三个类,TImageProcessingEngine
、TImage
和 TProcessing
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.
或者您会吗推荐我的班级设计的任何其他方法吗?
I have three classes, TImageProcessingEngine
, TImage
and TProcessing
TImageProcessingEngine
is the one which i am using to expose all my methods to the world.
TImage
is the one i plan to use generic image read and image write functions.
TProcessing
contains methods that will perform imaging operations.
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();
};
My question is how do i access the object mpImageMatrix
in class TProcessing
? So that my calling application can use the following
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.
Or Do you recommend any other approach to my class design?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我当然会推荐一种不同的实现,但让我们先检查一下设计。
我不太明白
TImageProcessingEngine
的附加值,它没有带来任何功能。事实上,我的建议非常简单:
Image
类,保存值Processing
类(接口),应用操作Encoder
和Decoder
类(接口),用于读取和写入不同的格式只有当您能够从中获得效率时,
Processing
类才能访问内部图像,这确实有意义(这很可能),在这种情况下,您可以简单地使Processing
成为朋友,并让它解压其派生的Encoder
和Decoder
的值,遵循相同的原则。
请注意,我从来不需要显式指针,以及由此保证的正确性。
I would certainly recommend a different implementation, but let's check the design first.
I don't really understand the added value of
TImageProcessingEngine
, it doesn't bring any functionality.My advice would be quite simple in fact:
Image
class, to hold the valuesProcessing
class (interface), to apply operationsEncoder
andDecoder
classes (interfaces), to read and write to different formatsIt does make sense for the
Processing
class to have access to the images internal only if you can get efficiency from it (which is likely), in this case you can simply makesProcessing
friend and having it unpack the values for its derivedEncoder
andDecoder
follow the same principle.Note how I never needed an explicit pointer, and the guaranteed correctness that results from it.
首先,根据您提供的代码,没有 ReadImage() & WriteImage() 函数位于 TImageProcessingEngine 类中,因此后面使用此类功能的代码存在缺陷。
至于解决方案,您可以为 tImageMatrix 指针创建一个 getter 函数,如下所示:
然后将该指针(或指向整个 TImage 实例的指针)传递给您要调用的 TProcessing 函数。
First off, based on your provided code there are no ReadImage() & WriteImage() functions in the TImageProcessingEngine class, so the later code where you use such functionality is flawed.
As for the solution, you can make a getter function for the tImageMatrix pointer like this:
Then just pass that pointer (or a pointer to the whole TImage instance) to the TProcessing function you want to call.
为什么你想要一个单独的
TProcessing
进程,当它专门具有仅访问mpImageMatrix;
的函数时,在 OOP 中,你必须绑定 数据成员及其操作..
因此,IMO,删除您的
TProcessing
类并在TImage
中拥有这两个函数..您的
TImage就像,
Why you want to have a separate
TProcessing
process, when it specifically has functions just accessingmpImageMatrix;
In OOP, you have to bind the data members and it's operations..
So, IMO, remove your
TProcessing
class and have both the functions withinTImage
..Your
TImage
will be like,您可以创建一个访问器
TImage
类:You could create an accessor
TImage
class: