You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4cxx-user@logging.apache.org by Jon Dahl <jd...@yahoo.com> on 2008/09/12 19:00:39 UTC
Forward Declaration
Hello,
I'm wondering if it is possible to use forward declaration with log4cxx classes in a class header file. I would like to exclude the log4cxx headers files in my header files if possible and include the log4cxx header files in my cpp(source) files.
Should this work?
// Test.hpp
namespace log4cxx {
class LoggerPtr;
}
class Test {
private:
static log4cxx:LoggerPtr _logger;
public:
Test();
};
//Test.cpp
#include <log4cxx/logger.h>
using namespace log4cxx;
LoggerPtr Test::_logger(Logger::getRootLogger());
Test::Test() {
}
// end test.cpp
Thanks,
-jd-
Re: Forward Declaration
Posted by Curt Arnold <ca...@apache.org>.
On Sep 12, 2008, at 12:00 PM, Jon Dahl wrote:
> Hello,
>
> I'm wondering if it is possible to use forward declaration with
> log4cxx classes in a class header file. I would like to exclude the
> log4cxx headers files in my header files if possible and include the
> log4cxx header files in my cpp(source) files.
>
> Should this work?
>
No. LoggerPtr is a template expansion and you'd need at least the
template definition incorporate it in a header file. However, you may
want to consider not having a LoggerPtr in your class definition at
all, but just have it in your implementation. Something like:
myfoo.cpp
#include <log4cxx/logger.h>
// anonymous namespace, members are visible only in this compilation
unit
// like static but better
//
namespace {
log4cxx::LoggerPtr logger(log4cxx::Logger::getRootLogger());
}
void Foo::someFunction() {
LOG4CXX_INFO(logger, "Hello, World");
}
RE: Forward Declaration
Posted by re...@rubixinfotech.com.
Stephen - you're right... if the class itself is used, the compiler won't
know how to construct it... if it is a ptr, it won't complain.
Renny Koshy
President & CEO
--------------------------------------------
Rubix Information Technologies, Inc.
www.rubixinfotech.com
"Stephen Bartnikowski" <sb...@barkinglizards.com>
09/12/2008 01:16 PM
Please respond to
"Log4CXX User" <lo...@logging.apache.org>
To
"'Log4CXX User'" <lo...@logging.apache.org>
cc
Subject
RE: Forward Declaration
Somebody correct me if I'm wrong, but I don't think it will. In Test.hpp,
the compiler doesn't know what type LoggerPtr is. Since you're referring
to that type directly rather than by a pointer, the compiler needs to know
the full details. It doesn't know that in reality it's a pointer type
defined elsewhere. If for example, your code instead read:
...
class Test {
private:
static log4cxx::LoggerPtr* _logger;
...
then the compiler wouldn't complain.
- Stephen
From: Jon Dahl [mailto:jdahl1972@yahoo.com]
Sent: Friday, September 12, 2008 12:01 PM
To: log4cxx-user@logging.apache.org
Subject: Forward Declaration
Hello,
I'm wondering if it is possible to use forward declaration with log4cxx
classes in a class header file. I would like to exclude the log4cxx
headers files in my header files if possible and include the log4cxx
header files in my cpp(source) files.
Should this work?
// Test.hpp
namespace log4cxx {
class LoggerPtr;
}
class Test {
private:
static log4cxx:LoggerPtr _logger;
public:
Test();
};
//Test.cpp
#include <log4cxx/logger.h>
using namespace log4cxx;
LoggerPtr Test::_logger(Logger::getRootLogger());
Test::Test() {
}
// end test.cpp
Thanks,
-jd-
RE: Forward Declaration
Posted by Stephen Bartnikowski <sb...@barkinglizards.com>.
Somebody correct me if I'm wrong, but I don't think it will. In Test.hpp,
the compiler doesn't know what type LoggerPtr is. Since you're referring to
that type directly rather than by a pointer, the compiler needs to know the
full details. It doesn't know that in reality it's a pointer type defined
elsewhere. If for example, your code instead read:
...
class Test {
private:
static log4cxx::LoggerPtr* _logger;
...
then the compiler wouldn't complain.
- Stephen
_____
From: Jon Dahl [mailto:jdahl1972@yahoo.com]
Sent: Friday, September 12, 2008 12:01 PM
To: log4cxx-user@logging.apache.org
Subject: Forward Declaration
Hello,
I'm wondering if it is possible to use forward declaration with log4cxx
classes in a class header file. I would like to exclude the log4cxx headers
files in my header files if possible and include the log4cxx header files in
my cpp(source) files.
Should this work?
// Test.hpp
namespace log4cxx {
class LoggerPtr;
}
class Test {
private:
static log4cxx:LoggerPtr _logger;
public:
Test();
};
//Test.cpp
#include <log4cxx/logger.h>
using namespace log4cxx;
LoggerPtr Test::_logger(Logger::getRootLogger());
Test::Test() {
}
// end test.cpp
Thanks,
-jd-