Memberwise assignment

I have a few questions about memberwise assignment. First of all, what exactly is a memberwise assignment? Also, what is the syntax for it?
I didn't know that term existed, but after looking it up it turns out it is actually what it sounds like. All it is is assigning each member of one object to each corresponding member of the other object. The syntax is exactly as you would expect:
1
2
3
4
obj1.member1 = obj2.member1;
obj1.member2 = obj2.member2;
obj1.member3 = obj2.member3;
//... 
This is similar to how old-style copy assignment operators were implemented. In modern C++, copy assignment operators use copy-and-swap instead.
Last edited on
How would this work when you want to set one object of a class equal to another object of that class. Would it be done within the main function?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <string>

struct member
{
    member( const std::string& value, const std::string& name, const std::string& held_by )
        : value(value), member_name(name), containing_object_name(held_by) {}

    std::string value ;

    const std::string member_name ;
    const std::string containing_object_name ;

    member& operator= ( const member& that )
    {
        static int n = 0 ;
        std::cout << ++n << ". "
                  << this->containing_object_name << '.' << this->member_name << " = "
                  << that.containing_object_name << '.' << that.member_name << '\n' ;

        value = that.value ;
        return *this ;
    }
};

struct holder
{
    holder( const char* holder_name, const char* a, const char* b, const char* c )
       : one( a, "one", holder_name ),
         two( b, "two", holder_name ),
         three( c, "three", holder_name ) {}

    member one ;
    member two ;
    member three ;

    // *** the class does not have a user-defined assignment operator ***
    // *** the semantics of implicitly defined assignment is member-wise assignment
    // *** ie. assign members one by one in the order of declaration
};

int main()
{
    holder lhs( "lhs", "a", "b", "c" ) ;
    const holder rhs( "rhs", "dd", "eee", "ffff" ) ;

    std::cout << "value of members before assignment: " << lhs.one.value << ',' << lhs.two.value << ','
              << lhs.three.value << "\n\n" ; // value of members before assignment: a,b,c

    // assign rhs to lhs using the implicitly defined assignment operator
    lhs = rhs ; // this performs member-wise assignment
                // ie. assign members of rhs to the corresponding members in lhs
                // in the order of declaration of the members
                // 1. lhs.one = rhs.one
                // 2. lhs.two = rhs.two
                // 3. lhs.three = rhs.three

    std::cout << "\nvalue of members after assignment: " << lhs.one.value << ',' << lhs.two.value << ','
               << lhs.three.value << '\n' ; // value of members after assignment: dd,eee,ffff
}

http://coliru.stacked-crooked.com/a/51b4362697fd4207
Topic archived. No new replies allowed.