Results 1 to 4 of 4

Thread: [SOLVED] c++ - design question

  1. #1
    Join Date
    Mar 2008
    Beans
    170

    [SOLVED] c++ - design question

    Hey, I'm trying to write a class, called Country, which consists of a

    - string, which is the name of the country

    - vector<Country>, which is a vector of the neighboring countries.

    At the moment my class looks like this

    Code:
    #ifndef COUNTRY_H
    #define COUNTRY_H
    
    #include <vector>
    #include <string>
    
    class Country{
    
     public:
    
      Country();
      Country(std::string);
    
      std::string getName();
    
     private:
    
      std::string name;
    
      std::vector<Country> neighbours;
    
    };
    #endif
    Now I want to be able to add neighbours to a country, but I don't want the function to be public.

    To be more specific, I am going to create an Atlas class that will create, and store, the countries and read in the neighbours. So is there a way that I can add neighbours to a country from the atlas class, but not from anywhere else.

    Any help would be greatly appreciated.

  2. #2
    Join Date
    Dec 2008
    Beans
    6

    Re: c++ - design question

    Have a look at the friend modifier.

  3. #3
    Join Date
    Mar 2008
    Beans
    170

    Re: c++ - design question

    That looks like what I need, thanks. Seems like the best solution to me, but if anyone has any other ideas of how to do this then I would love to hear.

  4. #4
    Join Date
    Feb 2008
    Beans
    785
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: [SOLVED] c++ - design question

    I dont think it will work , because , Country in here :
    PHP Code:
    std::vector<Countryneighbours
    is an incomplete object.
    it should be :
    PHP Code:
    std::vector<Country*> neighbours

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •