class vector
{
   int *v;
   int size;
    vector ( int m )
    {
       v=new int[size=m];
        for(int i=0;i<size;i++)
        v[i]=0;
     }
     vector( int *a)
   {
     for(int i=0;i<size;i++)
        v[i]=a[i];
   }
};
int main()
{
   int x[3]={1,2,3};
   vector v1(3);
   vector v2(3);
   v1=x;
   v2=y;

return 0;
}  

My doubt is , how can the constructor being called twice?
Is that even allowed? I thought constructors are invoked when objects are created.Also why is the argument of 2nd constructor int *a instead of int a[] . Is there any difference between those two?

Since the class has no public members it can't be used in main()

But to answer your question, it is common practice to have multiple constructors as long as they have different parameters. Only one constructor will get called, the one with the same type of parameters as the calling function. In the case you posted, the first constructor will be the one that gets called.

>>Also why is the argument of 2nd constructor int *a instead of int a[] .
Yes there is a difference -- int *a is a pointer to something, it may or may not be an array of integers because it also could be a pointer to a single integer. int a[] is definitely an array of integers. Whether you use them both to refer to an array of integers is up to you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.