I have a superclass Entry and subclasses MusicAlbum, Book and Film. Instances of these subclasses are stored according to the name of the item ie Book1. The name and type of all these instances are stored in a vector cat_vector which is a vector of objects of class libCatalogue which simply stores the name and type:
class libCatalogue{
std::string name;
std::string type;
public:
libCatalogue(std::string name, std::string type);
std::string getname();
std::string gettype();
};
libCatalogue::libCatalogue(std::string name, std::string type) :name(name), type(type) {};
std::vector <libCatalogue> cat_vector;
Entries in the vector are made in the constructor eg.
MusicAlbum::MusicAlbum(std::string a, std::string b, std::string borrower)
: name(a), artist(b), Entry(borrower){
cat_vector.push_back(libCatalogue(name, "MusicAlbum"));
Each subclass has a member function called printdetails(). I want to use a loop to step through each entry in cat_vector and print the details of the entry but the following does not work:
int no = 1;
for (auto it = begin(cat_vector); it != end(cat_vector); ++it)
{
std::string name_ = it->getname();
std::string type_ = it->gettype();
std::cout << "Entry no. " << no << std::endl;
std::cout << "Name: " << name_ << std::endl;
std::cout << "Type: " << type_ << std::endl << std::endl;
if (type_ == "MusicAlbum"){
name_.printdetails(); //print using MusicAlbum member function
}
//etc...
no++;
I know it is because name_ is a string and not an object of any of the classes I want to call, but I haven't been able to find any way to convert it so far. Is there any way to tell the compiler that name_ is referring to an object of one of the subclasses?
C++ is a statically typed compiled language.You cannot create variables on fly. Fortunately, for cases like these, the work around is to use a lookup table. Generally this is achieved through a map where the key would be the string and the value would be the function you would want to associate and call for the particular string.
I know it is because name_ is a string and not an object of any of the
classes I want to call, but I haven't been able to find any way to
convert it so far. Is there any way to tell the compiler that name_ is
referring to an object of one of the subclasses?
when you qualify a member, the member name is qualified with respect to the type of the variable not with respect to the content. So the call name_.printdetails() would mean you are trying to invoke the member function printdetails for the instance of type std::string but std::string does not have a member function named printdetails.
A simple example to extend the above idea
struct Spam
{
enum { NO_OF_FUNCTIONS = 4 };
Spam()
{
lookup_callback["Foo1"] = std::bind(&Spam::foo1, this);
lookup_callback["Foo2"] = std::bind(&Spam::foo2, this);
lookup_callback["Foo3"] = std::bind(&Spam::foo3, this);
lookup_callback["Foo4"] = std::bind(&Spam::foo4, this);
}
void foo1() { std::cout << "Foo1" << std::endl; }
void foo2() { std::cout << "Foo2" << std::endl; }
void foo3() { std::cout << "Foo3" << std::endl; }
void foo4() { std::cout << "Foo4" << std::endl; }
void call(std::string name)
{
if (lookup_callback.count(name) > 0)
{
lookup_callback[name]();
}
else
{
std::cerr << "Invalid Function Call" << std::endl;
}
}
std::map<std::string, std::function<void(void)>> lookup_callback;
};
// Driver program to test above functions
int main()
{
std::string name;
Spam spam;
for (std::cin >> name; name != "quit"; std::cin >> name)
{
spam.call(name);
}
}
If you pass an instance of Entry around than you won't have a problem because you will be able to call:
it->entry->print_details();
If you don't want LibCatalogue to be aware of instances of Entry you can create a new class called Printable or something similar. This class will be held by Entry and by LibCatalogue. The `Printable class will have all the details required to print. That way you could call both:
it->printable->print_details();
entry->printable->print_details();
To augment #abhijit's answer, I often use static tables if the content is small and the usage is few.
// Typedef for a the function pointer
typedef void (*Function_Pointer)(void);
struct key_function_entry
{
const char * key_text;
Function_Pointer function;
};
void Process_Foo1_Request(void);
void Process_Bach_Request(void);
void Process_Eat_Request(void);
static const key_function_entry delegation_table[] =
{
{"foo1", Process_Foo1_Request},
{"Bah", Process_Bah_Request},
{"eat", Process_Eat_Request},
};
static const unsigned int delegation_entries =
sizeof(delegation_table) / sizeof(delegation_table[0]);
void Process_Request(const std::string& request)
{
for (unsigned int i = 0U; i < delegation_entries; ++i)
{
if (request == delegation_table[i].key_text)
{
delegation_table[i].function(); // Execute the associated function.
break;
}
}
}
An advantage here is that the table is static (one instance) and constant, so it can be placed into read-only memory. The table doesn't need to be built during runtime (like a std::map). The code references a table created during compilation phase. (It's an embedded systems thing, saving memory or placing stuff into read-only memory.)
For small number of entries, a linear search may be faster than a std::map.
For larger entries or very large number of accesses, an std::map may be preferred.
Related
I have a factory that creates class instances from strings. KeyValueType is an abstract class, which will be passed to Map/Reduce functions.
class KeyValueType {
public:
virtual void parse(const std::string &) = 0;
virtual std::string to_string() const = 0;
};
Factories in code are getting from the shared library (to be able to config map/reduce functions from a remote computer).
std::unique_ptr<KeyValueType> KeyValueTypeFactory::create() override {
return std::make_unique<KeyValueType<T>>();
};
std::unique_ptr<KeyValueType> KeyValueTypeFactory::create(const std::string &str) override {
std::unique_ptr<KeyValueType> ptr = this->create();
ptr->parse(str);
return ptr;
};
So, I have the next code, where I'm creating two objects key/value and returning them, as a pair of unique_ptr
std::pair<std::unique_ptr<KeyValueType>, std::unique_ptr<KeyValueType>>
get_key_value_from_json(const std::string &data, std::unique_ptr<KeyValueTypeFactory> &key_factory, std::unique_ptr<KeyValueTypeFactory> &value_factory) {
boost::property_tree::ptree pt{};
boost::property_tree::json_parser::read_json(dynamic_cast<std::stringstream &>(std::stringstream{} << data), pt);
return { std::move(key_factory->create(pt.get("key", ""))),
std::move(value_factory->create(pt.get("value", ""))) };
}
std::pair<std::unique_ptr<KeyValueType>, std::unique_ptr<KeyValueType>> blocking_get_result() {
... // Get json and config
auto[key, value] = get_key_value_from_json(json, cfg->key_out_factory, cfg->value_res_factory);
std::cout << "The result of Map/Reduce is " << value->to_string() << std::endl;
return { std::move(key), std::move(value) };
}
int main() {
auto[key, value] = blocking_get_result();
std::cout << (value.get() == nullptr) << std::endl;
std::cout << "The result of Map/Reduce is " << value->to_string() << std::endl;
return 0;
}
The actual problem is, that in blocking_get_result() function key and value are valid and virtual function to_string() is working correctly, but after returning pair from function to main unique_ptr is not null, but to_string throws Segmentation Fault. Also, dynamic_cast to a derived class is causing Segfault.
The actual problem was, that getting config fulfilled by using dlopen and dlsym and wrapping result into shared_ptr. So shared library was freed in blocking_get_result. So in the main pointer in the vtable became invalid.
I was writing some exercise code to understand class inheritance and I couldn't figure out If there is a way to do what I tried to explain in the title.
So what I want to do to have Family and Member (family members) class. A family can have multiple members but members can have only one family. But let's say one of the members got married. So they are changing their family from A to other family B. Is there a way to do that?
#include <iostream>
#include <string>
class Family {
std::string _familyName;
public:
Family();
void setFamilyName(std::string& str) { _familyName = str;}
void printFamilyName() {
std::cout << "Family name is: " << _familyName << std::endl;
}
}
class Member : public Family {
std::string _firstName;
public:
Member();
void setFirstName(std::string& str) { _firstName = str;}
void changeFamily(Family *f) {} // What do i do here ?
}
int main(){
Member m;
m.setFamilyName("Smith");
m.setFirstName("John");
m.printFamilyName();
Family f;
B.setFamilyName("Williams");
m.changeFamily(&f);
m.printFamilyName();
return 0;
}
From this, I would like to get an output as
Family name is: Smith
Family name is: Williams
Thank you.
(An extra question is, is there a way to create a member variable without constructing the family part, but to bind the new member varible to an existing family variable while declaring the member ?)
A family can have multiple members but members can have only one family. But let's say one of the members got married. So they are changing their family from A to other family B. Is there a way to do that?
You need the relationship defined between objects, not the object types. You can do that by using a container-contained relationship between objects, not a base class - derived class relationship between the classes.
struct Member;
struct Family
{
std::vector<Member*> members;
}
struct Member
{
Family* family;
}
I'll let you figure out how define the member functions of the classes so the relationships between the objects and the lifetimes of the objects are maintained in a robust manner.
I don't think you need inheritance, and what you perceive to be inheritance is not correct. When working with multiple classes that work together either it be one class containing another or parent child relationship, there is always one question to ask yourself: The relationship between the two classes is it a "Is a relationship" or is it "Has a relationship". When you ask yourself that question and it is very easy to answer then you should know what design type your classes will need.
For example: A dog is a mammal and A dog has paws. So if we have 3 classes here. The structure would look something like this:
class Mammal {
/* code */
};
class Dog : public Mammal {
/* code */
};
Here we have inheritance because a Dog IS A Mammal and will inherit all of the traits that all Mammals have!
Next we would have this:
class Paws {
/* code */
};
So let's go back to our "dog" class
class Dog : public Mammal {
private:
Paws paws; // this is a member of Dog because a Dog HAS PAWS!
};
I hope this clears things up with the basic structure of classes!
Now to look back at your initial problem, here is what I have done. I made a basic struct to represent a FamilyMember where they all have the same information about each other in common. I abstracted out the last name completely to remove any dependencies and having to check if the last name is the same and to update or replace it etc. My code looks like this:
struct FamilyMember {
std::string firstName_;
unsigned int age_;
char gender_;
FamilyMember() = default;
FamilyMember(const std::string& firstName, unsigned int age, char gender) :
firstName_(firstName),
age_(age),
gender_(gender)
{}
};
class Family {
private:
std::string familyName_;
std::vector<FamilyMember> members_;
public:
Family() = default;
explicit Family(const std::string& familyName) : familyName_( familyName ) {
}
void addMember(FamilyMember& member) {
members_.push_back(member);
}
FamilyMember& getMember(unsigned int idx) {
return members_.at(idx);
}
std::vector<FamilyMember>& getFamily() {
return members_;
}
const std::string& getFamilyName() const { return familyName_; }
};
int main() {
Family Smith("Smith");
FamilyMember John("John", 32, 'M');
FamilyMember Sarah("Sarah", 29, 'F');
FamilyMember Amanda("Amanda", 19, 'F');
Smith.addMember(John);
Smith.addMember(Sarah);
Smith.addMember(Amanda);
std::cout << "Meet the " << Smith.getFamilyName() << "s:\n";
for (auto& m : Smith.getFamily()) {
std::cout << m.firstName_ << " " << Smith.getFamilyName() << " " << m.age_ << " " << m.gender_ << '\n';
}
Family Jones("Jones");
FamilyMember Tom("Tom", 44, 'M');
FamilyMember Mary("Mary", 43, 'F');
FamilyMember Mike("Mike", 21, 'M');
Jones.addMember(Tom);
Jones.addMember(Mary);
Jones.addMember(Mike);
std::cout << "Meet the " << Jones.getFamilyName() << "s:\n";
for (auto& m : Jones.getFamily() ) {
std::cout << m.firstName_ << " " << Jones.getFamilyName() << " " << m.age_ << " " << m.gender_ << '\n';
}
std::cout << "We present to you today the Union between: "
<< Jones.getMember(2).firstName_ << " " << Jones.getFamilyName() << " and "
<< Smith.getMember(2).firstName_ << " " << Smith.getFamilyName() << '\n';
Jones.addMember(Amanda);
std::cout << "We now have Mr. " << Jones.getMember(2).firstName_ << " " << Jones.getFamilyName() << " and "
<< "Mrs. " << Jones.getMember(3).firstName_ << " " << Smith.getFamilyName() << " " << Jones.getFamilyName() << '\n';
return 0;
}
So as you can see from the small program above; we do not have the dependency of having to modify the last name at all. With this kind of structure, when "Amanda" marries "Mike". She still belongs to the "Smith" family, but she has also joined with the "Jones" family. Now if you try to access her individual data through the structure, there is no information about her last name. You can retrieve her first name, age and gender. If you want the information for the last name, you have to retrieve that from the Family class itself as it never changes!
Short answer: No. Inheritance is set at compile time, you can't change it at runtime.
But I don't think inheritance is what you're looking for. Member inherits all the members and functions of Family. It doesn't imply a relationship between that Member and any particular instance of Family. This is reflected in your design - _familyName is a string, not a reference to an object or something. But you can have references to other objects. For example, like this:
#include <iostream>
#include <string>
#include <vector>
class Family {
std::string _familyName;
public:
Family(std::string name) : _familyName(name) {};
void printFamilyName() {
std::cout << "Family name is: " << _familyName << std::endl;
}
}
class Member {
std::string _firstName;
std::vector<Family*> families;
public:
Member(Family *familyPtr) { addFamily(familyPtr) };
void setFirstName(std::string& str) { _firstName = str;}
void addFamily(Family *familyPtr) { families.push_back(familyPtr) };
void printFamilies() { for (auto &ptr : families) { ptr->printFamilyName() } };
}
int main()
{
Family Jackson("Jackson");
Family Williams("Williams");
Member John(&Jackson);
Member Alice(&Williams);
John.printFamilies();
John.addFamily(&Williams);
John.printFamilies();
return 0;
}
Member doesn't need any of the functions or values of class Family, so we don't inherit them. Instead we maintain a list of pointers to Family objects that this Member "belongs" to, thus implying a relationship between this Member and those instances of Family. When we want to print all the Family objects that Member owns, we just loop through the vector of pointers and call printFamilyName() on each of them.
Within JavaScript, you can pull off something like this:
function bunny() { alert("The bunny jumped."); }
var oldBunny = bunny;
function bunny() {
oldBunny();
alert("The bunny also ran.");
}
bunny(); // The bunny Jumped. The bunny also ran.
As one can see, the old "bunny" function had code appended to it by copying to a variable, then recreating the function with the same name. The copy of the original function runs, and the new code also runs.
I wish to replicate a similar mechanic in C++.
Now before you have a meltdown and start explaining the differences between static and dynamic languages, I get it. I'm not looking for something identical to what's provided, but I do desire something similar.
Furthermore, I'm not trying to do this to modify existing code; I wish to format my own source code to allow such a mechanic for other users to take advantage of.
One of the first ideas I had was to perhaps setup various macros within the code that could later be modified by other files.
Another idea would be to create a Signal and Slots system like in QT. Though I have no clue how to do such a thing myself.
Thank you for reading; I hope you have some suggestions.
Well, if you recognize which feature of JavaScript functions makes this possible, it's not too hard to do the same in C++. In JavaScript functions also have closures, which regular function in C++ don't have. But C++ lambdas are of a closure type. And if one defines bunny to be something which can both hold an object of a closure type, and be reassigned, you're all set.
The C++ standard library offers a nice default choice for this, in the form of std::function. We can just re-write your original JavaScript as follows:
std::function<void()> bunny = [] {
std::cout << "The bunny jumped.\n";
};
auto oldBunny = std::move(bunny);
bunny = [oldBunny] {
oldBunny();
std::cout << "The bunny also ran.\n";
};
bunny();
You can use functors.
#include <iostream>
#include <string>
class Base
{
public:
virtual std::string operator ()()
{
return "Base call";
}
virtual ~Base() {}
};
class Derived : public Base
{
public:
virtual std::string operator()()
{
return "Wrapper: " + Base::operator()();
}
};
int main()
{
Base* pFun = new Base;
std::cout << "Now check Base: " << (*pFun)() << std::endl;
delete pFun;
pFun = new Derived;
std::cout << "Now check Derived: " << (*pFun)() << std::endl;
return 0;
}
Assuming the goal is to allow the calling code to extend the program's functionality beyond what the initial code provided, I might use a user-updatable array of functor-objects, something like this:
#include <iostream>
#include <memory>
class Function
{
public:
virtual void Call() = 0;
};
typedef std::shared_ptr<Function> FunctionSharedPointer;
class OldBunny : public Function
{
public:
virtual void Call()
{
std::cout << "The bunny jumped." << std::endl;
}
};
class NewBunny : public Function
{
public:
NewBunny(FunctionSharedPointer oldFunction) : _oldFunction(oldFunction) {/* empty */}
virtual void Call()
{
_oldFunction->Call();
std::cout << "The bunny also ran." << std::endl;
}
private:
FunctionSharedPointer _oldFunction;
};
enum {
FUNCTION_BUNNY,
// other functions could be declared here later...
NUM_FUNCTIONS
};
// Our table of functions that the user can Call() if he wants to
static FunctionSharedPointer _functionTable[NUM_FUNCTIONS];
// Wrapper function, just to keep users from accessing our table directly,
// in case we ever want to change it to something else
void CallFunction(int whichFunction)
{
_functionTable[whichFunction]->Call();
}
// Another wrapper function
void SetFunction(int whichFunction, FunctionSharedPointer newFunctionDefinition)
{
_functionTable[whichFunction] = newFunctionDefinition;
}
// And another
FunctionSharedPointer GetFunction(int whichFunction)
{
return _functionTable[whichFunction];
}
int main(int argc, char ** argv)
{
// Our default function values get set here
SetFunction(FUNCTION_BUNNY, std::make_shared<OldBunny>());
std::cout << "before:" << std::endl;
CallFunction(FUNCTION_BUNNY);
// Now let's update an entry in our function table to do something different!
FunctionSharedPointer op = GetFunction(FUNCTION_BUNNY);
FunctionSharedPointer np = std::make_shared<NewBunny>(op);
SetFunction(FUNCTION_BUNNY, np);
std::cout << "after:" << std::endl;
CallFunction(FUNCTION_BUNNY);
return 0;
}
void bunny()
{
cout << "The bunny jumped." << endl;
}
void oldBunny()
{
bunny();
}
void newBunny()
{
bunny();
cout << "The bunny also ran." << endl;
}
#define bunny newBunny
int main()
{
bunny();
return 0;
}
If you don't need oldBunny(), just remove it.
Consider the following code:
#include <iostream>
#include <map>
class Value
{
public:
void set(const int intValue){ intValue_ = intValue; }
int read() const { return intValue_; }
void replaceIfInMap(){
std::map<int,int>::iterator it;
it = valuesToReplace_->find(intValue_);
if(it != valuesToReplace_->end()){
intValue_ = it->second;
}
}
Value(std::map<int,int>* valuesToReplace) : valuesToReplace_(valuesToReplace){}
private:
std::map<int,int>* valuesToReplace_;
int intValue_;
};
class Holder {
public:
void doStuffWithValues(){
Value a(&valuesToReplace_), b(&valuesToReplace_), c(&valuesToReplace_);
a.set(1); b.set(2); c.set(3);
valuesToReplace[2]=5;
a.replaceIfInMap(); b.replaceIfInMap(); c.replaceIfInMap();
std::cout << "a: " << a.read()
<< " b: " << b.read()
<< " c: " << c.read() << std::endl;
}
private:
std::map<int,int> valuesToReplace_;
};
int main()
{
Holder holder;
holder.doStuffWithValues();
}
How could I get access to the valuesToReplace_ member in a more convenient (and preferably more elegant) way? I have considered storing the map as a public static member of the class Value, but that would deny the possibility of having multiple instances of the Holder class, as each Holder instance requires a set of Value instances with different replacement settings.
A global map would be an even uglier "solution"...
Calling Value::read() from Holder and doing the map interaction there is not an option as this code is only a simplification and in the real code the equivalent of each instance of Value can store pointers to other instances of the same class rendering the aforementioned method overly complex and bulky.
Why does the above code even work? Holder::valuesToReplace_ is private! Is this just normal C++ behaviour (as you cannot get that pointer without access to the private members of the class anyway)?
Why does the above code even work? Holder::valuesToReplace_ is
private!
It is private, so Holder::doStuffWithValues can access it because it is a member function, nothing wrong there.
Value a(&valuesToReplace_), b(&valuesToReplace_), c(&valuesToReplace_);
a.set(1); b.set(2); c.set(3);
Here, all your Value objects have valuesToReplace_ pointing to the same map is that what you want? It seems strange, I would either have a static map (which would make a copy on assignment) or a smart pointer to prevent unexpected deletion (but allow NULL values).
How could I get access to the valuesToReplace_ member in a more
convenient (and preferably more elegant) way?
You could keep it private and have public member functions which return begin/end const_iterators for the map, or setIntForInt/getIntForInt accessor methods which are not dependent on internal implementation.
How about passing a reference to the valuesToReplace map to your replaceIfInMap method?
class Value
{
public:
void set(const int intValue){ intValue_ = intValue; }
int read() const { return intValue_; }
void replaceIfInMap(std::map<int,int> const& valuesToReplace_){
std::map<int,int>::const_iterator it;
it = valuesToReplace_->find(intValue_);
if(it != valuesToReplace_->end()){
intValue_ = it->second;
}
}
Value() {}
private:
int intValue_;
};
class Holder {
public:
void doStuffWithValues(){
Value a, b, c;
a.set(1); b.set(2); c.set(3);
valuesToReplace_[2]=5;
a.replaceIfInMap(valuesToReplace_);
b.replaceIfInMap(valuesToReplace_);
c.replaceIfInMap(valuesToReplace_);
std::cout << "a: " << a.read()
<< " b: " << b.read()
<< " c: " << c.read() << std::endl;
}
private:
std::map<int,int> valuesToReplace_;
};
I've messed up something.
Here is the code:
#include <iostream>
class connection_c {
private:
std::string data_;
void (*saveCallBack_)();
public:
connection_c(std::string &data) : data_(data) { std::cout << "ctor: " << __FUNCTION__ << ":" << data_ << std::endl;}
void registerCallBack(void(*cb)()) { saveCallBack_ = cb; }
};
class inst_c {
private:
static int id;
connection_c conn;
static void cb() { std::cout << __FUNCTION__ << " id = " << id << std::endl; }
public:
inst_c(connection_c &c, int a) : conn(c), id(a) {
std::cout << "ctor: " << __FUNCTION__ << " " << id << std::endl;
conn.registerCallBack(&cb);
}
};
class group_inst_c {
private:
connection_c conn;
inst_c i,j,k;
public:
group_inst_c(std::string data) : conn(data), i(conn,1), j(conn,2), k(conn,3) {}
};
int main() {
group_inst_c gi("asdf");
return 0;
}
What I want to achieve ;)
create a group of instances (group_inst_c)
it should initialize single connection for the group (connection_c)
each instance (inst_c) should use this connection (it will be serialized)
.. in addition each instance should register separate callback
For sure I've messed up with cloning, but I guess probably not only.
Can someone help me solve this puzzle? thx.
Your code creates a copy of your connection object for each instance. The original connection object is then only accessible by your group_inst_c. Is this what you want? If not, you need to change:
class inst_c {
private:
static int id;
connection_c& conn; // <-- Needs to be a reference.
in addition each instance should register separate callback
I'm not sure what you mean here. Are the callbacks supposed to be member functions? Then you need to use a "pointer to member function" (the ::*, .*, and ->* operators). If the callbacks are supposed to be regular functions, you should be okay with your current code. You'll just need to add this to class connection_c:
void doCallback(void) { (*saveCallBack_)(); }
If I understood that correctly (you want to call several callbacks from a single connection [object]), you need a list in connection_c to register the callbacks (just like delegates in C# if you know them).
If an event occurs to this connection, it has to know where to report. So you have to iterate through the callbacks somehow (call them one by one; you cannot call them all at once). The easiest, straightforward way is to use an STL list or maybe boost offers something appropriate.
Take a look at this: A C++ delegate class. In the main function, there's a vector defined that takes multiple callbacks. You could use this pattern in you connection_c class to add and not set a callback.
Try to keep it simple at first. There's always an opportunity to grow/improve the design later on. Below is some example code and here are a couple of things I was thinking about while building it:
1) As mentioned, keep it simple. For example, maybe the group concept can be a vector (i.e. inst_group_t) to start. You can always grow the design later as you learn more about it.
2) Try to reduce class dependencies. For example, maybe I do not need to have the connection as a member variable. I can pass it in when its needed (i.e. execute()). Maybe the callback doesn't need to be registered (i.e. execute()), since its 1 connection_c to many inst_c instances registering a callback for each inst_c would mean connection would have some container. Keep it simple :)
3) Try to use const and reference as much as possible (i.e. connection_c constructor). Less copy constructors/temp objects will be created.
#include <iostream>
class connection_c {
private:
std::string data_;
public:
connection_c(const std::string &data) : data_(data) {
std::cout << "ctor: " << __FUNCTION__ << ":" << data_ << std::endl;
}
};
class inst_c {
private:
int id;
public:
inst_c(int a) : id(a) {
std::cout << "ctor: " << __FUNCTION__ << " " << id << std::endl;
}
typedef void (*execute_callback_t)(int i);
void execute(connection_c& connection, execute_callback_t callback) {
callback(id);
}
};
void mycallback(int id) {
std::cout << "Instance number " << id << " executed" << std::endl;
}
int main() {
typedef std::vector<inst_c*> inst_group_t;
inst_group_t group;
std::string data;
connection_c connection(data);
for (int i = 0; i < 10; ++i)
group.push_back(new inst_c(i) );
for (int i = 0; i < 10; ++i)
group[i]->execute(connection, mycallback);
for (int i = 0; i < 10; ++i)
delete group[i];
return 0;
}