Why this assignment work?


I have a struct like

} quad;

class MyObject
quad myValue;
const quad& getValue const{ return myValue;}

Do you think this assignment will work?

MyObject myObject;
1 quad temp = myObject.myValue;
2 quad temp = myObject.getValue();

I thought I have to do a memcpy every time, but it apparently is working in my program? How is it possible without a copy constructor to deal with the char array?
If both are correct, could you explain what is the difference between 1 and 2? I am confused on pass a reference of array to another array.


It looks to me that neither will work, because myValue & getValue are both private by default (absence of the access specifier) in the class.

You need to have an interface for your class.


PS - please use code tags - the <> button on the right under format.
my bad. type in a rush. Let me change it to

struct MyObject{
quad myValue;
const quad& getValue const{ return myValue;}

Thanks for the advice on code button. Never notice that.
arrays are perfectly copyable and assignable when they are wrapped in structs. That was one of the major innovations of C over B.

In C++, you can also use std::array<type, 8> to get an array wrapped in a struct with a container-compatible interface. (where type is the type of the elements of quadw that you didn't write)

There is no visible difference between 1 and 2, in both cases temp is copy-initialized from myObject.myValue

PS: the C++ standardese for this is § 12.8[class.copy]/15
Each base or non-static data member is copied/moved in the manner appropriate to its type:
— if the member is an array, each element is direct-initialized with the corresponding subobject of x;
Last edited on
thank you very much.
Topic archived. No new replies allowed.