Hi,
This is the program that i am trying.
class A
{
public:
A()
{
cout<<"Default Constructor"<<endl;
a=0;
}
A(void *arg)
{
cout<<"I am into
Constructor(Void *)<<endl;
cout<<"Value Of A : "<<*((int *)arg)<<endl;
a= *((int *)arg);
}
int getA()
{
return a;
}
void setA(int arg)
{
a=arg;
}
~A()
{
cout << "Inside destructor" << endl;
}
private:
int a;
};
int main()
{
A obj;
obj=NULL;
return 1;
}
Output :
Default Constructor
I am into Constructor(Void *)
Segmentation Fault(Core Dumped)
Here I have a default Constructor and a single Argument Constructor
which takes a void * (I dont see any reason for having this but juz to
explain this scenario).
In my main the instance of A "obj" gets constructed by the default
constructor. When I try to assign "obj=NULL" i expect a compilation
error. Instead it compiles fine and it tries to call my single argument
constructor "A( void *)" and gives me a "core dump" as it is a NULL
pointer. when I say "obj=NULL" i expect the default assignment operator
method to be called but how is my constructor call happens.
I tried this in Linux with g++ compiler.
Question is
1. when i try to assign a obj with a NULL why i am not getting a
compilation error.
2. why is A(void *) getting invoked.
3. How is C++ Default Assignment Operator Implemented.
Thanks,
Ganesh.