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

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?

Edited by Nick Evan: Fixed formatting

6 Years
Discussion Span
Last Post by Ancient Dragon

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.