473,805 Members | 1,896 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Rule of Thumb:Copy ctor,assingment opr

Dear all,

How do experienced programmers using this group use the rule of
thumb,namely copy ctor, assignment operator, and dtor.

example

if the class does not need them dont use and define them

use them but dont define them (for future revisions of the class as
placeholders)

or...

One more resource question on default and value iniliatization, I am
still looking references to read on these topics.

Regards,

Jun 22 '06 #1
9 1640
utab wrote:
Dear all,

How do experienced programmers using this group use the rule of
thumb,namely copy ctor, assignment operator, and dtor.
That's the Rule of *THREE*, not the Rule of thumb.
example

if the class does not need them dont use and define them

use them but dont define them (for future revisions of the class as
placeholders)

If I use them, I define them, even if they're trivial. That way I know
exactly what's happening, rather than relying on compiler magic.
Otherwise, I explicitly hide them.
Jun 22 '06 #2
In message <BU************ *******@newssvr 14.news.prodigy .com>, red floyd
<no*****@here.d ude> writes
utab wrote:
Dear all,
How do experienced programmers using this group use the rule of
thumb,namely copy ctor, assignment operator, and dtor.


That's the Rule of *THREE*, not the Rule of thumb.
example
if the class does not need them dont use and define them
use them but dont define them (for future revisions of the class as
placeholders)


If I use them, I define them, even if they're trivial. That way I know
exactly what's happening, rather than relying on compiler magic.
Otherwise, I explicitly hide them.


The compiler is more reliable than I am at keeping track of every single
data member of a large class. If they are trivial, I already know what
the "compiler magic" is, so why add clutter to the definition and risk
getting it wrong?

If the OP really does need to define them, he should consider defining a
swap function as well, and then defining the assignment operator in
terms of the copy constructor using the copy-and-swap idiom.

--
Richard Herring
Jun 23 '06 #3

red floyd wrote:
utab wrote:
Dear all,

How do experienced programmers using this group use the rule of
thumb,namely copy ctor, assignment operator, and dtor.


That's the Rule of *THREE*, not the Rule of thumb.
example

if the class does not need them dont use and define them

use them but dont define them (for future revisions of the class as
placeholders)

If I use them, I define them, even if they're trivial. That way I know
exactly what's happening, rather than relying on compiler magic.


That makes no more sense to me than "If I use a string class I define
one, rather than relying on <string> magic".

If the compiler "magic", which is no more magic than any other correct
compiler behaviour, does the right thing then let it. That's what it's
there for. If you write them yourself, you add nothing but risk
introducing bugs and confusing other programmers. Not to mention the
loss of your own time.

Gavin Deane

Jun 23 '06 #4
"utab" <um********@gma il.com> wrote in message
news:11******** **************@ g10g2000cwb.goo glegroups.com.. .
Dear all,

How do experienced programmers using this group use the rule of
thumb,namely copy ctor, assignment operator, and dtor.

example

if the class does not need them dont use and define them

use them but dont define them (for future revisions of the class as
placeholders)

or...

One more resource question on default and value iniliatization, I am
still looking references to read on these topics.

Regards,


As soon as I declare any pointer or reference in a struct/class I hide the
copy and assigment operators by declaring them private. I usually find it
non trivial to create the copy and assignemnt operators as I'm usually
loading some resource into a pointer that is not easy to duplicate. By
hiding them if I try to use them I'll get a compilation error.

Personally, I find it easier to use just pointers for these types of classes
when I stick them in vectors. It would probably be easier for me to use
smart pointers, but for some unknown reason I just don't trust smart
pointers to be smart enough, so do them manually.

What I normally do is start out any class as a struct if it is POD. As soon
as I need to create an initialization list, I change it to a class and
create a dtor.
Jun 23 '06 #5
utab wrote:
if the class does not need them dont use and define them

use them but dont define them (for future revisions of the class as
placeholders)

IMHO good rules are:
1. If class should not be copied by application logic - declare copy
ctor and operator =() as private and do not provide any implementation.
2. copy ctor and operator=() should be both either defined or not
defined.

Jun 23 '06 #6

utab wrote:
Dear all,

How do experienced programmers using this group use the rule of
thumb,namely copy ctor, assignment operator, and dtor.

example

if the class does not need them dont use and define them

use them but dont define them (for future revisions of the class as
placeholders)

or...

One more resource question on default and value iniliatization, I am
still looking references to read on these topics.

Regards,


I see that thereis still no compromise on this issue. I asked this
question after reading related parts from effective C++. I think that
if no dynamic allocation takes place then it is not wise to use these
because the compiler synthesizes them for you but even then maybe there
could be problems concerning memberwise and bitwise copy.

Still not an exact clarification, are there others to comment on the
subject?

Regards,

Jun 23 '06 #7
On Fri, 23 Jun 2006 07:17:30 -0700, "Jim Langston"
<ta*******@rock etmail.com> wrote:
As soon as I declare any pointer or reference in a struct/class I hide the
copy and assigment operators by declaring them private. I usually find it
non trivial to create the copy and assignemnt operators as I'm usually
loading some resource into a pointer that is not easy to duplicate. By
hiding them if I try to use them I'll get a compilation error.
This is a good rule of thumb. From the conceptual point of view you
implement object types (as opposed to value types) that way. For
object types copying makes no sense.
Personally, I find it easier to use just pointers for these types of classes
when I stick them in vectors. It would probably be easier for me to use
smart pointers, but for some unknown reason I just don't trust smart
pointers to be smart enough, so do them manually.
Trust you instincts. 'Smart' pointers create more problems than they
solve.
What I normally do is start out any class as a struct if it is POD. As soon
as I need to create an initialization list, I change it to a class and
create a dtor.


