?
Solved

STL vector of obects ( NOT ptr to object!)

Posted on 2009-05-05
2
Medium Priority
?
545 Views
Last Modified: 2013-12-14
Hi Guys,

I'm writing a program which stores objects (not pointers to objects) in a vector, I have a few different iterators using the vector, and in some cases I need a pointer to the object which is in the vector.

In the example below I use the *funky* syntax

SType *s = &(*it);

The following are my objectives, I want the vector to manage the objects themselves, not ptr-to-objects, and I need to get a pointer to one of those objects for use outside of the vector itself.

Is there a more elegant syntax for the above?

Note that the following does not work!
SType *s = it;

Any ideas on tidying this up?
#include <vector.h>
 
using namespace std;
 
struct SType
{
  double d;
  // Some other funky stuff here, ok to perform copy operations
  // but prefer to avoid them where possible
};
 
vector<SType> VType;
 
void DoStuff(VType &V)
{
  for (int i = 0; i < 20; i++)
  {
    SType s;
    s.d = i * 100.0;
    V.push_back(s);
  }
}
 
// This function NEEDS to accept a ptr-to-object
// which modifies the content of the element in the vector
// because the function works in many other contexts of the program
// it can't accept an iterator etc, must be ptr-to-object
void DoubleMe(SType *S)
{
  S->d *= 2;
}
 
 
void Func()
{
  VType v;
 
  DoStuff(v);
 
  VType::iterator it;
  for (it = v.begin(); it != v.end(); ++it)
  {
    // Funky syntax to get a pointer to the underlying object
    SType *s = &(*it);
    DoubleMe(s);
  }
}

Open in new window

0
Comment
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 2

Accepted Solution

by:
Gregdo earned 1000 total points
ID: 24307703
SType *s = &(*it); is pretty much the only way to do it.  The main problem with this method is that if you add more data to the vector, then your pointer could become invalid.  Vectors use contiguous memory, so when you add items, it they do not fit in the currently available space, new memory will be allocated and the vector will be copied to the new memory and the existing memory will be freed which would then make your pointer invalid.

As long as you are certain that no new data is being added to the vector while you are using the pointer, then this will work.

AFAIK, your only other option would be to store pointers to your structure in the vector instead of the objects themselves.  This would then remove the possibility of memory moving when new elements are added to the vector.  It would then put the responsibility back on you to free all the pointers before the vector is deleted. You could write a class to contain your structure that handle all the memory allocation and deletion.
0
 
LVL 1

Author Closing Comment

by:A_Ridiculously_Long_Unique_Name
ID: 31577929
Great response! I hadn't considered the possibility of the pointer becoming invalid!!!
Due to the nature of the particular problem I am working with, no new data is being added so I think I can continue with this technique.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
Suggested Courses

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question