Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8361461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:42:25+00:00 2026-06-09T11:42:25+00:00

Why would I use a member function when I can pass a static function

  • 0

Why would I use a member function when I can pass a static function a reference to an object?

For example:

#include <iostream>

class Widget{
private:
    int foo;
public:
    Widget(){
        foo = 0;
    }
    static void increment( Widget &w ){
        w.foo++;
        std::cout << w.foo << std::endl;
    }
};

class Gadget{
private:
    int foo;
public:
    Gadget(){
        foo = 0;
    }
    void increment(){
        foo++;
        std::cout << foo << std::endl;
    }
};


int main(int argc, const char * argv[]){

    Widget *w = new Widget();
    Widget::increment( *w );

    Gadget *g = new Gadget();
    g->increment();

    return 0;
}

Is this more than a stylistic thing? My understanding is that member functions are created per object instance, while static functions are not — and since you can make static functions operate on a per instance basis like in the above example, shouldn’t it slightly more efficient to create static functions instead of member functions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T11:42:27+00:00Added an answer on June 9, 2026 at 11:42 am

    Memeber functions are are not created by instance. They have an implicit first parameter which is the this pointer, so they actually look quite similar to your static function.

    For example,

    struct Foo {
      void foo(int i) {}
    }
    
    Foo f;
    f.foo(42);
    Foo::foo(f, 42);
    

    the two last lines do the same. However, it is hard to see how one could implement this with static methods or functions:

    struct IFoo {
      virtual foo() const {}
    };
    
    struct Foo1 : virtual public IFoo {
      virtual foo() const {}
    };
    
    IFoo* f = new Foo1;
    f->foo();
    

    So, besides the syntactic sugar of allowing you to call the methods on an instance with the . operator, you need them for this kind of run-time polymorphism.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Inside of a static member function I need to get the type. class MyClass
Can I pass this to a function as a pointer, from within the class
Can I use generateDS.py in python in a similar way that I would use
I use singletons a lot because I hate to pass an object of the
Here is my problem. I made a class with a member function declared as
Im trying to develop a class in JavaScript I can use to access a
In this table how would I use YQL/Xpath to get all data members of
I would use a multi dimensional gaussian modell for regression. Rasmussen has a book
I would use Jmagick into an Android Project. Is it possible? I have to
I thought I would use a stored routine to clean up some of my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.