Note that there are some explanatory texts on larger screens.

plurals
  1. POC++,Need help to understand some constructors and functions in a vector class using pointers
    primarykey
    data
    text
    <p>Greetings All;</p> <p>I have to develop a C++ class library comprising a collection of numerical techniques for scientific computing. The library should implement Vector class (using pointers) with some basic functionality stated in a header file "Vector.h".</p> <pre><code>#ifndef VECTOR_H #define VECTOR_H template &lt;class T&gt; class CVector { private: int nn; //size of array T *v; //pointer to array of data public: //Default constractor CVector(); //zero based array CVector(int n); //initialize to constant of value a CVector(int n, const T &amp;a); //initialize to array a CVector(int n, const T *a); //copy constractor CVector(const CVector &amp;rhs); //assignment CVector &amp; operator=(const CVector &amp;rhs); //i'th element inline T &amp; operator[](const int i); inline const T &amp; operator[](const int i) const; inline int size() const; //resize (contents not preserved) void resize(int newn); //resize and assign a constant value void assign(int newn, const T &amp;a); //deconstractor ~CVector(); }; #endif /* VECTOR_H */ </code></pre> <p>I am a beginner in C++ and I have some confusion to understand some constructors and functions in the above code.</p> <p>My questions are:</p> <p>1- What is the concept of the following constructor?</p> <pre><code> //initialize to array a CVector(int n, const T *a); </code></pre> <p>I mean how to initialize a vector to an array a?</p> <p>2- What is the difference between the copy constructor and the assignment one?</p> <pre><code> //copy constractor CVector(const CVector &amp;rhs); //assignment CVector &amp; operator=(const CVector &amp;rhs); </code></pre> <p>3- I know that this function is to return the ith element of the vector:</p> <pre><code> //i'th element inline T &amp; operator[](const int i); </code></pre> <p>but what is the difference between it and this one:</p> <pre><code> inline const T &amp; operator[](const int i) const; </code></pre> <p>I need to understand the concept in order to know how to implement them in the .cpp file and how to call them in my main. I'll be glad if you help me.</p> <p>Best regards;</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload