Memcpy Vs Assignment Operator In Java

A copy assignment operator of class is a non-template non-static member function with the name operator= that takes exactly one parameter of type T, T&, const T&, volatile T&, or constvolatile T&. For a type to be , it must have a public copy assignment operator.

[edit]Syntax

class_nameclass_name ( class_name ) (1)
class_nameclass_name ( const class_name ) (2)
class_nameclass_name ( const class_name ) = default; (3) (since C++11)
class_nameclass_name ( const class_name ) = delete; (4) (since C++11)

[edit]Explanation

  1. Typical declaration of a copy assignment operator when copy-and-swap idiom can be used.
  2. Typical declaration of a copy assignment operator when copy-and-swap idiom cannot be used (non-swappable type or degraded performance).
  3. Forcing a copy assignment operator to be generated by the compiler.
  4. Avoiding implicit copy assignment.

The copy assignment operator is called whenever selected by overload resolution, e.g. when an object appears on the left side of an assignment expression.

[edit]Implicitly-declared copy assignment operator

If no user-defined copy assignment operators are provided for a class type (struct, class, or union), the compiler will always declare one as an inline public member of the class. This implicitly-declared copy assignment operator has the form T& T::operator=(const T&) if all of the following is true:

  • each direct base of has a copy assignment operator whose parameters are B or const B& or constvolatile B&;
  • each non-static data member of of class type or array of class type has a copy assignment operator whose parameters are M or const M& or constvolatile M&.

Otherwise the implicitly-declared copy assignment operator is declared as T& T::operator=(T&). (Note that due to these rules, the implicitly-declared copy assignment operator cannot bind to a volatile lvalue argument.)

A class can have multiple copy assignment operators, e.g. both T& T::operator=(const T&) and T& T::operator=(T). If some user-defined copy assignment operators are present, the user may still force the generation of the implicitly declared copy assignment operator with the keyword .(since C++11)

The implicitly-declared (or defaulted on its first declaration) copy assignment operator has an exception specification as described in dynamic exception specification(until C++17)exception specification(since C++17)

Because the copy assignment operator is always declared for any class, the base class assignment operator is always hidden. If a using-declaration is used to bring in the assignment operator from the base class, and its argument type could be the same as the argument type of the implicit assignment operator of the derived class, the using-declaration is also hidden by the implicit declaration.

[edit]Deleted implicitly-declared copy assignment operator

A implicitly-declared copy assignment operator for class is defined as deleted if any of the following is true:

  • has a user-declared move constructor;
  • has a user-declared move assignment operator.

Otherwise, it is defined as defaulted.

A defaulted copy assignment operator for class is defined as deleted if any of the following is true:

  • has a non-static data member of non-class type (or array thereof) that is const;
  • has a non-static data member of a reference type;
  • has a non-static data member or a direct or virtual base class that cannot be copy-assigned (overload resolution for the copy assignment fails, or selects a deleted or inaccessible function);
  • is a union-like class, and has a variant member whose corresponding assignment operator is non-trivial.

[edit]Trivial copy assignment operator

The copy assignment operator for class is trivial if all of the following is true:

  • it is not user-provided (meaning, it is implicitly-defined or defaulted) , , and if it is defaulted, its signature is the same as implicitly-defined(until C++14);
  • has no virtual member functions;
  • has no virtual base classes;
  • the copy assignment operator selected for every direct base of is trivial;
  • the copy assignment operator selected for every non-static class type (or array of class type) member of is trivial;
  • has no non-static data members of volatile-qualified type.
(since C++14)

A trivial copy assignment operator makes a copy of the object representation as if by std::memmove. All data types compatible with the C language (POD types) are trivially copy-assignable.

[edit]Implicitly-defined copy assignment operator

If the implicitly-declared copy assignment operator is neither deleted nor trivial, it is defined (that is, a function body is generated and compiled) by the compiler if odr-used. For union types, the implicitly-defined copy assignment copies the object representation (as by std::memmove). For non-union class types (class and struct), the operator performs member-wise copy assignment of the object's bases and non-static members, in their initialization order, using built-in assignment for the scalars and copy assignment operator for class types.

The generation of the implicitly-defined copy assignment operator is deprecated(since C++11) if has a user-declared destructor or user-declared copy constructor.

[edit]Notes

