PythonAddict
PythonAddict

Reputation: 57

C++, using a class that have no constructor

Assuming I have the following code:

class RecommenderSystemLoader
{

private:

public:
    RecommenderSystemLoader() = delete;

    /**
     * loads movies by the given format for movies with their feature's score
     * @param movies_file_path a path to the file of the movies
     * @return shared pointer to a RecommenderSystem which was created with those movies
     */
    static std::unique_ptr<RecommenderSystem> create_rs_from_movies_file(const std::string& movies_file_path) noexcept(false);
};


Assuming that RecommenderSystem is another class. I was wondering how can I obtain an instance of this class and use the static function create_rs_from_movies_file that lies in it, given the fact there is no constructor at all. I want to do it from the main.cpp file.

Upvotes: 1

Views: 277

Answers (2)

Zack
Zack

Reputation: 117

Not sure about what you are trying to do, it seems to me you are searching for a creational pattern, maybe factory method design pattern.

However, to directly address your question, if you want to call create_rs_from_movies_file you don't need to have an instance of RecommenderSystemLoader because the method is static.

From a main.cpp you can write:

auto rec_sys = RecommenderSystemLoader::create_rs_from_movies_file(path)

Upvotes: 3

user12002570
user12002570

Reputation: 1

how can I obtain an instance of this class

For your given example you can use zero initialization to create an instance of RecommenderSystemLoader as shown below:

//create an instance 
RecommenderSystemLoader r{};

Working demo


how can i use the static function create_rs_from_movies_file that lies in it

The static member function create_rs_from_movies_file can be called without any object so you don't need to create an instance to use create_rs_from_movies_file.

Upvotes: -1

Related Questions