How should I implement a static collection of Strings in my class

I am very new to C++ so this might be an easy question to answer. I am writing a class (Person) and when a Person is created it should be assigned a random name from a collection of predefined names. So within the Person class I would like to define some sort of static collection of strings that I can access randomly and therefore I would also need to know how many there are.

I am using Qt here too, so the solution should preferably be using things from the standard library or the Qt library.

I am from a Java background and in Java I would probably do something like:

private static final String[] NAMES = { "A", "B" };

What would be the equivalent in this case?

Answers


You could use QStringList.

Person.h:

class Person
{
private:
    static QStringList names;
};

Person.cpp:

QStringList Person::names = QStringList() << "Arial" << "Helvetica" 
    << "Times" << "Courier";

Assuming C++03:

class YourClass {
    static const char*const names[];
    static const size_t namesSize;
};

// in one of the translation units (*.cpp)
const char*const YourClass::names[] = {"A", "B"};
const size_t YourClass::namesSize = sizeof(YourClass::names) / sizeof(YourClass::names[0]);

Assuming C++0x:

class YourClass {
    static const std::vector<const char*> names;
};

// in one of the translation units (*.cpp)
const vector<const char*> YourClass::names = {"A", "B"};

And of course you can use your favorite string type instead of const char*.


First, a very simple program for generating random names from a static array. The proper class implementation can be found further down.

#include <iostream>
#include <string>
#include <stdlib.h>
#include <time.h>

// import the std namespace (to avoid having to use std:: everywhere)
using namespace std;
// create a constant array of strings
static string const names[] = { "James", "Morrison",
                                "Weatherby", "George", "Dupree" };
// determine the number of names in the array
static int const num_names = sizeof(names)/sizeof(names[0]);

// declare the getRandomName() function
string getRandomName();

// standard main function
int main (int argc, char * argv[])
{
    // seed the random number generator
    srand(time(0));
    // pick a random name and print it
    cout << getRandomName() << endl;
    // return 0 (no error)
    return 0;
}

// define the getRandomName() function
string getRandomName()
{
    // pick a random name (% is the modulo operator)
    return names[rand()%num_names];
}

Class implementation

Person.h

#ifndef PERSON_
#define PERSON_

#include <string>

class Person
{
    private:
        std::string p_name;
    public:
        Person();
        std::string name();
};

#endif

Person.cpp

#include "Person.h"
#include <stdlib.h>

using namespace std;

static string const names[] = { "James", "Morrison",
                                "Weatherby", "George", "Dupree" };
static int const num_names = sizeof(names)/sizeof(names[0]);

Person::Person() : p_name(names[rand()%num_names]) { }
string Person::name() { return p_name; }

main.cpp

#include <iostream>
#include <string>
#include <stdlib.h>
#include <time.h>
#include "Person.h"

using namespace std;

int main (int argc, char * argv[])
{
    // seed the random number generator
    srand(time(0));

    // create 3 Person instances
    Person p1, p2, p3;

    // print their names
    cout << p1.name() << endl;
    cout << p2.name() << endl;
    cout << p3.name() << endl;

    // return 0 (no error)
    return 0;
}

Need Your Help

How to get the references of all already opened child windows

javascript window.open

I want to get the references of all already opened child windows. is there any way? I am not using child = window.open(....) just using window.open(....) and opening multiple child windows.

Django "xxxxxx Object" display customization in admin action sidebar

django django-models django-admin

I would like to change the default behavior of how the admin recent changes sidebar displays the name of "objects" added. Refer to the picture below: