Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static Function Help C++

I can't get past this issue I am having. Here's a simple example:

class x
{
    public:
    void function(void);

    private:
    static void function2(void);
};

void x::function(void)
{
    x::function2(void);
}

static void function2(void)
{
     //something
}

I get errors in which complain about function2 being private. If I make it public (which I don't really want to do) I get errors about an undefined reference to function2. What am I doing wrong? Thank you!

like image 952
Alex Avatar asked Mar 22 '10 00:03

Alex


People also ask

Why static functions are used?

Static functions are used to invoke a class code when none of its instance exists( in more purer oop languages). Static functions can change static variables. Show activity on this post. Declaring class properties or methods as static makes them accessible without needing an instantiation of the class.

What is the advantage of static variable in C?

Static variables have a property of preserving their value even after they are out of their scope! Hence, static variables preserve their previous value in their previous scope and are not initialized again in the new scope.

Is static function better?

They are faster — Static methods are slightly faster than instance methods because in instance methods, you are also working with an implicit this parameter. Eliminating that parameter gives a slight performance boost in most programming languages.

Where are static functions stored in C?

The static variables are stored in the data segment of the memory. The data segment is a part of the virtual address space of a program. All the static variables that do not have an explicit initialization or are initialized to zero are stored in the uninitialized data segment( also known as the BSS segment).


2 Answers

  1. You can't have a function declaration and definition both in a class. Either move the definitions out of the class or remove the declarations.

  2. You can't call a function with a void as a parameter. That is used only in the declaration.

  3. The function definition for function2 if outside the class will need a x:: qualifier and static is not needed the second time.


    class x
    {
        public:
            void function(void); 
        private:
            static void function2(void);
    };

    void x::function(void)
    { 
        x::function2(); 
    }

    void x::function2(void)
    {
    }
like image 54
Sameer Avatar answered Nov 04 '22 00:11

Sameer


You must define function2 with

static void x::function2 (void)
{
    body
}

as it was with x::function

update: Yes. you don't need to mark class method with static when defining it.

class x
{
 public:
   void function(void);

 private:
   static void function2(void);
}; //end of class

// here are definitions of members
static void x::function(void)
{
  x::function2();
}
static void x::function2(void)
{
//something
}
like image 31
osgx Avatar answered Nov 04 '22 01:11

osgx