如何使用在构造函数中创建的对象

How to use object created in constructor

本文关键字:创建 对象 构造函数 何使用      更新时间:2023-10-16

假设您有两个类"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_;
};

这些对象超出了作用域,并在构造函数结束时被销毁。要在类的其他地方使用它们,请将它们声明为成员变量。