类中的枚举在调用自己的函数时不会改变
enum in class doesn't change when call own function
简短的问题描述
我有一个问题,当我更改枚举时,它没有改变。
它看起来只是一个局部变化。
如果我做下面的报价就可以了。
level = new level_1();
while(true)
{
cout << level->state << endl; // show '1' at first loop. Another loop show '2'
level->state = STATE_INITIALIZE;
}
但我必须改变他函数的状态
level = new level_1();
while(true)
{
cout << level->state << endl; // show '1' only
level.do();
}
level.do 功能
void Level_1::do()
{
state = STATE_INITIALIZE; // If this work, it should be '2'
// this->state = STATE_INITIALIZE;
}
所以这对我来说是一个大问题,它不会改变!!T_T
长代码
这是一些代码(几乎是完整的代码(
水平.h
enum LEVEL_STATE
{ STATE_LOAD = 1,
STATE_INITIALIZE = 2,
STATE_UPDATE = 3,
STATE_DRAW = 4,
STATE_FREE = 5,
STATE_UNLOAD = 6
};
class Level {
public:
LEVEL_STATE state = STATE_LOAD;
vector<Shader> shader;
vector<Model> model;
virtual void load()=0;
virtual void initialize()=0;
virtual void update()=0;
virtual void draw(Shader)=0;
virtual void free()=0;
virtual void unload()=0;
virtual Level* next()=0;
};
Map_Level_1.h
#include"Level.h"
class Level_1 : public Level {
public:
LEVEL_STATE state;
vector<Shader> shader;
vector<Model> model;
void load();
void initialize();
void update();
void draw(Shader);
void free();
void unload();
Level* next();
};
Map_Level_1.cpp
#pragma once
#include"Map_Level_0.h"
#include"Map_Level_1.h"
extern Camera camera;
extern float SCR_WIDTH;
extern float SCR_HEIGHT;
void Level_1::load()
{
shader.push_back(Shader("../Shader/alpha.vertex", "../Shader/alpha.fragment")); // this worked!
model.push_back(Model (FileSystem::getPath("res/arcade/arcade.obj"))); // this worked!
state = STATE_INITIALIZE; // this is not worked! the value doesn't change!
}
void Level_1::initialize()
{
shader[0].use();
state = STATE_UPDATE; // this is not worked! the value doesn't change!
}
void Level_1::update()
{
shader[0].use();
state = STATE_DRAW; // this is not worked! the value doesn't change!
}
void Level_1::draw(Shader shader)
{
glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 10000.0f);
glm::mat4 view = camera.GetViewMatrix();
glm::mat4 model;
shader.setMat4("projection", projection);
shader.setMat4("view", view);
shader.setInt("mode", 0);
//model = scale(vec3(5.0f, 5.0f, 5.0f));
shader.setMat4("model", model);
shader.setInt("fragMode", 1);
shader.setVec3("fragColor", vec3(0.92f, 0.92f, 0.92f));
this->model[0].Draw(shader);
state = STATE_UPDATE; // this is not worked! the value doesn't change!
}
void Level_1::free()
{
}
void Level_1::unload()
{
}
Level* Level_1::next() {
return new Level_0();
}
主.cpp
level = new Level_1();
while (!glfwWindowShouldClose(window))
{
DEV_processInput(window);
cout << level->state;
switch (level->state)
{
case STATE_LOAD:
level->load();
case STATE_INITIALIZE:
level->initialize();
case STATE_UPDATE:
level->update();
case STATE_DRAW:
level->draw(shader_1);
case STATE_FREE:
level->free();
case STATE_UNLOAD:
level->unload();
}
glfwSwapBuffers(window);
glfwPollEvents();
}
如果我1.(做一些容易解决
的错误2.(重复任何问题
我会道歉的..
您在基类中定义了state
成员,然后在子类中重新定义了同一成员,这导致了问题。
如果你有一个Level * level
指针,无论它指向哪个子类实例,你总是读取基类state
成员,同时你用他们的方法写入子类成员(即 do
(。
我建议仅在基类中定义state
(并使其protected
,也许(。
相关文章:
- 变量没有改变?通过向量的函数调用
- 为什么提升图库的 read_graphviz() 函数会改变节点的索引
- 为什么我的函数接受"std::string"进行排序不会改变它?
- 通过从构造函数中的'this'复制的指针改变常量对象
- 为什么我可以改变常量对象中的成员变量,这是返回常量对象函数的结果?
- 有没有办法创建一个不改变程序中参数的函数
- 为什么我的函数不改变对象的属性
- 类中的枚举在调用自己的函数时不会改变
- C - 如何将各处的静态函数称为改变其私有变量
- 基于继承的类型参数改变父虚拟函数的行为
- 对列表中的每个对象调用成员函数,以改变对象所在的列表
- 为什么调用 CUDA 内核函数时这个类成员变量没有改变
- 当函数应该改变对象的状态时,是选择成员函数还是朋友函数
- 一个什么都不做的函数可以改变一个对象吗?
- ifstream 函数"get"如何改变其字符参数?
- 为什么一个函数的末尾有常量这个词,即使它确实改变了一个变量
- visual C++未执行的代码会改变函数行为
- 不被类函数改变的私有int值
- 谷歌模拟函数改变参数值
- 被非const成员函数改变的可变变量