CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 4 of 5 FirstFirst 12345 LastLast
Results 46 to 60 of 65
  1. #46
    Join Date
    Apr 2009
    Posts
    1,355

    Re: [RESOLVED] template for events

    Quote Originally Posted by D_Drmmr View Post
    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
    what you can tell me about static class members?
    but i only see examples with class's names and not with objects\instances names... but what you can tell me?

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

    Re: [RESOLVED] template for events

    Quote Originally Posted by Cambalinho View Post
    what you can tell me about static class members?
    but i only see examples with class's names and not with objects\instances names... but what you can tell me?
    The only useful thing I can tell you, is that there probably is a good design for what you are trying to do in C++. But as long as you refuse to share what it is you are trying to do and are merely stuck on the solution that you would have chosen in another programming language, I can't help you any further.
    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

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

    Re: [RESOLVED] template for events

    Quote Originally Posted by D_Drmmr View Post
    The only useful thing I can tell you, is that there probably is a good design for what you are trying to do in C++. But as long as you refuse to share what it is you are trying to do and are merely stuck on the solution that you would have chosen in another programming language, I can't help you any further.
    ok..thanks for all
    at least doing that template was great, wasn't?

  4. #49
    Join Date
    Apr 2009
    Posts
    1,355

    Re: [RESOLVED] template for events

    see the actual template:
    Code:
    template <class ... b>
    class events
    {
    public:
        typedef std::function<void(b...argx )> OnSomethingHandler;
    
        events(OnSomethingHandler Handler)
        {
            handlers_=Handler;
        }
    
        void operator() (b&&... args)
        {
            handlers_ (std::forward<b> (args)...);
        }
    
        events& operator = (OnSomethingHandler Handler)
        {
            handlers_ = Handler;
            return *this;
        }
    
    private:
        OnSomethingHandler handlers_;
    
    };
    i'm trying convert it for a class, but i'm gettings errors because of parameters:
    Code:
    class events2
    {
    public:
        typedef std::function<void(...)> OnSomethingHandler;
    
        events2(OnSomethingHandler Handler)
        {
            handlers_=Handler;
        }
    
        void operator() (...)
        {
            handlers_ (...);
        }
    
        events2& operator = (OnSomethingHandler Handler)
        {
            handlers_ = Handler;
            return *this;
        }
    
    private:
        OnSomethingHandler handlers_;
    
    };
    error:
    "C:\Users\Joaquim\Documents\CodeBlocks\My Class\events.h|56|error: field 'handlers_' has incomplete type|"
    can anyone advice me?

  5. #50
    Join Date
    Apr 1999
    Posts
    27,449

    Re: [RESOLVED] template for events

    Quote Originally Posted by Cambalinho View Post
    ok..thanks for all
    at least doing that template was great, wasn't?
    Just because you attempted to use templates doesn't mean it is a good idea or good design.

    Regards,

    Paul McKenzie

  6. #51
    Join Date
    Apr 2009
    Posts
    1,355

    Re: [RESOLVED] template for events

    yah... that's why i'm trying convert it to a class

  7. #52
    Join Date
    Apr 1999
    Posts
    27,449

    Re: [RESOLVED] template for events

    Quote Originally Posted by Cambalinho View Post
    yah... that's why i'm trying convert it to a class
    How does that help? When I talk about design, I just don't mean syntax.

    What is it that you're trying to accomplish? Don't talk about templates or classes -- tell us what you're trying to do on a high level.

    Regards,

    Paul McKenzie

  8. #53
    Join Date
    Apr 2009
    Posts
    1,355

    Re: [RESOLVED] template for events

    Quote Originally Posted by Paul McKenzie View Post
    How does that help? When I talk about design, I just don't mean syntax.

    What is it that you're trying to accomplish? Don't talk about templates or classes -- tell us what you're trying to do on a high level.

    Regards,

    Paul McKenzie
    declare events: event created(); or event position(int x, int y);

    and then change them:
    Code:
    void created()
    {
        cout << "hello world";
    }
    
    void position(int x, int y)
    {
       if (x==100) cout << "position 100";
       if (y==1000) cout << "position 1000";
    }
    is what i need with that class

  9. #54
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: [RESOLVED] template for events

    But your're still talking at the code level - not at the high design level. Without using one line of code or any c++ terminology, explain what you are trying to accomplish.
    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)

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

    Re: [RESOLVED] template for events

    Quote Originally Posted by 2kaud View Post
    But your're still talking at the code level - not at the high design level. Without using one line of code or any c++ terminology, explain what you are trying to accomplish.
    1st i need declare 1 function variable, but that variable must accept parameters or not. how can i do it?
    typedef std::function<void(...)> OnSomethingHandler;

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

    Re: [RESOLVED] template for events

    Quote Originally Posted by Cambalinho View Post
    1st i need declare 1 function variable,
    You're still talking on the code level.

    Pretend the language is not relevant (C++, Java, C#, etc.). You are talking to us and you don't know what computer language we use, but we do know object-oriented analysis and design. What are you trying to accomplish? What design pattern, algorithm, paradigm are you trying to implement?
    Quote Originally Posted by Cambalinho View Post
    1st i need declare 1 function variable,
    Let's start right there. Why do you "need" to declare such a function? That's the issue that we have with your posts and code. We claim there is no "need" to do most of what you're doing -- the problem is that we can't give you workable solutions until we find out what you're trying to do.

    Remember the thread you started a few weeks ago?
    http://forums.codeguru.com/showthrea...-function-name

    Compare your "solution" to the one that others and myself gave. Our solution was only given once we understood what you were trying to do on a high-level.

    The solutions given to you in that thread were derived from first analyzing the problem at a high level, and then incorporating known and well-tested paradigms from C++ to implement the solution. We didn't just take C++ syntax we saw on a website and fight with it until it "fits" with what we want.

    For example, we expect you to say something like this:
    "I am attempting to design an event handler, where the event handler calls out to several different functions when an event occurs".

    Note that the description doesn't use a single word about classes, templates, void functions with varying arguments, etc. It describes the problem on a high-level and now would make us understand what it is you're doing. If you're going down the correct path of implementing such a thing, then we will say so and give you advice. If not, then we will say why it is the wrong path and give you alternatives (just like the previous thread that I mentioned).

    So please formulate a non-C++ description, similar to the one I wrote above, as to what you're trying to do. Once you do that, then you will get more help on this forum.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; November 8th, 2013 at 12:11 PM.

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

    Re: [RESOLVED] template for events

    Quote Originally Posted by Paul McKenzie View Post
    You're still talking on the code level.

    Pretend the language is not relevant (C++, Java, C#, etc.). You are talking to us and you don't know what computer language we use, but we do know object-oriented analysis and design. What are you trying to accomplish? What design pattern, algorithm, paradigm are you trying to implement?
    Let's start right there. Why do you "need" to declare such a function? That's the issue that we have with your posts and code. We claim there is no "need" to do most of what you're doing -- the problem is that we can't give you workable solutions until we find out what you're trying to do.

    Remember the thread you started a few weeks ago?
    http://forums.codeguru.com/showthrea...-function-name

    Compare your "solution" to the one that others and myself gave. Our solution was only given once we understood what you were trying to do on a high-level.

    The solutions given to you in that thread were derived from first analyzing the problem at a high level, and then incorporating known and well-tested paradigms from C++ to implement the solution. We didn't take C++ syntax and fight with it until it fits our ideas (as what you're doing now).

    Regards,

    Paul McKenzie
    i need 1 object that can change the function(it's value) and call that function. these function can recive arguments or not

  13. #58
    Join Date
    Apr 1999
    Posts
    27,449

    Re: [RESOLVED] template for events

    Quote Originally Posted by Cambalinho View Post
    i need 1 object that can change the function(it's value)
    Explain what you mean by "change the function's value". That is not clear.

    Regards,

    Paul McKenzie

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

    Re: [RESOLVED] template for events

    Quote Originally Posted by Paul McKenzie View Post
    Explain what you mean by "change the function's value". That is not clear.

    Regards,

    Paul McKenzie
    events a();
    a={ cout << "hello world";};

    (in other place we can change what a do)

  15. #60
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: [RESOLVED] template for events

    You're back to using c++ synatx again in your explanation. Why do you want to do this? At a higher design level, what are trying to achieve? We don't understand your design ojectives which is why we're having trouble helping you.
    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)

Page 4 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