C++,Need help to understand some constructors and functions in a vector class using pointers -


greetings all;

i have develop c++ class library comprising collection of numerical techniques scientific computing. library should implement vector class (using pointers) basic functionality stated in header file "vector.h".

#ifndef vector_h #define vector_h  template <class t> class cvector { private:     int nn; //size of array     t *v;   //pointer array of data  public:      //default constractor    cvector();      //zero based array     cvector(int n);      //initialize constant of value     cvector(int n, const t &a);      //initialize array     cvector(int n, const t *a);      //copy constractor     cvector(const cvector &rhs);      //assignment     cvector & operator=(const cvector &rhs);      //i'th element     inline t & operator[](const int i);      inline const t & operator[](const int i) const;      inline int size() const;      //resize (contents not preserved)     void resize(int newn);      //resize , assign constant value     void assign(int newn, const t &a);      //deconstractor     ~cvector();  };  #endif  /* vector_h */ 

i beginner in c++ , have confusion understand constructors , functions in above code.

my questions are:

1- concept of following constructor?

    //initialize array     cvector(int n, const t *a); 

i mean how initialize vector array a?

2- difference between copy constructor , assignment one?

    //copy constractor     cvector(const cvector &rhs);      //assignment     cvector & operator=(const cvector &rhs); 

3- know function return ith element of vector:

    //i'th element     inline t & operator[](const int i); 

but difference between , one:

    inline const t & operator[](const int i) const; 

i need understand concept in order know how implement them in .cpp file , how call them in main. i'll glad if me.

best regards;

q1: constructor can used fill vector contents of n elements of array starting @ a.

for example:

   float a[42] = { 31, 41, 59 };    cvector<float> v( 3, ); 

q2: first copy constructor, second assignment operator. copy constructor used copy values function parameter, return value function, or initialize variable.

for example, copy constructor used these:

cvector<float> foo( cvector<float> v ) { ... }  ... cvector<float> v1; cvector<float> v2 = foo( v1 ); // copy constructor used pass in v1, , return v2 cvector<float> v3 = v1; // copy constructor used copy v1 v2. 

and assignment used this:

cvector<float> v4; v4 = v1; 

q3. first can used on left-hand-side of assignment. const version used when applied const object.

void bar( const float & fval ) { ... } ... cvector<float> v1( 3, ); v1[0] = 42;   // non-const operator[]  const cvector<float> v2 = v1;  float value = v2[0];  // const operator[] bar( v2[0] ); // const operator[] 

Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -