分配不同类中的对象的值

时间:2017-11-17 17:33:31

标签: c++ class object

我正在尝试打印一个对象的xx, yy值,这些值从我分配它们的generator class获取这些值。例如:

Object.h

class Object {
public:
  // Define default constructors as this is an abstract class
  Object() = default;
  Object(const Object&) = default;
  Object(Object&&) = default;
  virtual ~Object() {};

  // Object properties
  glm::vec3 position{0,0,0};
  glm::vec3 rotation{0,0,0};
  /* I tried to define those variables here instead 
  of defining in cube.h but on print, values are never changed. */
  //int xx=-1;
  //int yy=-1;

protected:
  void generateModelMatrix();
};

Cube.h:

#include "object.h"

class Cube final : public Object{

private:
   //some code

public:
   int xx=-1;
   int yy=-1;

   Cube();

private:
};

Generator.cpp

#include "cube.h"
bool Generator::update() {
   for (int x = 0; x < 5; ++x) {
       for (int y = 0; y < 5; ++y) {
          auto obj = make_unique<Cube>();
          obj->position.x += 2.1f * (float) x - 4.0f; //this works fine
          obj->xx = x;   //In this case, I get debugger error for xx: -var-create: unable to create variable object.
          obj->yy = y;   //same error here
       }
    if(x==4) return false;
   }
return true;
}

Cube.cpp

#include "cube.h"

Cube::Cube(){
   printf("x: %d, y: %d\n", xx, yy);
}

在课程Cube中,我尝试打印出xxyy值,并为每个对象获得-1,显然它根本没有分配这些值。我做错了什么?

1 个答案:

答案 0 :(得分:3)

当对象创建时,将调用构造函数。 onInputValueChange(newValue: any):Number { return <Number>newValue; }调用会发生Cube个对象。

在创建对象后设置值,在make_unique构造函数返回后设置为long(相对来说)。