如何使用在构造函数中创建的对象
How to use object created in constructor
假设您有两个类"TwoDice"answers"Die"。您希望两个Die对象始终是TwoDice实例的一部分,因此您在构造函数中创建了两个Die对象。
TwoDice::TwoDice()
{
Die die1;
Die die2;
}
然后调用TwoDice对象的rollDice方法,该方法依次调用每个Die的roll方法。
bool TwoDice::rollDice()
{
faceValue1 = die1.roll();
faceValue2 = die2.roll();
}
目前的问题是,当我以这种方式设置它时,没有定义die1和die2,这是有意义的,因为它们只是构造函数中的局部变量。然而,当我让die1和die2为TwoDice类专门定义私有变量时,我收到了多个编译错误。有没有办法让这两个Die对象公开,这样其他方法就可以访问它们?
这是TwoDice.cpp文件:
// TwoDice.cpp: TwoDice class method definitions
#include "stdafx.h"
#include "time.h"
#include "TwoDice.h"
#include "Die.cpp"
#include "Die.h"
#include <cstdlib>
#include <iostream>
using namespace std;
TwoDice::TwoDice(void)
{
Die die1;
Die die2;
}
TwoDice::TwoDice(int d1, int d2)
{
Die die1(d1);
Die die2(d2);
}
void TwoDice::rollDice(void)
{
die1.roll();
die2.roll();
}
void TwoDice::getFaceValueDieOne(void)
{
faceValueDie1 = die1.getFaceValue();
}
void TwoDice::getFaceValueDieTwo(void)
{
faceValueDie2 = die2.getFaceValue();
}
bool TwoDice::isMatchingPair(void)
{
if(faceValueDie1 == faceValueDie2)
{
return true;
}
else
{
return false;
}
}
bool TwoDice::isSnakeEyes(void)
{
if(faceValueDie1 == 1 && faceValueDie2 == 1)
{
return true;
}
else
{
return false;
}
}
void TwoDice::display(void)
{
cout << "Die 1 = " << faceValueDie1 << endl;
cout << "Die 2 = " << faceValueDie2 << endl;
}
int TwoDice::getValueOfDice()
{
return faceValueDie1 + faceValueDie2;
}
这是TwoDice.h文件:
// TwoDice.h: class definition file
#pragma once
class TwoDice
{
private:
int faceValueDie1;
int faceValueDie2;
public:
TwoDice();
TwoDice(int, int);
void rollDice();
void getFaceValueDieOne();
void getFaceValueDieTwo();
bool isMatchingPair();
bool isSnakeEyes();
void display();
int getValueOfDice();
};
这是Die.cpp:
// Die.cpp: Die class method definitions
#include "stdafx.h"
#include "time.h"
#include "Die.h"
#include <cstdlib>
using namespace std;
Die::Die(void)
{
numSides = 6;
faceValue = 0;
srand((unsigned int)time(NULL));
}
Die::Die(int n)
{
numSides = n;
faceValue = 0;
srand((unsigned int)time(NULL));
}
int Die::roll()
{
faceValue = rand()%numSides + 1;
return faceValue;
}
int Die::getFaceValue()
{
return faceValue;
}
这是Die.h:
// Die.h: class definition file
#pragma once
class Die
{
private:
int numSides;
int faceValue;
public:
Die();
Die(int n);
int roll();
int getFaceValue();
};
所有的构造函数代码似乎都声明了堆栈变量。将类更改为具有Die成员变量。
class TwoDice
{
private:
int faceValueDie1;
int faceValueDie2;
Die die1;
Die die2;
// then as your code
然后按照以下更改构造函数
TwoDice::TwoDice()
{
}
TwoDice::TwoDice(int d1, int d2)
: die1(d1),
die2(d2)
{
}
第二个构造函数告诉Die
构造函数哪个int使用
使它们成为成员变量。
class Die
{
public:
// construct the die, given a face value
Die(int value) : face_value_(value)
{
}
int get_value() const
{
return face_value_;
}
// randomize the face value based on rolling
void roll()
{
face_value_ = 1 + (rand() % number_of_faces_);
}
private:
int face_value_;
};
// encapsulates two dice
class TwoDice
{
public:
// construct each one (one_ and two_) with a specific starting value
TwoDice(int fv_one, int fv2) : one_(fv_one), two_(fv_two)
{
}
// roll (both)
void roll()
{
one_.roll();
two_.roll();
}
// These objects are member variables. They are
// owned by a specific instance of this class.
Die one_;
Die two_;
};
这些对象超出了作用域,并在构造函数结束时被销毁。要在类的其他地方使用它们,请将它们声明为成员变量。
相关文章:
- 使用基类指针创建对象时,缺少派生类析构函数
- 如何创建对象函数指针C++映射?
- C++创建对象数组
- 在 C++ 的 Switch Case 中创建对象后对对象调用方法
- 如何获取在 main() 函数中构造的类的创建对象?
- 基于文件中的条目创建对象
- 错误:创建对象后无法分配区域
- C++ 通过输入创建对象
- 堆还是堆栈用于创建对象?
- 使用 C++ 创建对象数组
- 使用unique_ptr创建对象
- C++递归地在类构造函数中创建对象
- 通过向构造函数其他对象引用页面来创建对象
- ReactiveX (rx) - 在对象上应用可观察对象,而不是在可观察对象中创建对象
- 如何在OSX上正确创建C++对象文件(.o)
- 编译器是否会创建vtable,而不考虑在c++中创建对象
- 创建对象并防止被破坏
- 在C++中,友元类可以从友元类创建对象吗
- 只在堆中创建C++对象
- 创建用户定义的复制构造函数时无法创建对象