CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 3 of 5 FirstFirst 12345 LastLast
Results 31 to 45 of 65
  1. #31
    Join Date
    Apr 1999
    Posts
    27,449

    Re: template for events

    Quote Originally Posted by Cambalinho View Post
    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.

  2. #32
    Join Date
    Apr 1999
    Posts
    27,449

    Re: template for events

    Quote Originally Posted by Cambalinho View Post
    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

  3. #33
    Join Date
    Apr 2009
    Posts
    1,355

    Re: template for events

    Quote Originally Posted by Paul McKenzie View Post
    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

  4. #34
    Join Date
    Apr 1999
    Posts
    27,449

    Re: template for events

    Quote Originally Posted by Cambalinho View Post
    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.

  5. #35
    Join Date
    Apr 2009
    Posts
    1,355

    Re: template for events

    Quote Originally Posted by Paul McKenzie View Post
    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:
    Code:
    foo(int x);
    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.

  6. #36
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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)

  7. #37
    Join Date
    Apr 2009
    Posts
    1,355

    Re: [RESOLVED] template for events

    [QUOTE=2kaud;2132801]
    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/

    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.

  8. #38
    Join Date
    Apr 1999
    Posts
    27,449

    Re: [RESOLVED] template for events

    Quote Originally Posted by Cambalinho View Post
    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

  9. #39
    Join Date
    Apr 1999
    Posts
    27,449

    Re: template for events

    Quote Originally Posted by Cambalinho View Post
    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.

  10. #40
    Join Date
    Apr 2009
    Posts
    1,355

    Re: [RESOLVED] template for events

    Quote Originally Posted by Paul McKenzie View Post
    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.

  11. #41
    Join Date
    Apr 1999
    Posts
    27,449

    Re: [RESOLVED] template for events

    Quote Originally Posted by Cambalinho View Post
    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

  12. #42
    Join Date
    Apr 2009
    Posts
    1,355

    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.

  13. #43
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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)

  14. #44
    Join Date
    Apr 2009
    Posts
    1,355

    Re: [RESOLVED] template for events

    Quote Originally Posted by 2kaud View Post
    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|"

  15. #45
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: [RESOLVED] template for events

    Quote Originally Posted by Cambalinho View Post
    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

Page 3 of 5 FirstFirst 12345 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured