Topic: No way to define some extern consts?


Author: sdouglas@armltd.co.uk (scott douglass)
Date: 1996/03/14
Raw View
Hello all,

Given a class type with only a no-argument contstrutor:

struct T { T(); /* ... */ };  // assume no other ctors

Is there any way to define an extern const object of class T?

extern const T t; // nope:  this is a declaration
const T t2; // nope:  this is a definition, but t2 is not extern
extern const T t3();  // nope:  this is a declaration of the function
named t3 which returns a const T

So, can you find a way to *define* an extern const object type T?
(Besides adding some ctor with a dummy argument.)  If you can you9re
smarter than I am.

Thanks,
    --scott
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: sdouglas@armltd.co.uk (scott douglass)
Date: 1996/03/15
Raw View
In article <4iat2h$pk5@clarknet.clark.net>, gusty@clark.net (Harlan
Messinger) wrote:

: scott douglass (sdouglas@armltd.co.uk) wrote:
: : Hello all,
: :
: : Given a class type with only a no-argument contstrutor:
: :
: : struct T { T(); /* ... */ };  // assume no other ctors
: :
: : Is there any way to define an extern const object of class T?
:[...]
: You'll declare
:
:         extern const T t2;
:
: in the header file, but define
:
:         const T t2;
:
: as a global non-static object in the one source file in which you want it
: to reside.

Thanks.  As you mention and Geert-Jan Giezeman pointed out in private
e-mail, the key here is that the definition can be accomplished by having
both and extern declaration and a definition the same translation unit:

extern const T t;  // declaration
const T t;  // definition, still extern

You can't do it in one declaration but it's not hard after all.
    --scott
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: gusty@clark.net (Harlan Messinger)
Date: 1996/03/15
Raw View
scott douglass (sdouglas@armltd.co.uk) wrote:
: Hello all,
:
: Given a class type with only a no-argument contstrutor:
:
: struct T { T(); /* ... */ };  // assume no other ctors
:
: Is there any way to define an extern const object of class T?
:
: extern const T t; // nope:  this is a declaration
: const T t2; // nope:  this is a definition, but t2 is not extern

This is the correct one. The object ISN'T external from the point of view
of the module in which it's defined! You'll declare

 extern const T t2;

in the header file, but define

 const T t2;

as a global non-static object in the one source file in which you want it
to reside.
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]