1

I am new to C++, and I've seen a code like this

std::vector<Element*> *interactedElems

Where Element is a class. But not sure sure about the differences in the following cases:

  1. std::vector<Element> *interactedElems
  2. std::vector<Element*> interactedElems
  3. std::vector<Element*> *interactedElems
  4. std::vector<Element*> & interactedElems
1
  • 1
    Behind the scenes, pointers are fairly important to CPU's. C++ gives you direct access. However, we find that it's often not necessary to deal directly with pointers. std::vector<Element> will have a pointer internally, but you can safely ignore that. Commented May 13, 2015 at 7:38

2 Answers 2

2

A vector of Elements

std::vector<Element> elements;

You can add Elements to it. Assuming Element has default constructor, you can use:

elements.push_back(Element());

You can get the first Element from elements using:

Element e = elements[0];

Pointer to a vector of Elements

std::vector<Element>* elementsPointer;

In order to add Elements to elementsPointer, is has to point to something valid.

elementsPointer = new std::vector<Element>;
elementsPointer->push_back(Element());

You can get the first Element from elementsPointer using:

Element e = (*elementsPointer)[0];

or

Element e = elementsPointer->at(0);

A vector of pointers to Elements

std::vector<Element*> elementPointers;

You can add Element* to the vector.

elementPointers.push_back(new Element);

When you get the items from this vector, you get Element*s.

Element* ePtr = elementPointers[0];

Pointer to a vector of pointers to Elements

std::vector<Element*>* elementPointersPointer;

In order to add Element pointers to elementPointersPointer, is has to point to something valid.

elementPointersPointer = new std::vector<Element*>;
elementPointersPointer->push_back(new Element);

You can get the first Element* from elementPointersPointer using:

Element* ePtr = (*elementPointersPointer)[0];

or

Element* ePtr = elementPointersPointer->at(0);

Using a reference

Using a reference to any of the above forms is just like using references to any other types in C++.

Use can use:

std::vector<Element> elements;
std::vector<Element>& elementsRef = elements;

or any of the other variants.

Sign up to request clarification or add additional context in comments.

Comments

2
  1. is a pointer to a vector of Element objects.

The declaration itself is for a pointer to a container object. It contains individual Element objects.

  1. is a vector of pointers to Elements.

The declaration itself is for the container object. It contains pointers, each pointing to an Element object.

  1. is a pointer to a vector of pointers to Elements.

The declaration itself is a pointer. It points to a vector, which contains more pointers. These pointers are to actual element objects.

  1. is a reference to a vector of pointers to Elements.

This is just like #3, except that it is a reference to a vector instead of a pointer. A reference basically being a pointer that you can't re-point at something else.

1 Comment

in the first case, the declaration is not for a container, but for a pointer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.