c++multipleoutputs

Multiple Type output in Polymorphism


I have a problem when i want to create a pure virtual function and assign to this multiple range of output formats (like int, double and char).

i wrote below code and it works only when i eliminate my abstract class which it's not thing that i want.

my code:

enter code here

    //class DynamicArray
          //{
          //    public:
          //        virtual void GetData(unsigned int s, int& d) = 0;
          //        virtual void SetData(unsigned int s, int& d) = 0;
          //};
      
      
      
      class DynamicArrayDouble//: public DynamicArray
      {
          private:
              unsigned int m_Length;
              double* arr;
          public:
              DynamicArrayDouble(unsigned int l)
              {
                  m_Length = l;
                  arr = new double[m_Length];
              }
              void SetParam(unsigned int l)
              {
                  m_Length = l;
                  arr = new double[m_Length];
              }
              void GetData(unsigned int s, double& d)
              {
                  d = arr[s];
              }
              void SetData(unsigned int s, double& d)
              {
                  arr[s] = d;
              }
      };

when i uncomment DynamicArray class and DynamicArrayDouble inherit it i face with some error. it should be noted, first time i try to use void* for second parameter for Set and Get methods, but again i receive some errors that i can't use this code style like this: error: cannot declare variable 'd1' to be of abstract type 'DynamicArrayDouble'

and code of above error is:

    class DynamicArray
    {
        public:
            virtual void GetData(unsigned int s, void* d) = 0;
            virtual void SetData(unsigned int s, void* d) = 0;
    };
    
    class DynamicArrayDouble: public DynamicArray
    {
        private:
            unsigned int m_Length;
            double* arr;
        public:
            DynamicArrayDouble(unsigned int l)
            {
                m_Length = l;
                arr = new double[m_Length];
            }
            void SetParam(unsigned int l)
            {
                m_Length = l;
                arr = new double[m_Length];
            }
        void GetData(unsigned int s, double* d)
        {
            *d = arr[s];
        }
        void SetData(unsigned int s, double* d)
        {
            arr[s] = *d;
        }
    };
    
    int main()
    {
        DynamicArrayDouble d1(5);
        double x=0;
    
        for(unsigned int i=0;i<5;i++)
        {
            x = ((i+1.0)/2);
            d1.SetData(i,&x);
        }
    
        for(unsigned int i=0;i<5;i++)
        {
            d1.GetData(i,&x);
            cout << "Data " << i+1 << " is = " << x << endl;
        }
    
        return 0;
    }

i write my codes in codeblocks.

i will appreciate your answer... Thank you.


Solution

  • You can't simply override

    void func(some_pointer_type ptr);
    

    with

    void func(some_other_pointer_type ptr);
    

    and same goes for references. Those are in fact considered to be completely unrelated if they don't match.

    DynamicArrayDouble - this name tells me you should look into templates and not write the same code for all the types you'll need. This is how STL works. You'll completely avoid runtime polymorphism.

    For starters:

    template <typename T>
    DynamicArray;