In C++ you hardly ever need to implement a copy constructor and an
assignment operator. Both are usually either trivial for value types
or you make them private and leave them unimplemented for object
types.

Best wishes,
Roland Pibinger
Jun 23 '06 #8
In article <pC************ **@baesystems.c om>,
Richard Herring <ju**@[127.0.0.1]> wrote:
In message <BU************ *******@newssvr 14.news.prodigy .com>, red floyd
<no*****@here.d ude> writes
utab wrote:
Dear all,
How do experienced programmers using this group use the rule of
thumb,namely copy ctor, assignment operator, and dtor.


That's the Rule of *THREE*, not the Rule of thumb.
example
if the class does not need them dont use and define them
use them but dont define them (for future revisions of the class as
placeholders)


If I use them, I define them, even if they're trivial. That way I know
exactly what's happening, rather than relying on compiler magic.
Otherwise, I explicitly hide them.


The compiler is more reliable than I am at keeping track of every single
data member of a large class. If they are trivial, I already know what
the "compiler magic" is, so why add clutter to the definition and risk
getting it wrong?


It can be even worse than that. Defining what would otherwise be
trivial members (copy, assignment or destructor) can lead to a
performance loss. Generic code (such as std::vector) can make
significant optimizations if it knows that its value_type has trivial
special members (which it can detect through the new <type_traits>
library. As soon as you define these special members, they are no
longer trivial (even if they do the same thing as the compiler generated
variant), and generic code must use larger and slower code to ensure
that everything works correctly.

-Howard
Jun 24 '06 #9
In message <11************ **********@c74g 2000cwc.googleg roups.com>, utab
<um********@gma il.com> writes

utab wrote:
Dear all,

How do experienced programmers using this group use the rule of
thumb,namely copy ctor, assignment operator, and dtor.

example

if the class does not need them dont use and define them

use them but dont define them (for future revisions of the class as
placeholders)

or...

One more resource question on default and value iniliatization, I am
still looking references to read on these topics.

Regards,
I see that thereis still no compromise on this issue. I asked this
question after reading related parts from effective C++. I think that
if no dynamic allocation takes place then it is not wise to use these
because the compiler synthesizes them for you but even then maybe there
could be problems concerning memberwise and bitwise copy.


The problems are usually related to resource ownership: it's not so much
how the low-level copy takes place, but what it means.
Still not an exact clarification, are there others to comment on the
subject?


Another comment: if your class must be copied, but has members whose
semantics mean that you _can't_ use the compiler-generated functions,
consider delegating the problem: either add to the member's class
appropriate copy/assign functions or wrap it in a new class that has the
same effect. Apply the same technique recursively to the members'
members until the non-standard copy semantics are isolated in the
smallest possible space.

--
Richard Herring
Jun 26 '06 #10

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

Similar topics

11
2345
by: Sam Wilson [Bentley] | last post by:
If you pass a C++ object by value as an argument to a function which has a variable-length argument list (...), the MSVC7 C++ compiler does not call the object's copy constructor and will not complain if the copy constructor is private. 1) Is this part of the C++ language definition? What is the thinking behind it? 2) In any case, how can I catch at compile time any callers that try to
1
2311
by: Tony Johansson | last post by:
Hello experts! I have this piece of code. No user defined copy constructor exist. AccountForStudent create(long number) { AccountForStudent local(number, 0.0); return local; } int main() {
10
2585
by: utab | last post by:
Dear all, So passing and returning a class object is the time when to include the definition of the copy constructor into the class definition. But if we don't call by value or return by value, we do not need to use the copy-constructor. So depending on the above reasoning I can avoid call by value and return by value for class objects, this bypasses the problem or it seems to me like that. Could any one give me some simple examples...
12
3102
by: Mark E. Fenner | last post by:
Hello all, I have a code where my inner loop looks like: allNew = for params in cases: newObj = copy(initialObject) newObj.modify(params) allNew.append(newObj) return allNew
0
1349
by: Gonçalo Rodrigues | last post by:
Hi all, I have a single-rooted hierarchy of heap-allocated objects -- call the root Object. These objects are handled via a smart pointer template Reference<T>. Basically, Reference<Tis a wrapper around T* with some extra smarts including reference counting to make memory management easier. Below, Reference<Objectwill be denoted simply by Ref. Since you only get your hands on Reference<Tand never on a T (or T*) directly, my thumb rule...
9
1681
by: blangela | last post by:
2.0 Sample Code class ABC // dummy class used below {}; class Example2 { public: Example2(); // default ctor Example2( const Example2 &); // copy ctor
1
2129
by: blangela | last post by:
3.0 Advanced Topic Addendum There are a few cases where the C++ compiler cannot provide an overloaded assignment operator for your class. If your class contains a const member or/and a reference member, the compiler will not be able to synthesize an assignment operator for your class. It actually helps to think of a reference member as a const member (since it cannot be made to reference any other object once it has been initialized). ...
1
2328
by: petschy | last post by:
hello, i've run into an error when qualifying a copy ctor 'explicit'. the strange thing is that i get a compiler error only if the class is a template and declare the variable as X<Zx = y. X<Zx(y) is fine. Tested with gcc 2.95, 3.3, 4.1, all gave the same error: t.cpp: In function 'int main()': t.cpp:44: error: no matching function for call to 'D<int>::D(D<int>&)'
25
2096
by: vikram Bhuskute | last post by:
I have plans to train some students for C in coming weeks. I am badly looking for C programming assignments fot them. Need 1) lots of them per topiic 2) Should be doable for beginners thanks in advance Vikram
0
10360
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10366
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9185
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7646
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5542
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5677
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4323
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3845
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.