Thread: How to access class member and methods from static method(signal handler)

  1. #1
    Registered User
    Join Date
    Dec 2014
    Posts
    2

    How to access class member and methods from static method(signal handler)

    I have one problem. I am writting my program on C++ language. I have one promblem. I need to set signal handler for my process. As the signal is related with the process on system level I have faced the problem.
    My programm consists several classes. They are connected together. But it doesn't metter in this case.
    The problem is that I need to access to member and methods of the class from my signal handler. For instance , I have class named Foo at it has some members and methods.
    So from my handler I need to call its function and change members.
    I understand that compiler should know that this class instances will exist during all program execution.
    I have tried to set static member class Foo instance in another class , but this didn't solve the problem.
    I have no ideas what is correct approach to do this. Please explain how to correctly implement signal handling in such case.
    Thansks.




    Here is example of my code


    Code:
     class MyContainer
    {
    private:
    std::vector<Foo> container;
    public:
    int removeFromContainer(Foo* aFoo) { 
    // DO some stuff 
    return RESULT_CODE;
    }
    int addToContainer(Foo* aFoo) { 
    // DO some stuff 
    return RESULT_CODE;
    }
    };
    Here is my Main class
    Code:
    class MainClass
        {
        private:
            int member;
        public:
            void mainLoop(char* args) { 
              signal(SIGCHLD, &signalHandler);
         }
        };
    Here is my function for signal handling

    Code:
    void static signalHandler_child(int p)
              {
                   this->myContainerInstance->addToContainer(new Foo);
                    
              }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Awww. Come on.

    It's obvious all you're doing is making wild guesses (people who know what you're asking for can recognise the signs) based on some code in a language other than C++. All you've done is supply three disconnected pieces of code.

    You need to demonstrate genuine effort before you can expect help.

    Also, static member functions of classes NEVER receive a "this" pointer.

    There is no such thing as a main class in C++ (that is a hangover from the other programming language the code you nabbed was written in).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Since static methods belong to "all" instances or none, they cannot have a "this" pointer. Which instance would "this" belong to?
    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.

  4. #4
    Registered User
    Join Date
    Dec 2014
    Posts
    2
    Quote Originally Posted by Elysia View Post
    Since static methods belong to "all" instances or none, they cannot have a "this" pointer. Which instance would "this" belong to?
    Please provide correct way of implementing this.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Use a combination of static members/data and std::bind.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Composition,private Class member variables and methods
    By sigur47 in forum C++ Programming
    Replies: 13
    Last Post: 01-29-2012, 08:10 PM
  2. Cannot access a non-static member
    By codebot in forum C# Programming
    Replies: 9
    Last Post: 09-26-2010, 03:06 PM
  3. Access to methods of another (external) class
    By praul in forum C++ Programming
    Replies: 12
    Last Post: 04-19-2006, 11:42 AM
  4. Can not access static data member.
    By CottonXu in forum C++ Programming
    Replies: 5
    Last Post: 11-04-2005, 11:41 AM
  5. How to pass non-static method to signal() function?
    By registering in forum C++ Programming
    Replies: 3
    Last Post: 11-06-2003, 04:33 PM