By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,921 Members | 1,442 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,921 IT Pros & Developers. It's quick & easy.

private static function

P: 3
EDIT: ***SOLUTION FOUND***
Just to check, before I had the 2 functions in the second code box I used in the main the logger function and when I got it to be private I haven't erased the lines in the main.
*********
Question:
hello, If you remember me from yesterday, having some problems. I've defined a helpFunctions.cpp class with static functions. now I have a static function that I don't need the user to see so I'm trying to make it private.it works fine as public but once I put it in the private scope here is what I get:

Expand|Select|Wrap|Line Numbers
  1. private:
  2.     static void logger(string message, string outputFileName="")
  3. {
  4.  
  5.         Logger& loggerInstance = Logger::getInstance();
  6.         if(outputFileName != "")
  7.         {
  8.             loggerInstance.initialize(outputFileName);
  9.         }
  10.         else
  11.         {
  12.             loggerInstance.addMsg(message);
  13.         }
  14. }
  15.  
the error: ‘static void helpFunctions::logger(std::string, std::string)’ is private.

If it would help the logger instance that I'm using is a singleton instance.
The reason to put It private is because I have two functions:

Expand|Select|Wrap|Line Numbers
  1.     static void initialize(string outputFileName)
  2.     {
  3.         logger("",outputFileName);
  4.     }
  5.     static void addMsg(string message)
  6.     {
  7.         logger(message);
  8.     }
  9.  
So I don't want the user to use a the logger in a potentially wrong way.

thanks,
romand
Feb 27 '09 #1
Share this Question
Share on Google+
1 Reply


Savage
Expert 100+
P: 1,764
@romand
Simply put,you do not need that function at all.You can write initialize() like a first part of the logger function and addMsg() as the else case.Or if you want you can make a class which will wrap around logger adding another layer of indirection and than put that class as a static member:

Expand|Select|Wrap|Line Numbers
  1. class LogWrapper
  2. {
  3. private:
  4.    void logger(string msg,string file="")
  5.    {
  6.          //Implementation
  7.    }
  8.  
  9. friend class helperFunctions; //logger is private method,only friends can access            
  10.                                           //them.
  11. };
  12.  
  13.  
  14. //Your helperFunctions class(or however do you call it) 
  15. private:
  16.    Logwrapper wrapper;
  17.  
  18. };
  19.  
  20.     static void initialize(string outputFileName)
  21.     {
  22.         wrapper.logger("",outputFileName);
  23.     }
  24.     static void addMsg(string message)
  25.     {
  26.         wrapper.logger(message);
  27.     }

It's your call,but I would opt for the first one.
Feb 28 '09 #2

Post your reply

Sign in to post your reply or Sign up for a free account.