Home > front end >  accessing a c unique pointer declared in hpp file when set in a cpp file through constructor
accessing a c unique pointer declared in hpp file when set in a cpp file through constructor

Time:12-23

I'm a c beginner and am a little confused about how I would set a private unique from a class constructor while still managing to access it from other public functions. Should I even be using a unique pointer to begin with or a shared pointer instead?

example: (from a project I was working on)

header.hpp

class PixelHandler {
private:
// don't know if this ia legal
 std::unique_ptr<Ppm> ppm;
 std::vector<std::vector<int>> coordList;
 std::unordered_map<std::string, std::string> generatePassList(std::unordered_map<int, int>);
 

public:
 PixelHandler(int sizex, int sizey);
 PixelHandler(std::string picture);
 std::unordered_map<std::string, std::string> retrievePasswordList();
 void setPasswordList(std::string key, std::string password);

};

source.cpp

PixelHandler::PixelHandler(std::string picture)
{
  
   ppm = Ppm(picture.substr(0, -4)   ".ppm");

}

CodePudding user response:

Maybe you should do:

ppm = std::make_unique<Ppm>(picture.substr(0, -4)   ".ppm");

Because you can't assign the object (of type Ppm) to a pointer. You need to pass it to the constructor of the std::unique_ptr so that it can create a unique pointer that is pointing to that object.

CodePudding user response:

Should I even be using a unique pointer to begin with or a shared pointer instead?

You can't be advised about that without knowing the use that you will give to the pointer. Will PixelHandler be the only class accessing ppm? Even if you access it from different points within PixelHandler, how will those other accesses be? For example, would it be enough for other accessors to receive a raw pointer to ppm (e.g. read-only operations over Ppm)?... The decision between using a unique_ptr or a shared_ptr has mainly to do with ownership over the object you point to.

I'm a c beginner and am a little confused about how I would set a private unique from a class constructor while still managing to access it from other public functions.

You can initialize the Ppm pointer at PixelHandler's constructor, then use it from the other PixelHandler's methods. The Ppm instance won't be destroyed until the PixelHandler instance is destroyed.

BTW, if you want to form a Ppm path from a picture path (e.g. a jpg, png and so on), you can use the facilities from the std::filesystem (path and replace_extension). I don't think that substr(0, -4) works in C as a way to remove an extension.

[Demo]

#include <filesystem>
#include <iostream>  // cout
#include <memory>  // make_unique, unique_ptr
#include <string>

class Ppm
{
    const std::string file_extension{"ppm"};
public:
    Ppm(std::filesystem::path path)
    : path_{path.replace_extension(file_extension)}
    {
        
        std::cout << "Ppm ctor: " << path_ << "\n";
    }
    ~Ppm() { std::cout << "Ppm dtor\n"; }
private:
    std::filesystem::path path_{};
};

class PixelHandler
{
public:
    PixelHandler(std::filesystem::path path)
    : ppm_up_{std::make_unique<Ppm>(std::move(path))}
    {}
    ~PixelHandler() { std::cout << "PixelHandler dtor\n"; }
private:
    std::unique_ptr<Ppm> ppm_up_{};
};

int main()
{
    PixelHandler ph("blah.jpg");
    std::cout << "\n... short life :(\n\n";
}
  • Related