I am learning about pointers, and how they can be used to send array(size is not assigned) to objects. Ultimately i m trying to learn how arrays can be added and multipled using operator overloading instead of using "vector" in c++.
I defined a class named vector as follows,
class vector
{
private:
int len;
int *pvec;
}
public:
vector(int array[])
{
len = sizeof(array)/sizeof(*array);
cout<<len<<endl;
pvec = new int[len+1];
for(int i = 0;i<len;i++)
{
pvec[i] = *(array + i);
}
}
void display()
{
cout<<"[";
for(int i = 0;i<len;i++)
{
cout<<pvec[i];
if(i < len-1)
{
cout<<",";
}
}
cout<<endl;
}
};
and i declared an object of class vector as follows
int vec1[] = {1,2,3,4};
vector v(vec1);
when i try to display the elements in the vector using the member function as
v.display();
the output is as follows
2
[1,2]
but as you can see the length of the vector is 4, but in the output it is displayed as 2. If i try to display the length of the array in main as
cout<<sizeof(vec1)/sizeof(*vec1)<<endl;
the length is displayed as 4, but if i pass the same array to the object it is displayed as 2.
If any one could point out a mistake in this program, i would be grateful.