Consider the following program:
#include <iostream>
struct Test
{
int a;
Test() : a(3)
{ }
Test(const Test& t...)
{
std::cout<<"Copy constructor called\n";
a=t.a;
}
int get_a()
{
return a;
}
~Test()
{
std::cout<<"Destructor is called\n";
}
};
int main()
{
Test t;
Test* t1=new Test(t);
std::cout<<t.get_a()<<'\n';
std::cout<<t1->get_a()<<'\n';
delete t1;
}
Closely observe the three dots in parameter of copy constructor I was really surprised when I tried this program. What is the use of it? What does it mean?
What the language specification says about this?
I know that three dots are used to represent variable length arguments in variadic functions
like printf()
and scanf()
etc and also variadic macros introduced by C99. In C++, if I am not wrong, they are used in variadic templates.
Is this code well formed? Is this variadic copy constructor that can take any number of arguments?
It compiles & runs fine on g++ 4.8.1 & MSVS 2010.
Test(const Test& t, ...)
(note the comma) – Kerb