Removing Copy Constructor functionality

S

sarathy

Hi,
I need to see the output of the program when no copy
constructor is used. Since by default, there is an implicit one, how
should i perform the override, so as to totally remove the copy
constructor functionality.

Consider the following code.

class Employee
{
private:
char* designation;
int experience;
public:
Employee(char* dsgn, int exp)
{
experience=exp;
designation = new char[strlen(dsgn)];
strcpy(designation, dsgn);
}



char* getDesignation() { return designation; }
void setDesignation(char* dsgn) { strcpy(designation,
dsgn); }
int getExperience() { return experience; }
void setExperience(int exp) { experience=exp; }

// My override. Doesnt work fine.
Employee(Employee&e) { }
};

int main()
{
Employee trainee("Trainee",0);



Employee athi = trainee;
Employee vk = trainee;

cout << athi.getDesignation() << " with " <<
athi.getExperience() << " year experience" << endl;
cout << vk.getDesignation() << " with " << vk.getExperience()
<< " year experience" << endl;

return 0;
}

Output
----------

with 1108544020 year experience
Segmentation fault (core dumped)

As you can see, if no copy constructor is called, by default the
functionality should be a bitwise copy. i.e both should print same
results. But here it results in unexpected output with a seg fault.

Where am i going wrong?

Regards,
Sarathy
 
V

Victor Bazarov

sarathy said:
I need to see the output of the program when no copy
constructor is used. Since by default, there is an implicit one, how
should i perform the override, so as to totally remove the copy
constructor functionality.

Declare it, in the private section of the class. If it would be used,
then the compiler will complain.
Consider the following code.

class Employee
{
private:
char* designation;
int experience;
public:
Employee(char* dsgn, int exp)
{
experience=exp;
designation = new char[strlen(dsgn)];
strcpy(designation, dsgn);
}

Read about "the Rule of Three".
char* getDesignation() { return designation; }
void setDesignation(char* dsgn) { strcpy(designation,
dsgn); }
int getExperience() { return experience; }
void setExperience(int exp) { experience=exp; }

// My override. Doesnt work fine.
Employee(Employee&e) { }
};

int main()
{
Employee trainee("Trainee",0);



Employee athi = trainee;
Employee vk = trainee;

cout << athi.getDesignation() << " with " <<
athi.getExperience() << " year experience" << endl;
cout << vk.getDesignation() << " with " << vk.getExperience()
<< " year experience" << endl;

return 0;
}

Output
----------

with 1108544020 year experience
Segmentation fault (core dumped)

As you can see, if no copy constructor is called, by default the
functionality should be a bitwise copy.

No such thing.
i.e both should print same
results. But here it results in unexpected output with a seg fault.

Where am i going wrong?

You're not using 'std::string'. You should *immediately* abandon any
attempts to deal with dynamic memory yourself. You're not at that point
in your development yet. Use 'std::string'.

V
 
R

red floyd

sarathy said:
Hi,
I need to see the output of the program when no copy
constructor is used. Since by default, there is an implicit one, how
should i perform the override, so as to totally remove the copy
constructor functionality.

Declare the copy constructor private, and don't provide an implementation.
 
M

mlimber

sarathy said:
Hi,
I need to see the output of the program when no copy
constructor is used. Since by default, there is an implicit one, how
should i perform the override, so as to totally remove the copy
constructor functionality.

To add to what Victor said, *declare* it as private but don't *define*
it anywhere. You'll also want to disable the assignment operator the
same way.
Consider the following code.

class Employee
{
private:
char* designation;
int experience;
public:
Employee(char* dsgn, int exp)
{
experience=exp;
designation = new char[strlen(dsgn)];
strcpy(designation, dsgn);
}



char* getDesignation() { return designation; }
void setDesignation(char* dsgn) { strcpy(designation,
dsgn); }
int getExperience() { return experience; }
void setExperience(int exp) { experience=exp; }

The Creator says of this sort of thing: "I particularly dislike classes
with a lot of get and set functions. That is often an indication that
it shouldn't have been a class in the first place. It's just a data
structure. And if it really is a data structure, make it a data
structure" (http://www.artima.com/intv/goldilocks2.html). Should this
just be a simple struct? (Note also that at the very least, you should
be returning a const char*. Usually far better would be to use
std::string, as Victor suggested.)
// My override. Doesnt work fine.
Employee(Employee&e) { }

Whether this function is public or private, its parameter should almost
certainly be const (cf.
http://www.parashift.com/c++-faq-lite/coding-standards.html#27.10).
};

int main()
{
Employee trainee("Trainee",0);



Employee athi = trainee;
Employee vk = trainee;

cout << athi.getDesignation() << " with " <<
athi.getExperience() << " year experience" << endl;
cout << vk.getDesignation() << " with " << vk.getExperience()
<< " year experience" << endl;

return 0;
}

Output
----------

with 1108544020 year experience
Segmentation fault (core dumped)
GIGO.

As you can see, if no copy constructor is called, by default the
functionality should be a bitwise copy. i.e both should print same
results. But here it results in unexpected output with a seg fault.

But a copy constructor *is* called -- namely, the empty one you
specified. (Yes, on the surface, it looks like the compiler should call
the implicitly generated assignment operator instead, but because this
is initialization, it's actually the copy constructor that gets
called.)
Where am i going wrong?

Your copy constructor does nothing, and consequently, the data members
of your copied objects are uninitialized, which means they could be
anything. Dereferencing an uninitialized pointer is undefined behavior,
which happens to result in a segfault in this instance.

Cheers! --M
 
N

Noah Roberts

mlimber said:
Whether this function is public or private, its parameter should almost
certainly be const

Nah man...that's perfectly ok so long as you never declare a const
Employee.

:p
 

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
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top