Thread: member function question

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    95

    member function question

    Here is a code listing from c++ primer plus. I don't understand how the copy constructor in this listing works

    Code:
    #include <iostream>
    using namespace std;
    #include <cstdlib>
    
    template <class T, int n>
    class ArrayTP
    {
         private:
              T ar[n];
         public:
              ArrayTP();
              explicit ArrayTP(const T & v);// cpy constructor proto type
              virtual T & operator[](int i);
              virtual const T & operator[](int i) const;
    };
    
    .
    .
    .
    .
     template <class T, int n>
    ArrayTP<T,n>::ArrayTP(const T & v)
    {
         for (int i=0; i<n; i++)
              ar[i]=v;                  //my question is here. i dont understand 
                                            // shouldnt it be ar[i]=v[i];
    }
    .
    .
    .
    .

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    v is not an array, it's a value of type T (The ArrayTP class wraps a real array of T). That constructor initializes all the elements in the array to v. After the constructor, everything in the array will have the same value.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    95

    Thanks SilentStrike

    Thanks silentstrike!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-19-2008, 12:06 AM
  2. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. what does this warningmean???
    By kreyes in forum C Programming
    Replies: 5
    Last Post: 03-04-2002, 07:53 AM
  5. Pointer to member function as callback function
    By ninebit in forum C++ Programming
    Replies: 10
    Last Post: 03-01-2002, 05:52 AM