Generic Array Type.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DiptenduDas
    New Member
    • Mar 2008
    • 1

    Generic Array Type.

    Hi,
    I want to write a Generic Method which will display both Integer and Char array.

    My Sample Program Goes Below:
    =============== =========

    #include<iostre am>
    using namespace std;

    void displayInt(int* intArray)
    {
    for(int i = 0 ;i< 5;)
    cout<<intArray[i++]<<endl;
    }

    void displayChar(cha r* charArray)
    {
    for(int i = 0 ;i< 5;)
    cout<<charArray[i++]<<endl;
    }

    int main()
    {
    int intArray[] = {1,2,3,4,5};
    char charArray[] = {'a','b','c','d ','e'};

    cout<<"Displayi ng Integer Array :"<<endl;
    int* intArr = (int*) intArray;
    displayInt(intA rr);

    cout<<"Displayi ng Character Array:"<<endl;
    char* charArr = (char*) charArray;
    displayChar(cha rArr);

    return 0;
    }




    But I want to write a single function lets say
    void display(void* vArray)
    {
    for(int i = 0 ;i< 5;)
    cout<<vArray[i++]<<endl;
    }
    instead of
    displayInt(int* intArray) and displayChar(cha r* charArray).

    What I should do for the same?
    Thanking you in advance.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Thois is what C++ templates are for.

    First, wrote your code and get it working, say using int. Then convert yourt code to a template.

    Comment

    Working...