开发者

Accessing an object from a different class - Design

开发者 https://www.devze.com 2023-01-14 14:48 出处:网络
I have three classes, TImageProcessingEngine, TImage and TProcessing TImageProcessingEngine is the one which i am using to expose all my methods to the world.

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 TProcess开发者_如何学Cing? 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?


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 values
  • Processing class (interface), to apply operations
  • Encoder and Decoder classes (interfaces), to read and write to different formats

It 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 makes Processing friend and having it unpack the values for its derived

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;
};

Encoder and Decoder follow the same principle.

Note how I never needed an explicit pointer, and the guaranteed correctness that results from it.


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:

tImageMatrix* GetImageMatrix() { return mpImageMatrix; }

Then just pass that pointer (or a pointer to the whole TImage instance) to the TProcessing function you want to call.


Why you want to have a separate TProcessing process, when it specifically has functions just accessing mpImageMatrix;

In OOP, you have to bind the data members and it's operations..

So, IMO, remove your TProcessing class and have both the functions within TImage..

Your TImage will be like,

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

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


You could create an accessor TImage class:

byte * pixelAt(unsigned x, unsigned y);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号