Thread: Need Help Passing an Array as a Parameter

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    3

    Need Help Passing an Array as a Parameter

    Code:
    #include <Windows.h>
    
    struct BaseClass 
    {
        private:
        protected:
        public:
            virtual void ReservedFunction() {} 
    };
    template <typename TYPE> struct DerivedClass : public BaseClass 
    { 
        private:
        protected:
        public:
            TYPE                            Value;
    
    
            template <typename TYPE> DerivedClass(const TYPE & in__Source) : Value(in__Source) {}
    };
    template <typename TYPE> TYPE ConvertParameter(BaseClass * in__Source)
    {
        DerivedClass<TYPE> * ReturnValue = dynamic_cast<DerivedClass<TYPE> *>(in__Source);
        return ReturnValue->Value;
    }
    template <typename TYPE> DerivedClass<TYPE> * BuildParameter(const TYPE in__Source)
    {
        DerivedClass<TYPE> * ReturnValue = new DerivedClass<TYPE>(in__Source);
        return ReturnValue;
    }
    
    
    struct ArrayFunction
    {
        private:
        protected:
        public:
            ArrayFunction(BaseClass ** in__Array)
            {
                in__Array++;
            }
    };
    
    
    int main()
    {
        BaseClass * Array[3];
            Array[0] = BuildParameter<int>(14);
            Array[1] = BuildParameter<double>(5.2);
            Array[2] = BuildParameter<char *>("string");
    
    
        ArrayFunction Function(Array);
    
    
        return 0;
    }
    I am able to make an array with any values using smart pointers and dynamic_cast. But the only problem I am having is that I am not able to use the array index when I pass it into a function. I am currently using the increment operator ++ to move through the list but I would like use the array index to move through it.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    you can pass a real array (not just a pointer to the first element) to a function by declaring your function like this:

    Code:
    template<typename T, int N>
    void function(T (&t)[N]);
    you can replace the type parameter with an explicit type and get the same effect.

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    3
    Thanks for that but now I am having problems with the ConvertParameter function.

    I tried changing the parameter in that function to accept a real to one of the elements in the array but I am not able to set the value in the array.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing an array to a function as a parameter
    By boxden in forum C Programming
    Replies: 1
    Last Post: 03-13-2010, 09:35 AM
  2. Passing array of struct as a function parameter
    By desmond5 in forum C Programming
    Replies: 5
    Last Post: 12-04-2007, 11:32 AM
  3. Problem passing double array as a function parameter
    By pppbigppp in forum C++ Programming
    Replies: 7
    Last Post: 06-06-2006, 03:08 AM
  4. Passing array member as a parameter
    By stumpster123 in forum C Programming
    Replies: 4
    Last Post: 03-29-2005, 11:36 AM
  5. passing 2D array of ptrs as parameter
    By Psycho in forum C++ Programming
    Replies: 3
    Last Post: 06-13-2003, 10:13 AM