Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7840375
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:45:43+00:00 2026-06-02T15:45:43+00:00

If I wanted a class that had a vector of pointers to other classes

  • 0

If I wanted a class that had a vector of pointers to other classes of the same type that could allow a cyclical cycle, how dangerous is it? For example, say I have a text file that looks like this:

city=Detroit
{
    sister=Toyota
    sister=Dubai
    ...
}
...

First the file is read into a series of temp classes, ParsedCity, where the name of the city and the names to the sister cities are held. After I have all the cities in the file I create the actual City class.

class City
{
    private:
        std::string name;
        std::vector<City*> sisterCities;
    public:
        City(const std::string& aName);
        CreateRelations(const ParsedCity& pcs); 
        std::string Name() const { return name; }
};

//If this were to represent Detroit, pc would contain a vector of strings
//containing Toyota and Dubai.  Cities contain the actual classes that sister
//cities should point to.  It holds all cities of the world.
City::CreateRelations(const ParsedCity& pc, std::vector<City>& cities)
{
    for (unsigned int i = 0; i < pc.ParsedSisterCities().size(); i++)
    {
        for (unsigned int j = 0; j < cities.size(); j++)
        {
            if (pc.ParsedSisterCities()[i] == cities[j].Name())
            {
                sisterCities.push_back(&cities[j]);
                break;
            }
        }
    }
}

My worry is that if more cities are pushed into the main City vector, that the vector will re-size, relocate to somewhere else, and all my Cities will be pointing to sisterCities that are dangling pointers. At least this is my thought that will happen based on my knowledge of the vector class. If all the cities in the world and sister cities were stored in a linked-list would this solve my problems? I would like a guarantee that once a city is built it does not move (in memory. Bad pun?)

This seems like a tricky problem to me. As if I call a sister city of Detroit, I can call it’s sister cities, etc. Then I could end up back at Detroit! If Topeka changes its name to Google, all of Topeka’s sister cities should automatically know (as they are all pointing at the same spot in memory that Topeka is located).

Any advice is appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T15:45:45+00:00Added an answer on June 2, 2026 at 3:45 pm

    If you have a vector of pointers, and the vector resizes, the pointees locations in memory are not affected, so all your pointers remain valid.

    The biggest problem with this solution is that any recursive algorithm you apply to your data-structure will have to have some mechanism to detect cycles, otherwise you will end up with stack-overflows due to infinite recursion.

    Edit:

    I just realized that I misread your question originally. If the cities-vector resizes, any pointer to its elements will become invalid. The best alternative would be store pointers to cities in that vector. To make this more manageable, I suggest you use a boost::ptr_vector. This has the benefit that pointers to cities remain valid even if you delete a city from your vector, or you reorder the cities in your vector (for instance if you want to sort them by name for faster lookup).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So, I wanted to extend the Python smtpd SMTPServer class so that it could
I have a class built from a hierarchy of other classes, and I had
If I wanted to create a simple class that has three instance variables and
Say, for example purposes I wanted a list of items that each had a
Normally if I had Class 1 and I wanted to use it elsewhere, I
I had the following line in a class that I was using. private static
I wanted a numeric keypad that had a go or done button that closed
I wanted to create my own Python exception class, like this: class MyException(BaseException): def
I wanted to set a CSS class in my master page, which depends on
I wanted to serialize a java class to XML with XStream. I needed to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.