Ed J wrote:
> In a public include file, how can I declare an object and instantiate
> an object all in one line, avoid seemingly duplicate information? I
> am typically used to doing something like the following, but I've
> never liked having to provide two lines related to the same object:
>
> extern MyClass obj1;
> extern MyClass obj2;
> #if defined( INSTANTIATE_OBJECTS )
> MyClass obj1( 111 );
> MyClass obj2( 222 );
> #endif
>
> In one and only one C++ file, I would define INSTATIATE_OBJECTS to
> force the instantiate.
>
> I'm hoping to find a C-preprocessor trick that would let me use a
> single line to both declare and instantiate an object, depending on
> the value of a preprocessor macro. For example, I could use
> something like this (if I could embed a "//" comment into a macro
> definition):
> #if defined( INSTANTIATE_OBJECTS)
> #define DECLARE_VS_DEFINE
> #else
> #define DECLARE_VS_DEFINE ; //
> #endif
> extern MyClass obj1 DECLARE_VS_DEFINE ( 111 );
> extern MyClass obj2 DECLARE_VS_DEFINE ( 222 );
>
> Does such a preprocessor macro trick exist? Is there another way to
> do it? It would have to work with the Visual C compiler and GNU GCC.
As V says, you don't. That's why we have header files and source files.
Why can't you just put
MyClass obj1( 111 );
MyClass obj2( 222 );
in a .c file and include it in your project? That's the way the language is
designed.
--
Jim Langston
tazmaster@rocketmail.com