Re: [RESOLVED] template for events
[quote]
Code:
foo(typename1 var1, typename2...var2);//using ':' after ')', we can inicializate the variables.
this is how you do it.
Have a look at this and the following few pages
http://www.learncpp.com/cpp-tutorial...ization-lists/
Re: [RESOLVED] template for events
[QUOTE=2kaud;2132801]
yes.. now i ask, again(sorry), why these?
Code:
class foo
{
public:
foo(int x);
};
these continues confuse me? there ins't statements inside of construtor or ':'
Re: [RESOLVED] template for events
Quote:
Originally Posted by
Cambalinho
yes.. now i ask, again(sorry), why these?
Code:
class foo
{
public:
foo(int x);
};
these continues confuse me? there ins't statements inside of construtor or ':'
The code is there for you to compile and to fix the errors. An empty constructor is not an error -- it is perfectly legal.
Regards,
Paul McKenzie
Re: [RESOLVED] template for events
Quote:
Originally Posted by
Paul McKenzie
The code is there for you to compile and to fix the errors. An empty constructor is not an error -- it is perfectly legal.
Regards,
Paul McKenzie
(compile the resource files before link them.)
compiling: convert the source code to object file;
linking: combine the compiled resources files, object files,i think DLL's too, libraries for make the exe.
understood... sorry for something, and thanks for all. your tip was great for i think. thanks ;)
anotherthing: i'm realy sorry, but i can't rate you... sorry about that
Re: [RESOLVED] template for events
Quote:
Originally Posted by
Cambalinho
understood... sorry for something, and thanks for all. your tip was great for i think. thanks ;)
anotherthing: i'm realy sorry, but i can't rate you... sorry about that
ok. So go back to your original code, and attempt to fix it using the initialization list.
Regards,
Paul McKenzie
Re: [RESOLVED] template for events
see my events.h:
Code:
#ifndef events_H_INCLUDED
#define events_H_INCLUDED
#include <functional>
#include <vector>
template <class ... b>
class events
{
public:
typedef std::function<void(b...argx )> OnSomethingHandler;
events(OnSomethingHandler Handler)
{
handlers_=Handler;
}
void operator ()(b... args)
{
handlers_(args...);
}
events& operator = (OnSomethingHandler Handler)
{
handlers_ = Handler;
return *this;
}
private:
OnSomethingHandler handlers_;
};
#endif // events_H_INCLUDED
the main.cpp:
Code:
#include <iostream>
#include <string>
#include "events.h"
using namespace std;
class test
{
public:
events<> Printed{[]() { ; }};
void write(string a)
{
cout << a;
Printed();
}
};
test a;
int main()
{
a.Printed=[]()
{
cout << "\nMessage printed\n";
};
a.write("hello world");
cin.get();
return 0;
}
can i change the events template for i do these:
Code:
#include <iostream>
#include <string>
#include "events.h"
using namespace std;
class test
{
public:
events<> Printed{[]() { ; }};
void write(string a)
{
cout << a;
Printed();
}
};
test a;
a.Printed=[]()
{
cout << "\nMessage printed\n";
};
int main()
{
a.write("hello world");
cin.get();
return 0;
}
?????
Re: [RESOLVED] template for events
operator overloading, like member functions is per class not per instance.
Re: [RESOLVED] template for events
Quote:
Originally Posted by
2kaud
operator overloading, like member functions is per class not per instance.
my problem with these code is that i can't change the Printed() outside of main. like you see in code. if i use the 'void', i get these error:
"C:\Users\Joaquim\Documents\CodeBlocks\My Class\main.cpp|21|error: expected initializer before '.' token|"
Re: [RESOLVED] template for events
Quote:
Originally Posted by
Cambalinho
my problem with these code is that i can't change the Printed() outside of main. like you see in code. if i use the 'void', i get these error:
"C:\Users\Joaquim\Documents\CodeBlocks\My Class\main.cpp|21|error: expected initializer before '.' token|"
The solution is to stop trying to run code outside of main. Run it inside main or a function (indirectly) called from main.
Global variables are evil for a reason. See http://www.parashift.com/c++-faq/global-vars.html