#include<iostream>
using namespace std;
class Test
{
public:
Test(){}
Test(int param):i(param){}
int i;
};
int main()
{
Test obj1(100);
//Test obj2[100](obj1) ; - This doesn't work I know
Test obj3[10] = obj1; //This works
cout<<obj3[9].i<<endl;
return 1;
}
In the above code Test obj2[100](obj1);
doesn't work but Test obj3[10] = obj1;
Why is the former supported but latter.(Both would be calling the copy constructor.)
Is that the former isn't supported because of implementation constrains in compilers?
Edit: I am not using c++11. gcc version 4.8.2 (i686-posix-dwarf-rev3, Built by MinGW-W64 project) Qt 5.3.1
Any conclusion?
I'm mainly answering your last comment, because all elements were already given in comments.
What can we see :
Test obj2[100](obj1);
is rejected as error by all compilers tested because ... it does not follows specification for C++ language !Test obj2[100] = obj1;
does not seem to cleanly fit to current specifications because you initialize an array with a single element. More on that :
Test obj2[100] = {obj1};
)obj1
My opinion is that as it unclear that it is correct C++ and as it causes errors on some heavily used C++ compilers, you should avoid to use even the second syntax, unless documenting that in red flashing font.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With