C++ Default Assignment Operator

  • Thread starter Ganesh Rajaraman
  • Start date
G

Ganesh Rajaraman

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.
 
R

red floyd

Ganesh said:
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.
 
?

=?ISO-8859-15?Q?Juli=E1n?= Albo

Ganesh said:
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);
 
G

Grizlyk

Ganesh said:
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;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top