[CSI] A structure with an array as a member variable.

Mikhail Nesterenko mikhail at cs.kent.edu
Wed Dec 14 16:42:12 EST 2016


> I was reviewing structures and how they are passed to a function and I came
> to a question about structures with an array as a member variable. Is there
> anything we can do to stop an array as a member variable of a structure
> from being passed by reference so that way we don't edit the array in the
> original structure? Kind of like the copy constructor for classes but for
> structures.

Actually, a structure may be passed by value or reference and even
returned by the function. This means that, depending on the structure
parameter passing discipline, the member array is passed by value by
reference or returned.  To prevent a function from modifying the
parameter, you need to use "const" parameter type modifier.

Now, this is the expected behavior and does not cause problems unless
structure members are pointers to dynamically allocated variables.

I do not believe I mentioned this in class, but in (modern) C++,
structures have the same functionality as class, including ability to
have member functions. This includes big three: copy constructor,
desructor, and assignment overloading.

However, it is considered good style to use structures for data only.

Thanks,
-- 
Mikhail


More information about the cs13001 mailing list