If both copy and move assignment operators are provided, overload resolution selects the move assignment if the argument is an rvalue (either a prvalue such as a nameless temporary or an xvalue such as the result of std::move), and selects the copy assignment if the argument is an lvalue (named object or a function/operator returning lvalue reference). If only the copy assignment is provided, all argument categories select it (as long as it takes its argument by value or as reference to const, since rvalues can bind to const references), which makes copy assignment the fallback for move assignment, when move is unavailable.

It is unspecified whether virtual base class subobjects that are accessible through more than one path in the inheritance lattice, are assigned more than once by the implicitly-defined copy assignment operator (same applies to move assignment).

See assignment operator overloading for additional detail on the expected behavior of a user-defined copy-assignment operator.

[edit]Example

Run this code

Output:

#include <iostream>#include <memory>#include <string>#include <algorithm>   struct A {int n;std::string s1;// user-defined copy assignment, copy-and-swap form A& operator=(A other){std::cout<<"copy assignment of A\n";std::swap(n, other.n);std::swap(s1, other.s1);return*this;}};   struct B : A {std::string s2;// implicitly-defined copy assignment};   struct C {std::unique_ptr<int[]> data;std::size_t size;// non-copy-and-swap assignment C& operator=(const C& other){// check for self-assignmentif(&other == this)return*this;// reuse storage when possibleif(size != other.size){ data.reset(new int[other.size]); size = other.size;}std::copy(&other.data[0], &other.data[0]+ size, &data[0]);return*this;}// note: copy-and-swap would always cause a reallocation};   int main(){ A a1, a2;std::cout<<"a1 = a2 calls "; a1 = a2;// user-defined copy assignment   B b1, b2; b2.s1="foo"; b2.s2="bar";std::cout<<"b1 = b2 calls "; b1 = b2;// implicitly-defined copy assignmentstd::cout<<"b1.s1 = "<< b1.s1<<" b1.s2 = "<< b1.s2<<'\n';}
a1 = a2 calls copy assignment of A b1 = b2 calls copy assignment of A b1.s1 = foo b1.s2 = bar

[edit]Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior
CWG 2171 C++14 operator=(X&)=default was non-trivial made trivial

1. Very good !!
View Tutorial          By: SGK at 2009-04-26 23:53:44

2. Its really good and helpfull..........
chee
View Tutorial          By: sumit at 2009-04-28 05:17:17

3. helpfull !!!!
View Tutorial          By: Anonymous at 2009-05-12 03:48:15

4. Awesome .
Helpfull!!
View Tutorial          By: ARMLAB-NG at 2009-06-06 09:22:00

5. Thanks that made this concept of memory very cleat
View Tutorial          By: Tarak at 2009-06-24 07:58:04

6. it was very helpful to fix one obscure bug
View Tutorial          By: ander13 at 2009-06-30 04:51:47

7. Helpful..........

Good
View Tutorial          By: Ravi at 2009-07-03 07:08:56

8. realy good explanation
View Tutorial          By: pandit at 2009-07-19 11:24:55

9. Thanks a lot.. it made my concepts of memmove,mems
View Tutorial          By: Rimmi Jain at 2009-07-23 06:42:01

10. Good and very clear explanation...
View Tutorial          By: Pavana at 2010-01-05 23:35:30

11. It's Very Nice. Particularly:
DO use memmov
View Tutorial          By: Rasmi Ranjan Nayak at 2010-01-12 04:40:18

12. Very clearly explained...
View Tutorial          By: Bina at 2010-04-08 06:56:37

13. This example is very good, but it doesnt work in M
View Tutorial          By: soma at 2010-04-13 04:34:00

14. Very good explanation specially overlapped memory
View Tutorial          By: MANISH RASTOGI at 2010-06-22 05:44:07

15. VERY USEFUL ONE....
View Tutorial          By: Anonymous at 2010-06-30 03:15:20

16. message1[] before memset(): Four score and seven y
View Tutorial          By: Mithun at 2010-07-05 02:56:27

17. Very good tutorial, but I have a small question he
View Tutorial          By: Mohammad NABIL at 2010-07-20 05:58:19

18. Excellent.
Just one question... What is the
View Tutorial          By: HeilGates at 2010-08-04 13:38:12

19. hey awesome.....................Thanku very much
View Tutorial          By: bharath at 2010-08-23 04:26:17

