+ Reply to Thread
Page 2 of 2 FirstFirst 12
Results 11 to 13 of 13

Thread: Templates and Sorting

  1. #11
    Join Date
    Jul 2009
    Location
    Santa Clarita, CA
    Posts
    2,111
    Blog Entries
    47
    Rep Power
    31

    Re: Templates and Sorting

    Actually, it does succeed in sorting, and this is perfectly fine code. Array's are nothing more than thinly veiled pointers, so while it is true the function is not technically passing by reference, that would be because it is passing by address instead.

    You can just as easily use this code:
    Code:
    template<typename T>
    void selectSort(T arr[], int n) {
    As this code:
    Code:
    template<typename T>
    void selectSort(T *arr, int n) {
    - Zeke

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #12
    Join Date
    Mar 2008
    Posts
    7,144
    Rep Power
    86

    Re: Templates and Sorting

    Thanks for the info.

    So when you added the pointer operator before the array name, you were able to remove the array operator? Why? I tried it out but I don't quite see why that still works.

  4. #13
    Join Date
    Jul 2009
    Location
    Santa Clarita, CA
    Posts
    2,111
    Blog Entries
    47
    Rep Power
    31

    Re: Templates and Sorting

    Arrays are pointers, that's why. When arrays are declared, they return a pointer to a block sequence of values that are right next to each other, and when you use the array operator ([]), you're essentially asking to dereference the pointer value plus whatever value is inside of the array operator brackets. An example might be better... essentially using the array operator like this:
    Code:
    int t = myArray[5];
    Is the same as using this:
    Code:
    int t = *(myArray + 5);
    However since that doesn't really LOOK that good, they opted to use the array operator.

    There's one other difference I know of with arrays, and that's when you're using dynamic allocation and the delete operator. You need to use "delete[]" when you delete an array instead of plain old "delete", like you would for data types, structs, and objects.

    Hope that helps.

    - Zeke

+ Reply to Thread
Page 2 of 2 FirstFirst 12

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. ASP.NET templates
    By Richmord in forum ASP, ASP.NET and Coldfusion
    Replies: 1
    Last Post: 09-19-2008, 03:46 AM
  2. Problem with templates
    By merfin in forum C and C++
    Replies: 6
    Last Post: 02-08-2008, 03:02 PM
  3. Let me know what you think of my templates
    By Kleopatra in forum Website Design
    Replies: 2
    Last Post: 07-22-2007, 01:39 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts