In the Elem
constructor, you could have an argument to the callback (using e.g. std::function
or similar), and have the Elem
class store it. Then when the callback needs to be called, just call it.
You could also create a "setter" function in Elem
to set the callback.
Full example
#include <iostream>
#include <string>
#include <functional>
#include <vector>
class Elem
{
private:
int value_;
std::function<void(const std::string&)> callback_;
public:
Elem(int v, std::function<void(const std::string&)> cb)
: value_{v}, callback_{cb}
{}
void some_function() const
{
callback_("Elem with value " + std::to_string(value_));
}
};
class Cont
{
private:
std::vector<Elem> elements_;
void private_function(const std::string& s)
{
std::cout << "In private function: " << s << '\n';
}
public:
void add_value(const int v)
{
elements_.push_back(Elem(v, std::bind(&Cont::private_function, this, std::placeholders::_1)));
}
std::vector<Elem>::const_iterator begin() const
{
return std::begin(elements_);
}
std::vector<Elem>::const_iterator end() const
{
return std::end(elements_);
}
};
int main()
{
Cont c;
// Add some elements
for (int i = 1; i <= 10; ++i)
c.add_value(i);
// Call callback function
for (const auto& e : c)
e.some_function();
}
Output:
In private function: Elem with value 1
In private function: Elem with value 2
In private function: Elem with value 3
In private function: Elem with value 4
In private function: Elem with value 5
In private function: Elem with value 6
In private function: Elem with value 7
In private function: Elem with value 8
In private function: Elem with value 9
In private function: Elem with value 10
See it in action.
In the code above, the private function Cont::private_function
is totally unknown to the Elem
class, and can't be called directly. However, we can pass it to the Elem
class using arguments, and then call it from inside the Elem
class without exposing the private function at all.