20. To 15:
Sometimes it is useful to return the
View Tutorial          By: Anderson Goulart at 2010-08-24 19:15:14

21. To 16:
size_t is an ISO specification and m
View Tutorial          By: Anderson Goulart at 2010-08-24 19:24:15

22. http://captchas.net/http://capthttp:http://captcha
View Tutorial          By: varun at 2010-09-06 08:49:23

23. Hello friends can anybody tell me how to assign in
View Tutorial          By: gnvkdramesh at 2010-09-10 04:27:46

24. Really good
View Tutorial          By: Sunita at 2010-10-24 23:27:11

25. Very good.
Nice explanation.
View Tutorial          By: toon at 2010-10-29 20:12:12

26. Neatly explained about the three important functio
View Tutorial          By: Jitendra Lenka at 2011-02-03 06:34:10

27. Really very good explanation..........
View Tutorial          By: deepika at 2011-02-08 22:56:48

28. could somebody please tell a case where memcpy() w
View Tutorial          By: freak at 2011-02-09 20:29:26

29. great info thankz...
View Tutorial          By: rohan at 2011-03-08 21:50:56

30. fine ..........
View Tutorial          By: rajesh at 2011-03-21 02:34:54

31. Really nice explanation...
nice to understa
View Tutorial          By: Parth Kansara at 2011-06-21 02:11:52

32. thnx
View Tutorial          By: Prakash at 2011-07-27 03:05:17

33. NICE.,
VERY HELPFUL.,
View Tutorial          By: siva at 2011-08-19 13:47:39

34. awesome...very good illustration
View Tutorial          By: dynamite at 2011-08-20 10:24:38

35. Excellent...
Great explanation...
View Tutorial          By: Dhaval Patel at 2011-09-04 06:49:12

36. I have initialized a int array while declaring.

View Tutorial          By: Prashanth at 2011-09-15 11:16:40

37. ans to 23:
by using gdb(GNU deb
View Tutorial          By: Bhargav at 2011-11-02 13:23:51

38. Out of -1, 0, and 1 which one can I use as second
View Tutorial          By: Sanjay Bhosale at 2011-12-06 13:17:54

39. To 25
void *memset( void *buffer, int ch, s
View Tutorial          By: Doniv at 2011-12-15 10:10:52

40. I did not get the same result for memcpy - e and f
View Tutorial          By: dml at 2012-01-02 13:32:20

41. very useful...!
View Tutorial          By: mahesh at 2012-01-06 09:55:59

42. hello it wrong in memmove nd memcpy case. I am get
View Tutorial          By: jaya at 2012-04-05 17:18:14

43. very nice..!
View Tutorial          By: neeraj at 2012-04-21 11:12:08

44. Nice Explanation.....
View Tutorial          By: Vinoth at 2012-06-20 09:37:21

45. Excellent !!!
Never thought about it...
View Tutorial          By: RahulC at 2012-07-03 11:36:57

46. Does memmove care if the source is bytes and the d
View Tutorial          By: Paul at 2012-07-20 11:59:32

47. Really nice tutorial for memset(), memcopy() and m
View Tutorial          By: Hitesh Kumar at 2012-09-03 09:16:32

48. Hi,
Very Nice explanation ,I tried both mem
View Tutorial          By: Harsh Verma at 2013-01-30 13:43:05

49. awesome.........
View Tutorial          By: tar at 2013-03-14 07:36:56

50. thumbs up!
View Tutorial          By: huang at 2013-03-28 04:02:33

51. Well explained. Example clear out the diff
View Tutorial          By: sonam at 2014-06-25 19:05:20

52. my requirement is i have a source string as src{&q
View Tutorial          By: Anil at 2014-10-27 08:36:23

53. What happens if applied to an integer array?
View Tutorial          By: SHUVO at 2015-07-01 21:14:16

54. Thanks...
View Tutorial          By: SHUVO at 2015-07-01 21:20:47

55. Excellent! Thanks.
View Tutorial          By: Swee at 2015-11-17 09:13:12

56. Thanks a lot! It is really clear ^^
View Tutorial          By: yello at 2015-11-24 17:17:07

0 Replies to “Memcpy Vs Assignment Operator In Java”

Lascia un Commento

L'indirizzo email non verrĂ  pubblicato. I campi obbligatori sono contrassegnati *