forward decl

Started by
8 comments, last by original vesoljc 20 years, 10 months ago
not sure if a forward declaration can solve this, but what the hell... :D i want to declare a global pointer to my logger. logger is declared as an Logger which uses a few base classes. how do i use the pointer in these base classes. if i do: class Logger; // forward decl extern Logger* gpLog; in *.cpp Logger* gpLog = 0; // will be set at runtime if(gpLog) { gpLog->Output("bla"); // this line prodeuces error, as Logger is not declared } if i include logger header in it''s base class, i (probably) get a sort of cyclic including hell... any ideas on how to solve this?
Abnormal behavior of abnormal brain makes me normal...
Advertisement
Does Logger absolutely need to use those base classes? Do you really need to log in those base classes? It would be nice to see your code, as it''s kind of hard to picture the dependencies you have without more information...

If you''re logging in those base classes during another log call, you could also get into an infinite logging loop and have a stack overflow...
well, i want to dy(i?)namcly load the logger module

hierarchy:
concrete_interface_unknown, abstract_base, abstract_module, abstract_logger, concrete_text_logger

as for "need for logging": well if everything is being logged, i''d sure like to handle this to...

as for infinite logging loop, i dont think it will happen, coz things are gona be "simple" and logging from higher level will control "sub" logging... something like that :D
Abnormal behavior of abnormal brain makes me normal...
I''m not sure if this will answer your question, but here''s what I''ve found I''ve had to do.

To see if I understand correctly...

Logger.h
#include "BaseClass.h"class Logger : public BaseClass {blahblahblah}; 


And in your BaseClass.h
class Logger;class BaseClass {Logger* blahblahblah};#include "Logger.h" 


And finally, in your BaseClass.cpp file...
#include "Logger.h"extern Logger* gpLog;... use your logger ... 


Would this set up solve your problem?

Remember to protect your header files from multiple inclusion.

Forward declaration works, but remember that you have to include the actual declaration of the class at some point. This should work/solve your problem.
i just can''t get it to work

i tried this:
note: everything is header guarded and wrapped in a namespace.

global.h
namespace axp{    class AbstractLogger;    // forward decl    extern AbstractLogger*  gpLog;}


global.cpp
using namespace axp;AbstractLogger*  axp::gpLog = 0;


"All" include global.h





base_class.h
#include "global.h"namespace axp{   base_class   {    ...};#include "base_class.inl"};


base_class.inl
inline base_class::base_class(){   if(gpLog)    {       // use gpLog    }}





base_class2.h
#include "global.h"#include "base_class.h"namespace axp{    class base_class2: public base_class    {       ...    };};


base_class2.inl
inline base_class2::base_class2(): base_class(){   if(gpLog)   {       // use gpLog   }}







logger.h
#include "global.h"#include "base_class2.h"namespace axp{    class logger: public base_class2    {       ...    };};


logger.inl
inline logger::logger(): base_class2(){   if(gpLog)   {       // use gpLog   }}






now, if i try to include logger at the end of base_class, i keep getting errors, saying that my base_class2 (?!?) is not defined.

Abnormal behavior of abnormal brain makes me normal...
Why not just make your logger a singleton - rather than have global vars? I take it it is global because you only want one instance of it for all your classes to access and use? If so a singleton is a nicer solution and gets rid of the declaration problems.

quote:
now, if i try to include logger at the end of base_class, i keep getting errors, saying that my base_class2 (?!?) is not defined.


That''s because base_class includes logger, which includes base_class2, which includes base_class, which includes logger, etc.

Circular dependencies. You need to get rid of that.

First off, start by moving all your .inl functions into .cpp and stop including them from the headers. Don''t include .inl files from the headers, but instead include the headers from the cpp. You can''t make everything inline. Once you do that, hopefully you''ll be able to get rid of the circular dependencies.
so, no inlining for the classes which are "sub logger" ?

@stubble
never used singletons before... but can a singleton handle descriebed hierarchy?
Abnormal behavior of abnormal brain makes me normal...
Yes a singlton can handle different subclasses - depending on how you implement it and how you want to call it in your code.

A quick search of google should help if you want to go this way - sorry I''m having a nightmare day - otherwise I''d post something a little more detailed.

as for inline-ing issue:

would something like this + double implementation solve it?

#if(SDK_DEBUG_LEVEL < DEBUG_MIN)	#define SDK_INLINED#endif#ifdef SDK_INLINED	#define INLINE inline#else	#define INLINE     #endif


[edited by - original vesoljc on June 5, 2003 4:34:10 PM]
Abnormal behavior of abnormal brain makes me normal...

This topic is closed to new replies.

Advertisement