473,386 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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.

Jan 3 '07 #1
3 7356
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.
Jan 3 '07 #2
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
Jan 4 '07 #3

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;

Jan 4 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Dan Cernat | last post by:
Hi there, A few threads I had a little chat about default values. I am starting this thread because I want to hear more opinions about the default values of function parameters. Some say they...
10
by: cppaddict | last post by:
Hi, I am writing a program and needs to know one of its object members before it can be initialized. It doesn't really matter for my question (which a C++ question, not a windows question), but...
4
by: Steven T. Hatton | last post by:
I mistakenly set this to the comp.std.c++ a few days back. I don't believe it passed the moderator's veto - and I did not expect or desire anything different. But the question remains: ISO/IEC...
4
by: moleskyca1 | last post by:
Hi, In a recent discussion, some of us were in disagreement about the functions the C++ compiler generates. How many functions are generated by the compiler when you declare: class Foo { };...
5
by: raylopez99 | last post by:
I need an example of a managed overloaded assignment operator for a reference class, so I can equate two classes A1 and A2, say called ARefClass, in this manner: A1=A2;. For some strange reason...
23
by: Jess | last post by:
Hello, I understand the default-initialization happens if we don't initialize an object explicitly. I think for an object of a class type, the value is determined by the constructor, and for...
43
by: JohnQ | last post by:
Are a default constructor, destructor, copy constructor and assignment operator generated by the compiler for a struct if they are not explicitely defined? I think the answer is yes, because...
9
by: George2 | last post by:
Hello everyone, I am wondering the default implementation of assignment operator (e.g. when we do not implement assignment operator in user defined class, what will be returned? temporary...
9
by: puzzlecracker | last post by:
From my understanding, if you declare any sort of constructors, (excluding copy ctor), the default will not be included by default. Is this correct? class Foo{ public: Foo(int); // no...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.