Hi,
How, in general, does the default operator= work?
That is, say I have a custom class MyClass and do:
MyClass defaultMyClass;
MyClass initializedMyCl ass("set","some ","members" );
defaultMyClass = initializedMyCl ass;
Will that final line copy all of the members if initialized class into
defaultMyClass? A deep copy? If not, how does it work?
Thanks,
cpp 5 2109
cppaddict wrote: Hi,
How, in general, does the default operator= work?
That is, say I have a custom class MyClass and do:
MyClass defaultMyClass; MyClass initializedMyCl ass("set","some ","members" ); defaultMyClass = initializedMyCl ass;
Will that final line copy all of the members if initialized class into defaultMyClass? A deep copy? If not, how does it work?
The default generated operator= does the only sensible thing
it can do: do a memberwise assignment.
It depends on the internals of your class if this is a deep copy
or not.
--
Karl Heinz Buchegger kb******@gascad .at
cppaddict wrote: How, in general, does the default operator= work?
In invokes operator= semantics for every base class and every member,
in the declaration order.
That is, say I have a custom class MyClass and do:
MyClass defaultMyClass; MyClass initializedMyCl ass("set","some ","members" ); defaultMyClass = initializedMyCl ass;
Will that final line copy all of the members if initialized class into defaultMyClass? A deep copy? If not, how does it work?
It's called "member-wise assignment". It all depends on the types used
and whether the assignment operators are defined to do anything different
than the default behaviour.
Also, since it has some bearing on it, read about "The Rule of Three".
Victor
>It depends on the internals of your class if this is a deep copy or not.
Thanks for your reply.
What does ithe deepness of the copy depend on? Eg, if one of the
members of my class is a vector which contains other custom objects,
will a deep copy of the vector be made?
Thanks,
cpp
"cppaddict" <he***@hello.co m> skrev i en meddelelse
news:8t******** *************** *********@4ax.c om... It depends on the internals of your class if this is a deep copy or not.
Thanks for your reply.
What does ithe deepness of the copy depend on? Eg, if one of the members of my class is a vector which contains other custom objects, will a deep copy of the vector be made?
Thanks, cpp
Yes. Any standard collection will be copied correctly. Pointers are copied
too, but their content is not - and this might be problematic wrg e.g.
ownership issues.
/Peter
cppaddict wrote: It depends on the internals of your class if this is a deep copy or not.
Thanks for your reply.
What does ithe deepness of the copy depend on? Eg, if one of the members of my class is a vector which contains other custom objects, will a deep copy of the vector be made?
Yes.
As a simple rule of thumb.
If you don't need to do any special action in the destructor
to handle a specific member variable, then the copy constructor
and the assignment operator generated by the compiler will do
the right thing.
To your question: Assume there is a std::vector in your
class. You don't need to do anything in the destructor
to free that vector, because the vector handles
everything by itself. But so does it handle everythign
for itself when it is assigned to another vector.
When the compiler generates the code for op=, it uses
memberwise assignment to do this. This means that it
will tell the vector to assign itself to another vector
and since a vector knows how to do this, everything
is correct.
PS)
The above is called: rule of three
Whenever you need one of
* destructor
* copy constructor
* assignment operator
you most likely need all 3 of them
--
Karl Heinz Buchegger kb******@gascad .at This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Steven Bethard |
last post by:
I thought it might be useful to put the recent lambda threads into
perspective a bit. I was wondering what lambda gets used for in "real"
code, so I grepped my Python Lib directory. Here are some of the ones I
looked, classified by how I would rewrite them (if I could):
* Rewritable as def statements (<name> = lambda <args>: <expr> usage)
These are lambdas used when a lambda wasn't needed -- an anonymous
function was created with...
|
by: fastwings |
last post by:
mm the code
//////makemenu.h////
class menu {
public:
int op;
pmenu(int op,int sub = 0)
{
switch op
{
|
by: Peteroid |
last post by:
Why does reading a member of a std::map not considered const? For example:
class My_Class
{
int Get_Map_Value( int index ) const // ** error ** not considered
const!!!
{
return m_Map ; // note that m_Map is not changed, only read
from
}
|
by: baibaichen |
last post by:
i have written some code to verify how to disable slicing copy
according C++ Gotchas item 30
the follow is my class hierarchy, and note that B is abstract class!!
class B
{
public:
explicit B(INT32 i =0):i_(i){}
virtual ~B(){}
|
by: Ajay |
last post by:
Hi all,
i want to know when i create a class.what all it contains.I
know the following things are there by default if i do not declare them
by myself.Please tell the other things that are left.
1. constructor
2.Destructor
3.Copy constructor
4. Assignment operator
| |
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
{
}; ?
|
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 the built-in types,
the value is usually garbage. Is this right?
However, I'm a bit confused about value-initialization, when does it
happen, and what kind of values are assigned to objects?
|
by: Skybuck Flying |
last post by:
Hello,
Visual Studio .Net 2005 (Win32) Compile error:
Error 1 error C2804: binary 'operator +' has too many parameters <snipped>
line 16
class TSkybuckInt32
{
private:
|
by: Peng Yu |
last post by:
Hi,
In the following code, the 'copy' member function works. But the '='
operator does not work. Can somebody let me know why a member function
is different from an operator.
Thanks,
Peng
#include <iostream>
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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...
|
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,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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...
|
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...
|
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
| |
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...
| |