Thread: the C++ have something similar to __event(VS.NET) or afx_msg(VC++)?

  1. #1
    Registered User
    Join Date
    Aug 2013
    Posts
    434

    the C++ have something similar to __event(VS.NET) or afx_msg(VC++)?

    the C++ have something similar to __event(VS.NET) or afx_msg(VC++)?
    for i do something like these:
    Code:
    class events
    {
        public:
            afx_msg void OnDestroy();
    };
    
    events test;
    void test::OnDestroy()
    {
        MessageBox(NULL, "hi", "hello", MB_OK);
    }

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Not directly, no.
    But you can pass in an argument for use a function callback.
    Example:

    Code:
    template<typename T> void foo(F f) { f(); }
    
    void bar() { foo(&MyCallbackFnc); }
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Aug 2013
    Posts
    434
    i'm so sorry, but i'm confused on your thot

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It would help if you explained why you're confused.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Aug 2013
    Posts
    434
    i can't 'connect' my sample code with your template function

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Here's another example of on how to use callbacks for notifications or events.
    Code:
    // Assume this is a window class that represents a visible window on the screen
    class Window
    {
    public:
        // When we create the window, we can tell it to tell us when something happens, e.g. it is being destroyed.
        template<typename F> Window(F OnDestroy)
        {
            // ...Do stuff...
            // We store the function we're supposed to call when the destroy notification fires.
            m_OnDestroy = OnDestroy;
        }
    
        ~Window()
        {
            // We're being destroyed, so call the function the caller specified to notify them when we're being destroyed.
            m_OnDestroy();
        }
    
    private:
        // Used to store the function to call for the On Destroy notification. We use std::function to erase the signature type so we don't have to template the class itself.
        std::function<void()> m_OnDestroy;
    };
    
    // Assume this is where your code that uses Window is.
    class MyClass
    {
    public:
        // Assume this function is currently being executed
        void SomeFunction()
        {
            // Create a new window, and tell it to notify us when it's being destroyed. We pass a lambda, which calls our OnDestroy function.
            Window MyWindow([this] { OnDestroy(); });
            // ...Do stuff...
    
            // Window goes out of scope here and thus invokes our OnDestroy function.
        }
    
        void OnDestroy()
        {
            std::cout << "MyWindow is being destroyed...\n";
        }
    }
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Aug 2013
    Posts
    434
    theres 2 things on MyClass:
    1 - it's a class, instead an object;
    2 - again... we redefinition the OnDestroy on MyClass(if we need use it outside of class).
    (i'm sorry if i fail on keywords)
    please see again my sample:
    Code:
    class events
    {
        public:
            afx_msg void OnDestroy();
    
    };
     
    events test;
    void test::OnDestroy()//error: the OnDestroy was not declared on test
    
    {
    
        MessageBox(NULL, "hi", "hello", MB_OK);
    
    }
    is there another keyword, instead 'afx_msg', for i don't need redifinition the 'OnDestroy' on 'test' object?
    (i'm sorry, but i'm trying do instances that let change some functions, on Global Scope)

  8. #8
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    If you are switching from NET to the pure WinAPI you will find numerous differences. WinAPI documentation: Learn to Program for Windows in C++

  9. #9
    Registered User
    Join Date
    Aug 2013
    Posts
    434
    i'm learning more and more about C\C++ and Win32. but i had notice several things that are relay practical and useful on programming.
    that's why, with some help, i had created the property class and it's excelent... well except for some C++ professionals(some things i will not understand, but maybe are rules.. i don't know).
    maybe, with time, i will figure out the solution. thanks to all

  10. #10
    Registered User
    Join Date
    May 2009
    Posts
    4,076
    Quote Originally Posted by joaquim View Post
    theres 2 things on MyClass:
    1 - it's a class, instead an object;
    2 - again... we redefinition the OnDestroy on MyClass(if we need use it outside of class).
    (i'm sorry if i fail on keywords)
    please see again my sample:
    Code:
    class events
    {
        public:
            afx_msg void OnDestroy();
    
    };
     
    events test;
    void test::OnDestroy()//error: the OnDestroy was not declared on test
    
    {
    
        MessageBox(NULL, "hi", "hello", MB_OK);
    
    }
    is there another keyword, instead 'afx_msg', for i don't need redifinition the 'OnDestroy' on 'test' object?
    (i'm sorry, but i'm trying do instances that let change some functions, on Global Scope)
    I think we have no idea what you are asking about.

    If you are asking about class methods instead of object methods; see the static keyword.

    FYI: The "::" scope symbol normally should have a class name or a namespace name in front of it.
    That is what caused the error.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How similar are C# and C++?
    By MilleniumFalcon in forum Tech Board
    Replies: 21
    Last Post: 03-09-2014, 02:30 PM
  2. How similar are C# and C++?
    By MilleniumFalcon in forum C++ Programming
    Replies: 3
    Last Post: 03-08-2014, 10:47 PM
  3. Date in mm-dd or similar
    By Muscovy in forum C++ Programming
    Replies: 12
    Last Post: 07-18-2009, 06:22 PM
  4. Something similar to novell
    By frozt in forum Windows Programming
    Replies: 0
    Last Post: 06-09-2006, 12:04 PM
  5. Automation (or similar) without MFC
    By Robert602 in forum Windows Programming
    Replies: 0
    Last Post: 11-14-2002, 10:29 AM

Tags for this Thread