Alf P. Steinbach wrote:
* Alfonso Morra:
Alf P. Steinbach wrote:
* Alfonso Morra:
Alf P. Steinbach wrote:
>* Alfonso Morra:
>
>
>
>>In my (wrapper class) constructor, I have the following code:
>>
>>....
>>m_Appende r = new RollingFileAppe nder(log_file_n ame)
>>....
>
>
>Don't. Use a constructor initializer list. Initialization is not
>assignment .
>
I thoink you're missing the point I'm making. TThe point being this: The
private variable is declared somewhere else (meaning default construct
is invoked).
Either it's a member variable, or not. If it isn't, make it one. When it
is a member variable, it's only default constructed if you don't initialize
it in a constructor initializer list.
I have no doubt that you know what you're talking about. But honestly,
how does a comment such as the one help me to solve my problem?
You could try it out -- that's what I and others expect.
You should put in some effort yourself, such as at least trying.
What I
want to know, is a practical way to solve the problem - possibly, a one
line or two line code/pseudocode segment that show me how to solve the
problem, not generalized theories or rules from the C++ book (as other
people have tended to do). Sure, this may be asking a lot, but if you
don't have the time to answer my "basic questions", then please simply
don't reply to my posts.
Don't fear, I even help folks who badmouth their helpers instead of trying
anything on their own.
Here's an example of a class using a constructor initializer list instead of
assignment:
class Point
{
private:
int myX;
int myY;
public:
Point( int x, int y ): myX( x ), myY( y ) {}
// other member functions
};
Now try that out, and post some code if you can't get it to work.
Didn't mean to get up your nose Alf, and sorry about all the typos,
which probably didn't help my case much either. I am familiar with
constructor initializer list but they won't help (at least I can't see
how they can) in this case.
I am wrapping up the wonderful log4cplus code into a more managable
class. However, there is no documentation (the documentation is the
code) and I am using a provided example as the base of my wrapper class.
Here is the complete code of an example that I am using to write my
wrapper class:
int main( ) {
SharedAppenderP tr myAppender( new RollingFileAppe nder("myLogFile .log")) ;
myAppender->setName("myApp enderName") ;
std::auto_ptr<L ayout> myLayout = std::auto_ptr<L ayout>(new
log4cplus::Patt ernLayout("%d{% Y-%m-%d %a %H:%M:%S} %-5p [%t]: %m%n"));
myAppender->setLayout( myLayout );
Logger myLogger= Logger::getInst ance("myLoggerN ame") ;
myLogger.addApp ender(myAppende r);
myLogger.setLog Level ( INFO_LOG_LEVEL );
myLogger.log( INFO_LOG_LEVEL, "Info Level Message") ;
myLogger.log( TRACE_LOG_LEVEL , "Trace Level Mesage") ;
}
As you can see, the variables are all local variables that go out of
scope after main has ceased. What I am doing is wrapping up the
functionality provided by main, into a wrapper class (lets call this
LogWriter). It is clear from the code above, that I will need variables
(or pointers to objects) of type SharedAppenderP tr, Layout, LogLevel and
Logger at the very least. So armed with these facts, I proced as follows:
class LogWriter {
public:
LogWriter(int,L ogLevel);
virtual ~LogWriter() ;
LogMsg(const string&) ;
setLogLevel(Log Level) ;
...
private:
/* Keep hidden from prying eyes*/
LogWriter(const LogWriter&);
LogWriter& operator=(const LogWriter&);
/* Variables */
SharedAppenderP tr m_Appender ; //Default ctor invoked
log4cplus::tstr ing m_AppenderName ;
log4cplus::tstr ing m_LoggerName ;
log4cplus::tstr ing m_logFilename ;
std::auto_ptr<L ayout> m_Layout ; //Default ctor invoked
LogLevel m_lvl ;
Logger m_Logger ; //Default ctor invoked
bool m_debugOn ;
/*Methods */
log4cplus::tstr ing getLogNameByMod uleId( int ) ;
};
Since we have m_Appender, m_Layout and m_Logger already constructed
(default ctors), I need to know how to "re-create" (for lack of a better
word, so that I can use the variables passed to the ctor of LogWriter. I
(obviously) can't use the code in the example above main() function
verbatim and copy into the ctor of LogWriter, becaue all the variables
will be local to the ctor and after the ctor went out of scope, so will
be the local variables (local to LogWriter::LogW riter()) and all the
private variables will be left unitialised, or at ;least initialised
with values other than that speciifed in the constructor.
So my question then was (and is): how can I initialise my LogWriter
class so that the private variables are assigned the values passed in
the LogWriter ctor? (basically, all I want to do is to replicate the
functionality I havce provided in my main function above.
Armed with some (hopefully more useful information), I hope you will be
able to shed some light on how to solve this - it is not for the lack of
trying, I can assure, you.
MTIA