Home  >  Q&A  >  body text

c++ - 这两段代码谁能看出区别?

glm::vec3 cameraPos = glm::vec3(1, 1, 3);
glm::vec3 cameraFront = glm::vec3(-1.0f, -1.0f, -3.0f);

glm::vec3 center;
center = cameraPos + cameraFront;
view = glm::lookAt(cameraPos, center, cameraUp);
glm::vec3 center = cameraPos + cameraFront;
view = glm::lookAt(cameraPos, center, cameraUp);

这个view是相机视图矩阵,上一段代码能显示出物体,下一段就是显示不出来,

glm::vec3 center;
center = cameraPos + cameraFront;

glm::vec3 center = cameraPos + cameraFront;

对象赋值上面两种方式有何区别?编译器 apple llvm 7.0

PHP中文网PHP中文网2764 days ago568

reply all(2)I'll reply

  • 大家讲道理

    大家讲道理2017-04-17 13:19:56

    glm::vec3 center;                  // 调用默认构造函数(default constructor)创建 center
    center = cameraPos + cameraFront;  // 调用 operator= 拷贝赋值运算符
    
    glm::vec3 center = cameraPos + cameraFront;  // 调用拷贝构造函数(copy constructor)创建 center
    

    This problem may occur if the implementation of glm::vec3's copy constructor and copy assignment operator are different. For example, if dynamic memory is used in glm::vec3, you must implement the copy constructor and copy assignment operator yourself. The default is shallow copy.

    According to the title description

    The previous piece of code can display the object, but the next piece of code cannot display it

    I guessglm::vec3This class is likely to use the default copy constructor.

    reply
    0
  • 迷茫

    迷茫2017-04-17 13:19:56

    I guess an implicit transformation may have occurred

    reply
    0
  • Cancelreply