C++11 member variable of reference type, different behaviour after vector push_back

badger5000

I was using somebody else's class which was acting odd when I pushed it into a vector. It involves a member variable which is a reference to another member variable. Here is the smallest self-contained example:

#include <iostream>
#include <vector>

class Myclass {
public: 
  Myclass() : a(1.0) {}

  float a;
  float &a_ref = a;

  void addOne() {
    a = a + 1.0;
  }
};

int main() {
  Myclass instance1;
  instance1.addOne();

  //prints 2:
  std::cout << "instance.a_ref is " << instance1.a_ref << std::endl;

  std::vector<Myclass> vec;
  Myclass instance2;
  vec.push_back(instance2);

  vec.at(0).addOne();

  //prints 1;
  std::cout << "vec.at(0).a_ref is " << vec.at(0).a_ref << std::endl;
  return 0;
}

I was compiling with g++ and -std=c++11, so I didn't notice the problem for a while. I see now the issue is probably to do with the synthesised copy constructor and the reference member. But what I'm not sure about is:

  1. Why is there different behaviour when the object is in a vector ?
  2. Why does g++ not give any warnings about this, using c++11 standard ?

Bonus question because I'm curious:

  1. What is initialized first, a or a_ref?
Angew is no longer proud of SO

The problem is indeed with the defaulted copy constructor. The defaulted copy constructor initialises all members from the members of the source object. That is, the defaulted copy constructor is identical to this:

Myclass(const Myclass &src) :
  a(src.a),
  a_ref(src.a_ref)
{}

The defaulted copy constructor initialises all members, so it ignores any in-class initialisers.

This is also why pushing into a vector causes the problem. vec.at(0) was created as a copy of instance2, which means vec.at(0).a_ref refers to instance2.a. You could easily verify this by printing their addresses (live example).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

vector of reference wrapper, push_back failure?

From Dev

Behaviour of reference(&) data member pointing to stack variable

From Dev

C++ reference changes when push_back new element to std::vector

From Dev

Cant push_back() non copyable object to vector reference C++

From Dev

c++ push_back() a struct into a vector

From Dev

vector::push_back conversion from const Type* to type*

From Dev

Vector iterator incompatible after push_back() of a new object

From Dev

C++ Vector: push_back Objects vs push_back Pointers performance

From Dev

c++ vector - what's the difference between push_back(*new obj()) and push_back(obj())?

From Dev

push_back/append or appending a vector with a loop in C++ Armadillo

From Dev

push_back objects into vector memory issue C++

From Dev

function that push_back value to vector in c++

From Dev

c++ no matching function for call to vector push_back

From Dev

function that push_back value to vector in c++

From Dev

C++: vector<Foo*> with reserve(), push_back() destroying values

From Dev

vector string push_back is not working in c++

From Dev

vector push_back doesn't work c++

From Dev

c++ - runtime error: vector push_back()

From Dev

c++ push_back copying object instead of reference

From Dev

Type trait: Check if reference member variable is static or not

From Dev

c++11 vector of threads as class member

From Dev

Behaviour of the static reference variable

From Dev

C++11 How to use lambda and higher order functions to transform a vector to a vector of different type

From Dev

Is the behaviour of a setter for a member with same variable name determined in c++?

From Dev

c++11 how to use reference in vector?

From Dev

Forwarding a non-type argument causes different behaviour on Variable Template

From Dev

C++11 init templated member variable

From Dev

c++11 static functional member variable

From Dev

vector push_back in STL?

Related Related

  1. 1

    vector of reference wrapper, push_back failure?

  2. 2

    Behaviour of reference(&) data member pointing to stack variable

  3. 3

    C++ reference changes when push_back new element to std::vector

  4. 4

    Cant push_back() non copyable object to vector reference C++

  5. 5

    c++ push_back() a struct into a vector

  6. 6

    vector::push_back conversion from const Type* to type*

  7. 7

    Vector iterator incompatible after push_back() of a new object

  8. 8

    C++ Vector: push_back Objects vs push_back Pointers performance

  9. 9

    c++ vector - what's the difference between push_back(*new obj()) and push_back(obj())?

  10. 10

    push_back/append or appending a vector with a loop in C++ Armadillo

  11. 11

    push_back objects into vector memory issue C++

  12. 12

    function that push_back value to vector in c++

  13. 13

    c++ no matching function for call to vector push_back

  14. 14

    function that push_back value to vector in c++

  15. 15

    C++: vector<Foo*> with reserve(), push_back() destroying values

  16. 16

    vector string push_back is not working in c++

  17. 17

    vector push_back doesn't work c++

  18. 18

    c++ - runtime error: vector push_back()

  19. 19

    c++ push_back copying object instead of reference

  20. 20

    Type trait: Check if reference member variable is static or not

  21. 21

    c++11 vector of threads as class member

  22. 22

    Behaviour of the static reference variable

  23. 23

    C++11 How to use lambda and higher order functions to transform a vector to a vector of different type

  24. 24

    Is the behaviour of a setter for a member with same variable name determined in c++?

  25. 25

    c++11 how to use reference in vector?

  26. 26

    Forwarding a non-type argument causes different behaviour on Variable Template

  27. 27

    C++11 init templated member variable

  28. 28

    c++11 static functional member variable

  29. 29

    vector push_back in STL?

HotTag

Archive