private static function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • romand
    New Member
    • Feb 2009
    • 3

    private static function

    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.c pp 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:

    Code:
    private:
    	static void logger(string message, string outputFileName="")
    {
    
    		Logger& loggerInstance = Logger::getInstance();
    		if(outputFileName != "")
    		{
    			loggerInstance.initialize(outputFileName);
    		}
    		else
    		{
    			loggerInstance.addMsg(message);
    		}
    }
    the error: ‘static void helpFunctions:: logger(std::str ing, 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:

    Code:
    	static void initialize(string outputFileName)
    	{
    		logger("",outputFileName);
    	}
    	static void addMsg(string message)
    	{
    		logger(message);
    	}
    So I don't want the user to use a the logger in a potentially wrong way.

    thanks,
    romand
  • Savage
    Recognized Expert Top Contributor
    • Feb 2007
    • 1759

    #2
    Originally posted by romand
    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.c pp 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:

    Code:
    private:
    	static void logger(string message, string outputFileName="")
    {
    
    		Logger& loggerInstance = Logger::getInstance();
    		if(outputFileName != "")
    		{
    			loggerInstance.initialize(outputFileName);
    		}
    		else
    		{
    			loggerInstance.addMsg(message);
    		}
    }
    the error: ‘static void helpFunctions:: logger(std::str ing, 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:

    Code:
    	static void initialize(string outputFileName)
    	{
    		logger("",outputFileName);
    	}
    	static void addMsg(string message)
    	{
    		logger(message);
    	}
    So I don't want the user to use a the logger in a potentially wrong way.

    thanks,
    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:

    Code:
    class LogWrapper
    {
    private:
       void logger(string msg,string file="")
       {
             //Implementation
       }
    
    friend class helperFunctions; //logger is private method,only friends can access            
                                              //them.
    };
    
    
    //Your helperFunctions class(or however do you call it) 
    private:
       Logwrapper wrapper;
    
    };
    
    	static void initialize(string outputFileName)
    	{
    		wrapper.logger("",outputFileName);
    	}
    	static void addMsg(string message)
    	{
    		wrapper.logger(message);
    	}

    It's your call,but I would opt for the first one.

    Comment

    Working...