fbpx

C++ interview question including operators like conversion operator and assignment operator

(Last Updated On: May 12, 2010)

Operators:
What’s the difference between assignment operator and copy constructor

copy constructor can do a deep copy whereas assignment is merely a shallow copy ie references.
wrong…
When not provided, compiler will provide both; which does bitwise copy

A copy constructor initialises a new object from an already existing object. An assignment operator is used to copy values from one object to an already existing object.
The key difference here is that for assignment operator the target object already exists.
Right, that’s from Effective C++

I think the difference lies in the fact that using a copy constructor allows you to make a deep copy while the assignment operator only makes a shallow copy.

A sample code for copy constructor.. though i have not made use of it for making a deep copy.. will post it soon

#include
#include
#include
using namespace std;

struct copy_variable
{
public:

int abc_1;
int abc_4;
char abc_2;
char abc_3[10];
copy_variable()
{
}
copy_variable(const copy_variable& some_object):abc_2(‘a’),abc_1(1000)
{
abc_1 = some_object.abc_1;
strcpy(abc_3,”something”);
abc_4 = 0;
cout<<"The values of abc_1, abc_2, abc_3\n" <
#include
#include
using namespace std;

struct copy_variable
{
public:

int abc_1;
int abc_4;
char abc_2;
char abc_3[10];
copy_variable()
{
}
copy_variable(const copy_variable& some_object):abc_2(‘a’),abc_1(1000)
{
abc_1 = some_object.abc_1;
strcpy(abc_3,”something”);
abc_4 = 0;
cout<<"The values of abc_1, abc_2, abc_3" <<"\t"< NOTE I now post my TRADING ALERTS into my personal FACEBOOK ACCOUNT and TWITTER. Don't worry as I don't post stupid cat videos or what I eat!

Don't miss out!
Get my TRADE LIKE A BOSS 2 PDF Books

You will received instantly the download links.

Invalid email address
Give it a try. You can unsubscribe at any time.

NOTE!

Check NEW site on stock forex and ETF analysis and automation

Scroll to Top