Where could you see the fallacy of this approach?
I will explain: in the api_method_add method, I add the User data to the "work_list" list and return a "pointer" to the added list item. BUT, since the User who called my api does not have access to the definition of the worrk_struct structure, I cannot return a direct pointer to the worrk_struct structure to him - so I return the addresses to the beginning of the structure placed in the size_t variable.
And when the user calls the "api_method_set" method, he passes a pointer to the beginning of the structure in which he wants to change the data, and I already bring this passed address to the beginning of the structure to the real structure "work_struct". What could be the problem with such an approach?
struct user_struct
{
float float_;
double double_;
};
class my_class
{
public:
size_t api_method_add(user_struct user_struct_)
{
work_list.emplace_back();
(work_list.back()).user_struct_ = user_struct_;
return (size_t)&work_list.back();
}
void api_method_set(size_t pointer)
{
std::cout << ((work_struct*)pointer)->user_struct_.double_ << std::endl;
}
private:
struct work_struct
{
user_struct user_struct_;
int int_;
std::string string_;
};
std::list<work_struct>work_list;
};
int main()
{
user_struct user_struct_;
user_struct_.double_ = 777;
my_class my_class_;
size_t pointer = my_class_.api_method_add(user_struct_);
my_class_.set(pointer);
}