-
October 17th, 2013, 12:43 PM
#31
Re: template for events
Originally Posted by Cambalinho
with 1 parameter. but i still confuse
how we can initializate the m_foo?
What feature of a constructor does all of your code that you post is missing? It is a feature that you should be using, but you never used it. In some ways, this feature is not a requirement, but it is a requirement if any member variables do not have default constructors.
Now given all of that, do you know what this "feature" is that
1) You seem to never use which you should start to use and
2) Is required to initialize members that cannot be default constructed (like the m_foo variable, and the events variable in your class.
?
That is your homework. Believe me, this is covered in beginner C++ books. The code I posted is beginner code, but from the looks of it, you can't fix it. I know it's humbling, but seriously, this indicates that you need to learn the fundamentals before embarking on this other code you're trying to write.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; October 17th, 2013 at 12:45 PM.
-
October 17th, 2013, 01:13 PM
#32
Re: template for events
Originally Posted by Cambalinho
with 1 parameter. but i still confuse
how we can initializate the m_foo?
How do you initialize any member variable when an object is constructed? Note I said initialize, and not assign. Hint -- the initialization occurs before the body of the constructor.
Regards,
Paul McKenzie
-
October 17th, 2013, 01:19 PM
#33
Re: template for events
Originally Posted by Paul McKenzie
How do you initialize any member variable when an object is constructed? Note I said initialize, and not assign. Hint -- the initialization occurs before the body of the constructor.
Regards,
Paul McKenzie
Code:
class foo
{
public:
foo(int x);
};
what i see in these class is that we have 1 construtor declaration(not the function, just the declaration) with an int parameter.
i understand, if you complete that construtor with another variable\function. but in these situçao i don't understand how we inicializate the foo
but like you said: imust read more
-
October 17th, 2013, 01:26 PM
#34
Re: template for events
Originally Posted by Cambalinho
Code:
class foo
{
public:
foo(int x);
};
what i see in these class is that we have 1 construtor declaration(not the function, just the declaration) with an int parameter.
i understand, if you complete that construtor with another variable\function. but in these situçao i don't understand how we inicializate the foo
but like you said: imust read more
The answer is obvious -- it can be found in any beginner C++ book, on any C++ website, and thousands of posts here.
What is the syntax required to initialize member variables when a constructor is called? It is that simple of a question. I gave you a hint. Do a google search about C++ constructors, initialization and , member variables.
In addition, here is a post directed to you that I made that explains it to you. However it seems you didn't take my advice:
http://forums.codeguru.com/showthrea...61#post2130661
Do you see it now?
Regards,
Paul McKenzie
Last edited by Paul McKenzie; October 17th, 2013 at 01:30 PM.
-
October 17th, 2013, 01:39 PM
#35
Re: template for events
Originally Posted by Paul McKenzie
The answer is obvious -- it can be found in any beginner C++ book, on any C++ website, and thousands of posts here.
What is the syntax required to initialize member variables when a constructor is called? It is that simple of a question. I gave you a hint. Do a google search about C++ constructors, initialization and , member variables.
In addition, here is a post directed to you that I made that explains it to you. However it seems you didn't take my advice:
http://forums.codeguru.com/showthrea...61#post2130661
Do you see it now?
Regards,
Paul McKenzie
Code:
foo(typename1 var1, typename2...var2);//using ':' after ')', we can inicializate the variables.
why you did:
instead :
Code:
foo(int x)
{
//do something
}
(or outside of class)
Code:
foo::foo(int x)
{
//do something
}
these is what confuse me... you only declare the construtor, you don't do it.
-
October 17th, 2013, 01:58 PM
#36
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/
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
October 17th, 2013, 02:04 PM
#37
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 ':'
Last edited by Cambalinho; October 17th, 2013 at 02:07 PM.
-
October 17th, 2013, 02:17 PM
#38
Re: [RESOLVED] template for events
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
-
October 17th, 2013, 02:20 PM
#39
Re: template for events
Originally Posted by Cambalinho
these is what confuse me... you only declare the construtor, you don't do it.
That is because I don't care about creating a program that links. All that needs to be done is to compile the program so that you can fix the compiler errors.
Also, what if the constructor body is in another file somewhere? That code is still supposed to compile, regardless of whether the function body is there or not. This seems to be another fundamental of C++ that seems to have alluded you.
Do you know the difference between compiling and linking? The compiler doesn't care if the function exists or not -- all the compiler cares about is that if you do call the function, its declaration is present (either as a prototype, or the entire function body is present).
For example:
Code:
void Junk();
int main()
{
Junk();
}
This code compiles with no errors. Does it link? No. The reason why it doesn't link is that it is the linker that cares about whether there really is a Junk() function that exists somewhere. The compiler doesn't care at all if Junk() really does exist.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; October 17th, 2013 at 02:25 PM.
-
October 17th, 2013, 02:24 PM
#40
Re: [RESOLVED] template for events
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
Last edited by Cambalinho; October 17th, 2013 at 02:28 PM.
-
October 17th, 2013, 02:27 PM
#41
Re: [RESOLVED] template for events
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
-
November 7th, 2013, 08:15 AM
#42
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;
}
?????
Last edited by Cambalinho; November 7th, 2013 at 08:18 AM.
-
November 7th, 2013, 08:21 AM
#43
Re: [RESOLVED] template for events
operator overloading, like member functions is per class not per instance.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
November 7th, 2013, 08:26 AM
#44
Re: [RESOLVED] template for events
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|"
-
November 7th, 2013, 09:45 AM
#45
Re: [RESOLVED] template for events
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
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|