這裏是處理對象陣列。我們有2個不同的班級F級和O級指針作爲成員覆蓋內存
class F {
private:
int x;
int y;
public:
int getXf(){ return x; }
int getYf(){ return y; }
f(int ,int);
};
class O {
private:
int n;
int k;
int x;
int y;
char type;
int id;
int t;
public:
O(int ,int ,int ,int ,int);
int getX(){ return x; }
int getY(){ return y; }
};
而且我們有第三個P類,我們初始化值。在這個類中我們創建了兩個對象數組。
class Prog {
public:
int count;
int fcount;
O *o[]; //here we are declaring the arrays of objects
F *f[];
public :
//void init(); Here is the function where we initializing the values
};
現在我們創建對象的語句的2。
for(int i=0;i<10;i++){
randx = rand() % 10;
randy = rand() % 20;
o[i] = new O(100,50,i,randx,randy);
}
for(int i=0;i<3;i++){
randx = rand() % 10;
randy = rand() % 10;
f[i] = new F(randx, randy);
}
當我們打印所有的對象都在這裏,但第一類的第一個3被秒的對象替換。恰好從分別randx
和randy
(第二對)的100
和50
(第一對)。
這不是你的問題,但你是否爲你的隨機函數申請了一顆種子? srand(time(NULL)); – Laurence
在StackOverflow上提出問題時,如果您發佈* actual *代碼,這將非常有幫助。例如,在這裏,您將'F'構造函數作爲小寫'f' - 但在您編譯和運行的實際代碼中不太可能出現這種情況。如果您發佈真實的代碼,我們可以幫助您更好。 – Borealid
數組是否真的被聲明爲'O * o [];'(無大小)或'O * o [10];'這是您的代碼所需要的? –