| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"Ganesh Rajaraman" |
| Date: |
03 Jan 2007 05:24:13 PM |
| Object: |
C++ Default Assignment Operator |
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.
.
|
|
| User: "Grizlyk" |
|
| Title: Re: C++ Default Assignment Operator |
04 Jan 2007 02:17:44 AM |
|
|
Ganesh Rajaraman wrote:
Hi,
A(void *arg)
{
cout<<"I am into Constructor(Void *)<<endl;
cout<<"Value Of A : "<<*((int *)arg)<<endl;
a= *((int *)arg);
}
A obj;
obj=NULL;
Default Constructor
I am into Constructor(Void *)
Segmentation Fault(Core Dumped)
Segmentation Fault because
a= *((int *)arg);
for (arg == NULL) is equal to
a= *((int*)0) =* 0 = segmentation fault due to access (read) from
address 0.
It is implementation depended, in some systems (OSes and compilers)
access to address 0 treats as segmentation fault, even for DOS, one can
see "NULL pointer assignment" message after program exit.
Check "arg" for example like this:
a= arg? *((int*)arg): 0;
.
|
|
|
|
| User: "red floyd" |
|
| Title: Re: C++ Default Assignment Operator |
03 Jan 2007 05:42:32 PM |
|
|
Ganesh Rajaraman wrote:
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)
[redacted]
Question is
1. when i try to assign a obj with a NULL why i am not getting a
compilation error.
Because the compiler silently translates
obj=NULL;
into
obj = A(NULL);
2. why is A(void *) getting invoked.
See above
3. How is C++ Default Assignment Operator Implemented.
See above.
.
|
|
|
|
| User: "=?ISO-8859-15?Q?Juli=E1n?= Albo" |
|
| Title: Re: C++ Default Assignment Operator |
03 Jan 2007 06:26:18 PM |
|
|
Ganesh Rajaraman wrote:
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.
Because you have a constructor that takes a void * and is not marked as
explicit. A non explicit constructor with one argument can be used by the
compiler as an implicit conversion operator.
So the line:
obj= NULL;
becomes:
obj= A (NULL);
--
Salu2
.
|
|
|
|

|
Related Articles |
|
|