473,387 Members | 3,750 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,387 software developers and data experts.

Removing Copy Constructor functionality

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

Aug 9 '06 #1
5 2425
sarathy wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 9 '06 #2
sarathy wrote:
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.
Aug 9 '06 #3
sarathy wrote:
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-lit...ds.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

Aug 9 '06 #4

mlimber wrote:
// My override. Doesnt work fine.
Employee(Employee&e) { }

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

Aug 9 '06 #5
Noah Roberts wrote:
mlimber wrote:
// My override. Doesnt work fine.
Employee(Employee&e) { }
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.
....or want to copy a temporary.

Cheers! --M

Aug 9 '06 #6

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

Similar topics

42
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same...
6
by: Jason Heyes | last post by:
What is a good way of removing elements from std::vector so that the elements removed satisfy a predicate and end up stored in another std::vector. It seems as though the algorithm std::remove_if...
2
by: pragtideep | last post by:
Kindly help me explain the behaviour of defult copy constructor . Why the destructor is freeing the SAME memory twice , though it was allocated just once . #include<iostream> using namespace...
5
by: Martijn van Buul | last post by:
Hi. I'm having a peculiar problem at work. I've been googling for it, but haven't found an authorative answer. In a nutshell (Long story follows), what I'd like to know is: If I have a C++...
13
by: Jeroen | last post by:
Hi all, I'm trying to implement a certain class but I have problems regarding the copy ctor. I'll try to explain this as good as possible and show what I tried thusfar. Because it's not about a...
2
by: saxman | last post by:
Hi everyone, I'm writing a class that inherits from std::vector in order to add some additional functionality. I'm having a compiler problem, however, when I have a function that returns this...
3
by: subramanian100in | last post by:
If we provide any ctor for a class the compiler does not supply the default ctor. However if we do not provide the copy ctor but provide any other ctor, the compiler still supplies the copy ctor....
22
by: clicwar | last post by:
A simple program with operator overloading and copy constructor: #include <iostream> #include <string> using namespace std; class Vector { private: float x,y; public: Vector(float u, float...
34
by: =?ISO-8859-1?Q?Marcel_M=FCller?= | last post by:
Hi, is there a way to avoid the automatic copy constructor generation. I do not want the object to be non-copyable. I simply do not want that copying is done by the default copy constructor. But...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.