Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > C++ Default Assignment Operator

Reply
Thread Tools

C++ Default Assignment Operator

 
 
Ganesh Rajaraman
Guest
Posts: n/a
 
      01-03-2007
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.

 
Reply With Quote
 
 
 
 
red floyd
Guest
Posts: n/a
 
      01-03-2007
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.
 
Reply With Quote
 
 
 
 
=?ISO-8859-15?Q?Juli=E1n?= Albo
Guest
Posts: n/a
 
      01-04-2007
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
 
Reply With Quote
 
Grizlyk
Guest
Posts: n/a
 
      01-04-2007

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;

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
POD and assignment operator and test operator Hicham Mouline C++ 2 09-01-2009 06:00 PM
conditions for automatic generation of default ctor, copy ctor,and default assignment operator (operator) puzzlecracker C++ 8 04-15-2008 09:56 PM
default implementation of assignment operator George2 C++ 9 12-16-2007 07:46 PM
Assignment operator self-assignment check Chris C++ 34 09-26-2006 04:26 AM
comma operator and assignment operator G Patel C Programming 4 02-08-2005 02:53 AM



Advertisments