0

If I have a class:

Class aClass
{
    vector<aClass> connections;
}

with everything else declared properly, if I were to do:

aClass a = new aClass();
aClass b = new aClass();

b.connections.push_back(a);

would this create a reference to a or would it duplicate the class.

I would like it to be a pointer to the class but I wasn't sure if I needed extra syntax to ensure this. I remember reading that when a class is declared aClass a = new aClass it is creating a pointer to the object in the heap but I wasn't sure what would happen here.

For reference this is for something like a linked list.

Also if anyone can think of a better title for this question go ahead and edit it.

songyuanyao
  • 169,198
  • 16
  • 310
  • 405
Cjen1
  • 1,826
  • 3
  • 17
  • 47

4 Answers4

2
  1. connections should be vector<aClass*>, aClass a should be aClass* a.

  2. Then, for b.connections.push_back(a);, the value of the pointer will be copied, not the pointee (i.e. the object being pointed).

  3. I'll suggest you to use smart pointers instead of raw pointers, if you do want to use pointers, like std::vector<std::shared_ptr<aClass>>.

songyuanyao
  • 169,198
  • 16
  • 310
  • 405
  • Thanks, I haven't done much c++ programming for a few years so I couldn't remember exactly what would happen here. – Cjen1 Jun 11 '16 at 15:36
  • As a question about pointers, if a program is closed and there are still pointers referencing objects, will those objects be deleted and garbage collected? or will they cause a memory leak – Cjen1 Jun 11 '16 at 15:38
  • @Cjen1 If you use raw pointer, `new` sth but don't `delete` it, then will cause memory leak. That's why I suggested smart pointers. – songyuanyao Jun 11 '16 at 15:40
  • @sonyuanyao Even when the program that was referencing it is closed? – Cjen1 Jun 11 '16 at 15:41
  • @Cjen1 If the program ends the memory will be collected by OS. But the destructor of class won't be invoked and might be a potential problem. Anyway it's not a good idea in C++. – songyuanyao Jun 11 '16 at 15:45
  • @Cjen1 Actually, it depends on how the objects being pointed at were instantiated. – juanchopanza Jun 11 '16 at 16:09
0

First, vector<aClass> is not a vector of classes, it's a vector of instances of a class.

Second, aClass a = new aClass(); is hard to make to compile in C++. In most cases a default constructed instance of a class is created simply as aClass a;, and new aClass() usually returns a pointer to a fresh instance of the class.

Third, even if you push_back an instance to the vector, it is copied.

For default-constructed objects, you can construct a fresh object at the end of the vector by resizeing the vector by 1.

Since 2011, you can construct an instance via non-default constructor (with non-empty set of construction arguments) directly inside the vector using emplace_back.

bipll
  • 11,747
  • 1
  • 18
  • 32
0

If the code is made to be compilable, which I think what you intended then:

The documentation says that is has 2 overloads

void push_back( const T& value );
void push_back( T&& value );

So as long as you don't explicitly std::move or type cast or push_back return value from a function then the first overload will trigger, which means that copying push_back will be called. Otherwise the second, moving will be called.

EDIT: type cast and std::move is the same thing under the hood. But, it preserves const correctness, thus if you were to pass const T, then std::move will make it const T&&, which is very dangerous in overload resolution.

You shouldn't pass const T&& ever, about why read here.

Community
  • 1
  • 1
Incomputable
  • 2,188
  • 1
  • 20
  • 40
0
aClass a = new aClass();
aClass b = new aClass();

b.connections.push_back(a);

It will not even compile. As someone mentioned you should write aClass *a and aClass *b.

    aClass a;
    aClass b;

    b.connections.push_back(a);

This will copy the value to the vector, because your vector holds aClass objects (values, not addresses). It doesn't copy any address. However, if you did something like this:

vector<aClass*> connections;
aClass *a = new aClass();
aClass *b = new aClass();
b.connections.push_back(a);

Then a vector will hold a pointers to aClass objects (instances of aClass). So in this case connections[0] will point to a object. And if you do this::

 cout << connections[0]<< endl;
cout << a << endl;

Then these lines are equal. They will print the same thing (same address).

Finally, if you delete that a object:

delete a;

Then it will point to that place in memory that object was placed in, but there is no a object anymore. So you can not do:

cout << a << endl;

But you still can do:

cout << connections[0] << endl;

And it will print the same address as before.

MindRoller
  • 241
  • 4
  • 15