c++ - Failing to return a unique_ptr -


hpp:

class camera { public:     camera(float fov, float nearplane, float farplane);      std::unique_ptr<glm::mat4x4> getprojectionmatrix();  private:     std::unique_ptr<glm::mat4x4> projectionmatrix; }; 

cpp:

camera::camera(float fov, float nearplane, float farplane) {      float aspectratio = displaymanager::displaywidth / displaymanager::displayheight;      projectionmatrix = std::make_unique<glm::mat4x4>();     *projectionmatrix = glm::perspective(fov, aspectratio, nearplane, farplane); }  std::unique_ptr<glm::mat4x4> camera::getprojectionmatrix() {     //std::unique_ptr<glm::mat4x4> projectionmatrix = std::make_unique<glm::mat4x4>();     //*projectionmatrix = glm::perspective(90.0f, 1.333f, 0.1f, 1000.0f);     return std::move(projectionmatrix); } 

look @ 2 commented lines. program compile whether commented out or not, if are, data corrupted.

how can write getter returns unique_ptr private member of class? how set unique_ptr in constructor?

here's better idea: stop needlessly allocating memory. have camera store glm::mat4x4 directly, not unique_ptr. c++ not java; don't have allocate new. code becomes simpler:

camera::camera(float fov, float nearplane, float farplane)     : projectionmatrix(glm::perspective(fov, (displaymanager::displaywidth / displaymanager::displayheight), nearplane, farplane)) { }  glm::mat4x4 &camera::getprojectionmatrix() { return projectionmatrix; } 

however, if absolutely have use unique_ptr in camera, should return reference, not smart pointer:

glm::mat4x4 &camera::getprojectionmatrix() { return *projectionmatrix; } 

Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -