Thread: static functions

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    32

    static functions

    Hello there,
    i am forced to use a static function, unfortunately this means i cant call the variables which belong to this class from this function, what si the best way i can overcome this? The variable is gcroot<FileSystemWatcher^ > watcher;

    br

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by ashaikh432 View Post
    Hello there,
    i am forced to use a static function, unfortunately this means i cant call the variables which belong to this class from this function, what si the best way i can overcome this? The variable is gcroot<FileSystemWatcher^ > watcher;

    br
    Depends. Post a compilable example demonstrating the problem. Also, are you using straight C++ or some variant (as indicated by your usage of the '^' symbol)?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    The best way is to make that variable static too. Really, trying to get a non-static member variable form a static function makes no sense at all. What value would you expect: of what instance of the object? Or is there only one instance? In that case, you might want to consider a singleton class, though I doubt if that's the best solution.
    All in all, I think this question can't be answered as you went wrong somewhere earlier.

  4. #4
    Registered User
    Join Date
    Jul 2010
    Posts
    32
    hi guys, here is what i have done so far:
    i am using the filesystemwatcher function, the callback functions (or event handlers are static as so:
    static void OnChanged(Object* /*source*/, FileSystemEventArgs* e)
    {
    // Specify what is done when a file is changed, created, or deleted.
    Console::WriteLine(S"File: {0} {1}", e->FullPath, __box(e->ChangeType));
    }

    static void OnRenamed(Object* /*source*/, RenamedEventArgs* e)
    {
    // Specify what is done when a file is renamed.
    Console::WriteLine(S"File: {0} renamed to {1}", e->OldFullPath, e->FullPath);
    }

    Now in the same class but in a different function i have initialised the fileSystemWatcher as so:

    FileSystemWatcher* watcher = new FileSystemWatcher();
    watcher->NotifyFilter = static_cast<NotifyFilters>( NotifyFilters::LastAccess | NotifyFilters::LastWrite
    | NotifyFilters::FileName | NotifyFilters:irectoryName );

    watcher->Filter = "myfile.txt";

    // Add event handlers.
    watcher->Changed += new FileSystemEventHandler(0, Watcher::OnChanged);
    watcher->Created += new FileSystemEventHandler(0, Watcher::OnChanged);
    watcher->Deleted += new FileSystemEventHandler(0, Watcher::OnChanged);
    watcher->Renamed += new RenamedEventHandler(0, Watcher::OnRenamed);


    Now because i have given the absolute name for the filter, upon renaming the handlers wont be invoked
    because it is still searching for the orignal file name, so what i need to do is update the watcher->filter inside the rename handler in order to keep the file being monitored :S

  5. #5
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    There is a way to get around this but it is not the best idea. Normally the fact that you have to get around this is either a product of bad design or of being forced to use an API which forces C style callbacks as opposed to the observer/listener paradigm via interfaces.

  6. #6
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    Hm, deja vu...

    In C++ there is no other way than having static versions of those functions. The first parameter should hold a reference to your filesystemwatcher object so you can have a static lookup table to connect the filesystem watcher object to another class or subclass the watcher class so you can call one of it's new methods.

    Please note that those difficulties are due to your use of C++/CLI. If you need .NET use a native .NET Language like C# and you won't have those problems.
    Did you have any problems with the bold part?
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Did you also miss the part about this not being C++? Perhaps putting this in the RIGHT category would be better!
    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.

  8. #8
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Even if it isn't C++ the same 'trick' works but I won't post it b/c I don't recommend it.

  9. #9
    Registered User
    Join Date
    Jul 2010
    Posts
    32
    Quote Originally Posted by nvoigt View Post
    Hm, deja vu...



    Did you have any problems with the bold part?
    How on earth do i extract the FileSystemWatcher from the object? :S

    Code:
     static void fileRenamed( Object^ obj, RenamedEventArgs^ e )
    {
        obj->    <<<<< i cant see the reference from here.....
         
    }

  10. #10
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    The documentation says the object is the FileSystemWatcher that raised the event, so you can simply cast it. I haven't tested it though.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  11. #11
    Registered User
    Join Date
    Jul 2010
    Posts
    32
    thanks alot guys got it working, give me a shout if you guys want to see how i did it (nothing that great anyway).

  12. #12
    Registered User
    Join Date
    Jul 2010
    Posts
    32
    btw it is C++ not C#

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It's not C++, as we have been trying to tell you.
    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.

  14. #14
    Registered User
    Join Date
    Jul 2010
    Posts
    32
    i am telling you its C++!
    FileSystemWatcher Class (System.IO)

  15. #15
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by ashaikh432 View Post
    i am telling you its C++!
    FileSystemWatcher Class (System.IO)
    Nope - see here.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inserting a swf file in a windows application
    By face_master in forum Windows Programming
    Replies: 12
    Last Post: 05-03-2009, 11:29 AM
  2. How do i do this? (static structure inside class)
    By 39ster in forum C++ Programming
    Replies: 4
    Last Post: 11-17-2008, 03:14 AM
  3. Linking errors with static var
    By Elysia in forum C++ Programming
    Replies: 8
    Last Post: 10-27-2007, 05:24 PM
  4. Replies: 23
    Last Post: 07-09-2007, 04:49 AM
  5. Static functions.... why?
    By patricio2626 in forum C++ Programming
    Replies: 4
    Last Post: 04-02-2007, 08:06 PM