After reading this thread http://stackoverflow.com/questions/121162/what-does-the-explicit-keyword-in-c-mean
I made up this program
class MyClass
{
public:
explicit MyClass(int a)
{
cout<<"Int was called"<<endl;
val = a;
}
MyClass(char *a)
{
cout<<"Char was called"<<endl;
val = atoi(a);
}
MyClass(const MyClass& copy)
{
cout<<"Copy Const was called"<<endl;
this->val = copy.val;
}
inline const int getval() const
{ return val; }
private:
int val ;
};
main code
int main()
{
int x=4;
char y='4';
char *z = &y;
MyClass a(x);
MyClass b(z);
MyClass c(a);
MyClass d('4');
cout<<a.getval()<<endl;
cout<<b.getval()<<endl;
cout<<c.getval()<<endl;
cout<<d.getval()<<endl;
return 0;
}
The output was:
Int was called
Char was called
Copy Const was called
Int was called
4
4
4
52
Now, as per thread above, it should throw error after the constructor call on object d but it didn't.
g++ version info
Using built-in specs.
Target: i486-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.4.3-4ubuntu5' --with-bugurl=file:///usr/share/doc/gcc-4.4/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --enable-shared --enable-multiarch --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.4 --program-suffix=-4.4 --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug --enable-plugin --enable-objc-gc --enable-targets=all --disable-werror --with-arch-32=i486 --with-tune=generic --enable-checking=release --build=i486-linux-gnu --host=i486-linux-gnu --target=i486-linux-gnu
Thread model: posix
gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5)
I am not sure if i have done something wrong in the above code. Please help