0

I have a class Array which implements a generic two-dimensional array of elements of type T :

#ifndef _ARRAY_
#define _ARRAY_

namespace math
{

template <typename T>
class Array
{
protected:
    //! Flat storage of the elements of the array of type T
    T * buffer;                     

    //! The width of the array (number of columns)
    unsigned int width,             

    //! The height of the array (number of rows)
                 height;            

public:

    /*! Reports the width (columns) of the array
     *
     * \return the width.
     */
    unsigned int getWidth() const { return width; }      

    /*! Reports the height (rows) of the array
     *
     * \return the height.
     */
    unsigned int getHeight() const { return height; }    

    template <typename T>
    T & Array<T>::operator () (int x, int y) {
        float loc = y*width + x;
        return *(buffer + loc);
    }

    template <typename T>
    Array<T>::Array(unsigned int w, unsigned int h):width(w),height(h) {}

    template <typename T>
    Array<T>::Array(const Array<T> & source):width(source.width),height(source.height) {}                   

    template <typename T>
    Array<T>::Array & operator = (const Array<T> & source) {
        width = source.width;
        height = source.height;
        return *this;
    }

    /*! Virtual destructor.
     */
    virtual ~Array();                                    

};

} // namespace math

#endif

I also have a derived class named Image which is used to store the image data of a ppm image:

using namespace std;
#include <iostream>
#include "Image.h"

namespace math
{

class Image : public Array<Vec3<float>> {
protected:

    Vec3<float> * buffer;

public:


    //Obtains the color of the image at location (x,y).
    Vec3<float> getPixel(unsigned int x, unsigned int y) const {
        if (x > 0 && x < Array::getWidth() && y > 0 && y < Array::getHeight()) {
            float pixel_r = (x, y);
            float pixel_g = (x, y) + 1;
            float pixel_b = (x, y) + 2;
            Vec3<float> c = Vec3<float>(pixel_r, pixel_g, pixel_b);
            return c;
        }
        Vec3<float> c = Vec3<float>();
        return c;
    }

    //Constructor

    Image(int w, int h, Vec3<float> * t_buffer) : Array(w, h), buffer(t_buffer) {}

    //Copy Constructor
    Image(const Image & src) : Array(src) {
        memcpy(buffer, src.buffer, sizeof(Vec3<float>));
    }

    //Copy assignment operator
    Image & operator = (const Image& right) {
        operator=(right);
        if (buffer != nullptr)
            delete[] buffer;
        memcpy(buffer, right.buffer, sizeof(Vec3<float>));
        return *this;
    }

    //The Image destructor.
    ~Image() {
        if (buffer) delete[] buffer;
    }
};
}

My question is how can I use the getWidth() and getHeight() functions of the Array class in Image class.

  • 2
    http://stackoverflow.com/questions/357307/how-to-call-a-parent-class-function-from-derived-class-function – Valentin Dec 22 '16 at 16:04
  • Welcome to Stack Overflow. Please take the time to read [The Tour](http://stackoverflow.com/tour) and refer to the material from the [Help Center](http://stackoverflow.com/help/asking) what and how you can ask here. – πάντα ῥεῖ Dec 22 '16 at 16:08
  • Note that names that begin with an underscore followed by a capital letter (`_ARRAY_`) and names that contain two consecutive underscores are reserved for use by the implementation. Don't use them in your code. – Pete Becker Dec 22 '16 at 16:43

0 Answers0