Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Uninitialized data member in test code #1444

Open
jwakely opened this issue Jul 9, 2024 · 0 comments
Open

Uninitialized data member in test code #1444

jwakely opened this issue Jul 9, 2024 · 0 comments
Assignees
Labels

Comments

@jwakely
Copy link
Contributor

jwakely commented Jul 9, 2024

template<typename OutputType, typename InputType = int>
struct copy_counting_object {
std::size_t copy_count;/*increases on every new copied object*/
mutable std::size_t copies_count;/*count number of objects copied from this object*/
std::size_t assign_count;
bool is_copy;
copy_counting_object():
copy_count(0), copies_count(0), assign_count(0), is_copy(false) {}
copy_counting_object(int):
copy_count(0), copies_count(0), assign_count(0), is_copy(false) {}
copy_counting_object( const copy_counting_object<OutputType, InputType>& other ):
copy_count(other.copy_count + 1), is_copy(true) {
++other.copies_count;
}

If I'm reading this correctly, the copy constructor does not initialize copies_count. That means if you make a copy of a copy, the new object will do ++other.copies_count; on an uninitialized variable.

Is there any reason not to initialize copies_count to zero here?

Using default member initializers for all four data members seems sensible.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants