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 895687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:35:57+00:00 2026-05-15T14:35:57+00:00

I noticed while reading through my code that I have a static member function

  • 0

I noticed while reading through my code that I have a static member function which alters a private member of its class through a pointer to an instance of said class.

It compiles and functions without issue, but I just wanted to know whether or not it was kosher to edit a private variable in this way, from a member but static function, or if I should be implmenting a public setVar function.

Note that I’m not trying to bypass standard coding practice by editing member variables from a static function – the function is necessarily static so that it can be run as a thread using the POSIX pthread library.

Cheers,
Wyatt

  • 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-05-15T14:35:58+00:00Added an answer on May 15, 2026 at 2:35 pm

    Yes, this is valid.

    Although having a non-static member is better in most cases, static members are sometimes used in cases where you need to pass a function-pointer to an external library, like in your case for the pthread library.

    If it makes sense to change this private variable in other situations as well, and if you want to separate your class from the fact that it uses the pthread library, you could split up the class in two:

    • one class that handles the functionality (like your class now)
    • one class that handles the interfacing to pthread

    The second class will then set the variable in the first class via a public method.

    Example: this is probably your original code:

    class MyClass
       {
       public:
          static void someMethod(MyClass *);
       private:
          type myMember;
       };
    

    and this is how you could also write it:

     class MyClass
        {
        public:
           void setMember(type value) {myMember = value; /* other other logic */}
        private:
           type myMember;
        }
    
     class MyClassPThreadInterface
        {
        public:
           static void someMethod(MyClass *myclass) {myclass->...();}
        }
    

    That way, you completely separate your class from the fact that it is being used by the PThread library. It makes it usable in other cases as well (where the static-method is rather pointless) and it is also easy to add another thread-library (e.g. Windows threads) without polluting the original class.

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

Sidebar

Ask A Question

Stats

  • Questions 524k
  • Answers 523k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It's hard to debug this problem without seeing RunStream. So… May 16, 2026 at 9:47 pm
  • Editorial Team
    Editorial Team added an answer I'm not aware of a method or property to return… May 16, 2026 at 9:47 pm
  • Editorial Team
    Editorial Team added an answer It depends on the circumstances/assumptions about the program in general:… May 16, 2026 at 9:47 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Have noticed issue while testing iphone app that if one quickly opens/dismisses a modal
I noticed that certain code that evaluates some shoe sizes for an e-commerce site
While looking at the code in petclinic, part of Spring 3.0 samples I noticed
I have the following problem... For a while now i noticed a bug in
While creating a simple client for a REST service which I have stubbed out,
I encountered a problem when running some old code that was handed down to
I have been doing a lot of reading but not coming up with any
I noticed the WinForms RichTextBox has a ZoomFactor property that I assume is exactly
I've spent today looking into lockless queues. I have a multiple producer, multiple consumer
I have implemented an insertion sort in a double link list (highest to lowest)

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.