通过向类变量传递引用来初始化它
Initialize class variable by passing a reference to it
我有一个非常有趣的C++练习:
这是强加的主要:
int main(void)
{
Exam e = Exam(&Exam::cheat);
e.kobayashiMaru = &Exam::start;
(e.*e.kobayashiMaru)(3);
Exam::cheat = true;
if (e.isCheating())
(e.*e.kobayashiMaru)(4);
return (0);
}
这是要求的输出:
[The exam is starting]
3 Klingon vessels appeared out of nowhere.
You lost again.
[The exam is starting]
4 Klingon vessels appeared out of nowhere.
Win !
现在需要创建Exam
类来获得正确的输出。以下是我所做的:
class Exam
{
public:
Exam(bool *_cheat);
typedef void (Exam::*func)(int);
void start(int);
bool isCheating();
static bool cheat;
func kobayashiMaru;
};
我遇到了Exam(&Exam::cheat)
的问题。到目前为止,我所理解的是Exam
取的是它自己的cheat
变量的地址。当输入Exam
的构造函数时,cheat
未初始化。所以对我来说,我会在这里用false
初始化它。
Exam::Exam(bool * _cheat)
{
*_cheat = false;
}
但通过这样做,我得到了Exam(&Exam::cheat)
的多重定义。我不确定我的反思,也许有人能启发我这里到底发生了什么吗?
更改:
static bool cheat;
至:
bool cheat;
在Exam
类中,允许每个新对象用自己的值处理自己的cheat
变量。
构造函数将根据给定值初始化cheat
变量,同时使用如下构造函数创建新对象:
Exam::Exam(bool isCheating)
{
this->cheat = isCheating;
}
或者,如果您想在默认情况下将cheat
变量初始化为false/true,您可以制作这样的构造函数:
Exam::Exam()
{
cheat = false;
//or cheat = true;
}
您也可以处理多个构造函数。
现在要从Exam
类创建新对象,请使用:
Exam *exam1 = new Exam(true); //Will initialize cheat as false
Exam *exam2 = new Exam(false); //Will initialize cheat variable as true
然后访问exam1和exam2对象中的方法,如下所示:
exam1->methodName(1243);
exam2->secondMethodName(true);
exam2->thirdMethodName();
exam3->addFriend(&exam1);
这些只是例子。
希望我能理解你的问题
您有一些问题(在下面的评论中描述)
class Exam
{
public:
// If cheat is supposed to be a member variable assigned to each instance, passing a pointer to itself is silly and unnecessary
// If cheat is supposed to be shared between all instances of exam, passing a pointer to itself is silly and unnecessary
Exam(bool *_cheat);
static bool cheat;
// these two declarations are unnecessary. They do nothing but add complexity where it is not needed
typedef void (Exam::*func)(int);
func kobayashiMaru;
void start(int);
bool isCheating();
};
我遇到了考试的问题。我到目前为止的理解是,Exam正在获取自己作弊者的地址变量当进入考试的构造函数时,作弊是未初始化的。所以对我来说,我会在这里用false初始化它。
静态变量在创建类的第一个实例之前进行初始化。问题有两个方面:1)您没有初始化静态成员变量,2)您没有将其视为静态成员变量。
将变量声明为static
意味着它将在类的所有实例之间共享(例如,cheat
将有一个内存位置)。除非你想让每个服用Exam
的人在一个人作弊时就作弊,否则这不太可能是你想要的。
要修复它,您需要Exam
类看起来更像这样:
class Exam
{
public:
Exam(bool cheat) : m_cheat(cheat) {}
// don't expose member variables directly - use accessor functions
void setCheating(bool cheat) { m_cheat = cheat; }
bool isCheating() const { return m_cheat; }
void start(int);
private:
bool m_cheat;
};
main
功能的相应更改
int main(void)
{
Exam e(false);
e.start(3);
e.setCheating(true);
if (e.isCheating())
e.start(4);
return 0;
}
- C++-模板嵌套类的引用初始化无效
- 在引用初始化中使用已删除的复制构造函数进行复制初始化
- 模板流运算符重载错误:引用初始化无效,与basic_istream和basic_ifstream之间的差异有关
- 具有字符串文本构造函数的类不适用于 const 引用初始化
- 类型为"短整型 (&)"的引用初始化无效
- 引用初始化和常量表达式
- 在Visual Studio 2013中为rvalue引用初始化捕获
- 从大括号括起来的初始值设定项列表进行的Lvalue引用初始化无法编译
- Visual C++ 2015 中右值的非常量引用初始化无效
- 是否可以使用对派生类实例的基类引用初始化派生类引用
- 类型'int&'的引用初始化无效,传递参数 1 时出错
- 引用初始化表单
- 从类型为"int*"的临时引用初始化类型为"int&"的非常量引用无效
- 简单的C++日志记录类-ostream引用初始化
- 错误:类型为"cv::Mat&"的非常量引用初始化无效
- 为什么我收到类型为"const vec&"的引用初始化无效
- 从类型为"char*"的临时引用初始化类型为"char*&"的非常量引用
- 为什么这会发送一个关于引用初始化无效的错误
- 为什么此处不对引用初始化执行复制初始化?
- std::vector不会为多个向量条目创建cv::Mat的新引用——初始化矩阵时,数据会被覆盖