Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to make a function that you can pass any type of array to??

How to make a function that you can pass any type of array to??

Scheduled Pinned Locked Moved C / C++ / MFC
data-structurestutorialquestion
10 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    johnstonsk
    wrote on last edited by
    #1

    I think my last post wasn't understandable. I am trying to create a function that can accept any type of an array as a paramater. char[] or double[] or int[] etc. wouldn't it be myFunction(void *data){ do stuff } char tmp[100]; myFunction(tmp); I hope this makes sense. Steven

    D N I J 4 Replies Last reply
    0
    • J johnstonsk

      I think my last post wasn't understandable. I am trying to create a function that can accept any type of an array as a paramater. char[] or double[] or int[] etc. wouldn't it be myFunction(void *data){ do stuff } char tmp[100]; myFunction(tmp); I hope this makes sense. Steven

      D Offline
      D Offline
      Dean Goodman
      wrote on last edited by
      #2

      Yes that would work, but once inside your function, you have to know what type of array has been passed. You could do something like this:

      enum TYPES
      {
        CHAR,
        DOUBLE,
        INT,
        ...other types...
      };
      
      void myFunc(void* data, int data_type)
      {
        switch (data_type)
        {
         case CHAR: char* carray = (char*)data;
          .... do stuff
          break;
      
         case DOUBLE: double* darray = (double*)data;
         ...etc.
        }
      
        ...
      }
      

      You will probably also want to add a size parameter to myFunc as well, unless all your arrays will be a fixed size. --Dean

      J I 2 Replies Last reply
      0
      • J johnstonsk

        I think my last post wasn't understandable. I am trying to create a function that can accept any type of an array as a paramater. char[] or double[] or int[] etc. wouldn't it be myFunction(void *data){ do stuff } char tmp[100]; myFunction(tmp); I hope this makes sense. Steven

        N Offline
        N Offline
        Neville Franks
        wrote on last edited by
        #3

        void ptrs are dangerous as they aren't typesafe and are best avoided where possible. Have a look at Boost Any http://www.boost.org/doc/html/any.html[^] Neville Franks, Author of ED for Windows. Free Trial at www.getsoft.com

        1 Reply Last reply
        0
        • D Dean Goodman

          Yes that would work, but once inside your function, you have to know what type of array has been passed. You could do something like this:

          enum TYPES
          {
            CHAR,
            DOUBLE,
            INT,
            ...other types...
          };
          
          void myFunc(void* data, int data_type)
          {
            switch (data_type)
            {
             case CHAR: char* carray = (char*)data;
              .... do stuff
              break;
          
             case DOUBLE: double* darray = (double*)data;
             ...etc.
            }
          
            ...
          }
          

          You will probably also want to add a size parameter to myFunc as well, unless all your arrays will be a fixed size. --Dean

          J Offline
          J Offline
          johnstonsk
          wrote on last edited by
          #4

          thanks steven

          1 Reply Last reply
          0
          • J johnstonsk

            I think my last post wasn't understandable. I am trying to create a function that can accept any type of an array as a paramater. char[] or double[] or int[] etc. wouldn't it be myFunction(void *data){ do stuff } char tmp[100]; myFunction(tmp); I hope this makes sense. Steven

            I Offline
            I Offline
            Ian Darling
            wrote on last edited by
            #5

            What's wrong with templates?

            template<class T> void MyFunction(T* start, T* end)
            {
              for(T* iter=start; iter <= end; iter++)
              {
                cout << *iter;
              }
            }
            
            char temp [100];
            
            MyFunction(temp, &temp[99]);
            

            The beauty of this approach is that it works with things that provide iterators such as std::vector, as well as standard arrays. Untested code, so YMMV. -- Ian Darling If I was any more loopy, I'd be infinite.

            J 1 Reply Last reply
            0
            • D Dean Goodman

              Yes that would work, but once inside your function, you have to know what type of array has been passed. You could do something like this:

              enum TYPES
              {
                CHAR,
                DOUBLE,
                INT,
                ...other types...
              };
              
              void myFunc(void* data, int data_type)
              {
                switch (data_type)
                {
                 case CHAR: char* carray = (char*)data;
                  .... do stuff
                  break;
              
                 case DOUBLE: double* darray = (double*)data;
                 ...etc.
                }
              
                ...
              }
              

              You will probably also want to add a size parameter to myFunc as well, unless all your arrays will be a fixed size. --Dean

              I Offline
              I Offline
              Ian Darling
              wrote on last edited by
              #6

              Not being funny, or mean to be offensive, but that's about the worst solution I could imagine in C++. * it requires massive amounts of work if you wish to add a new type. * it's still not typesafe - you can't validate what's passed in * and there are far far better approaches in C++ (eg, templates) -- Ian Darling If I was any more loopy, I'd be infinite.

              D 1 Reply Last reply
              0
              • I Ian Darling

                Not being funny, or mean to be offensive, but that's about the worst solution I could imagine in C++. * it requires massive amounts of work if you wish to add a new type. * it's still not typesafe - you can't validate what's passed in * and there are far far better approaches in C++ (eg, templates) -- Ian Darling If I was any more loopy, I'd be infinite.

                D Offline
                D Offline
                Dean Goodman
                wrote on last edited by
                #7

                Yes, I agree -- it's quick, dirty, ugly... I get the feeling though that going into something like templates would not have been understood if the above could not have been done without help from this board (no offense meant to anyone). I try to approach these things from the standpoint that a less elegant but easier to understand solution might better serve beginners. Then, at a later time, the better way to do something can be learned when the supporting knowledge is available. A fine line has to be drawn however, as the easy code might be a terrible way to do things. I suppose I should have added a disclaimer saying that there are better ways of doing things. With that said, I hadn't even thought about templates at the time :-O --Dean

                1 Reply Last reply
                0
                • J johnstonsk

                  I think my last post wasn't understandable. I am trying to create a function that can accept any type of an array as a paramater. char[] or double[] or int[] etc. wouldn't it be myFunction(void *data){ do stuff } char tmp[100]; myFunction(tmp); I hope this makes sense. Steven

                  J Offline
                  J Offline
                  John M Drescher
                  wrote on last edited by
                  #8

                  [EDIT] Sorry did not read all the posts, but this problem screams for a template approach... [/EDIT]

                  template<class T>myFunction(T* data)
                  {
                  // do stuff
                  }

                  int main()
                  {
                  char tmp[100];
                  float fTmp[1000];
                  myFunction<char>(tmp);
                  myFunction<float>(fTmp);
                  }

                  John

                  1 Reply Last reply
                  0
                  • I Ian Darling

                    What's wrong with templates?

                    template<class T> void MyFunction(T* start, T* end)
                    {
                      for(T* iter=start; iter <= end; iter++)
                      {
                        cout << *iter;
                      }
                    }
                    
                    char temp [100];
                    
                    MyFunction(temp, &temp[99]);
                    

                    The beauty of this approach is that it works with things that provide iterators such as std::vector, as well as standard arrays. Untested code, so YMMV. -- Ian Darling If I was any more loopy, I'd be infinite.

                    J Offline
                    J Offline
                    Jorgen Sigvardsson
                    wrote on last edited by
                    #9
                    template <typename ForwardIter> void MyFunction(ForwardIter start, ForwardIter end)
                    {
                        while(start != end) {
                            doSomething(*start);
                            ++start;
                        }
                    }
                      
                    char temp[100];
                    MyFunction(temp, temp + 100);
                    

                    Would be a cleaner solution, as your will only work with std::vector. This'll work with any iterator. -- Gnnnnmmmpppppppfffffhhh!

                    I 1 Reply Last reply
                    0
                    • J Jorgen Sigvardsson
                      template <typename ForwardIter> void MyFunction(ForwardIter start, ForwardIter end)
                      {
                          while(start != end) {
                              doSomething(*start);
                              ++start;
                          }
                      }
                        
                      char temp[100];
                      MyFunction(temp, temp + 100);
                      

                      Would be a cleaner solution, as your will only work with std::vector. This'll work with any iterator. -- Gnnnnmmmpppppppfffffhhh!

                      I Offline
                      I Offline
                      Ian Darling
                      wrote on last edited by
                      #10

                      Like I said, untested code, and I'm a little rusty on C++ templates, but I figured it was essentially ok, and certainly illustrated what I wanted to suggest. -- Ian Darling If I was any more loopy, I'd be infinite.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups