2010-12-11 8 views
7

私はC++にはとても新しいので、これは簡単に答えるかもしれません。私はクラス(Person)を作成しています。Personが作成されると、あらかじめ定義された名前の集合からランダムな名前が割り当てられます。だから、Personクラスの中で、私がランダムにアクセスできるストリングの静的なコレクションを定義したいと思います。そのため、そこにいくつあるか知る必要があります。私のクラスに静的なStringのコレクションを実装する方法

私はここでもQtを使用していますので、ソリューションは標準ライブラリまたはQtライブラリのものを使用することをお勧めします。

私は、Javaの背景からだと、Javaで私はおそらくのようなものだろう。この場合の等価だろう何

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

を?

答えて

21

QStringListを使用できます。

Person.h:

class Person 
{ 
private: 
    static QStringList names; 
}; 

Person.cpp:

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

C++ 03と仮定すると:C++ 0xのを想定し

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]); 

を:

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

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

をそしてもちろん、あなたの好きな文字列型の代わりconst char*を使用することができます。

+0

おかげで、彼らがすべて正しいことを考えると受け入れ答えを選ぶのは難しいでしたが、私はちょうどQStringListと一緒に行きました私がQtを学ぼうとしているという事実にうまく収まるので、純粋に答えてください。 – DaveJohnston

+0

@Dave:私はあなたを理解することができますが、可能であれば、より標準的でポータブルなコードを書くことをお勧めします。それほどきちんとしていないとしても(C++ 03の例)。 – ybungalobill

4

まず、静的配列からランダムな名前を生成するための非常に簡単なプログラム。適切なクラスの実装は、さらに下に見つけることができます。

#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]; 
} 

クラスの実装

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; 
} 
+0

良い説明ですが、 'using namespace std;'は悪い考えであり、多くの問題が名前空間の競合のためにここに掲載されています。'std ::'と打ち込むのは時間のかかるものです。 –

+3

"using namespace std;' "がヘッダファイル*に不正です。それをソースファイルで使用すると完全にうまくいきます。 –

+0

自然に、議論は ':)'に激怒しますが、http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-a-bad-practice-in-c –

関連する問題