Идиома пароля может помочь:
Сначала создайте структуру без конструктора publi c и сделайте ее другом одного из ваших классов.
class Key
{
private: // All private
friend class MyClass; // only MyClass can use it.
Key() {}
Key(const Key&) = delete;
Key& operator=(const Key&) = delete;
};
Теперь объявите ваша функция для защиты с этим аргументом:
void reset_password(const Key&, std::string);
std::string get_description(const Key&, int error_code);
Затем ваш класс может запросить соответствующий функтор:
class MyClass
{
public:
void doathing(
std::string param1, std::string param2, std::string param3,
std::function<void(const Key&, int)> func)
{
// ...
auto error_code = 42;
func({}, error_code);
}
};
И в main()
:
int main()
{
MyClass c;
std::string error_description;
c.doathing(
"param1", "param2", "param3",
[&error_description](const Key& key, int error_code){
error_description = get_description(key, error_code);
}
);
std::cout << error_description;
// get_description({}, 42); // error: 'Key::Key()' is private within this context
}
Демо