Article View: comp.std.c++
Article #3568Re: 'const' in header files
From: John I. Moore, Jr.
Date: Sat, 02 Mar 1996 01:39
Date: Sat, 02 Mar 1996 01:39
139 lines
5055 bytes
5055 bytes
Lars Farm wrote:
>
> Experts explained that this is as it should be for const float, but not for
> const int (I know that the std does not talk about warnings). The point is:
> The experts seems to find some kind of conceptual difference between named
> constant floats and named constant ints, such that declaring const int K =
> L; in a header is considered good practice but const float X = Y; in a
> header is not and justifies a warning. I don't think there should be any
> such difference.
I think that there may be some misunderstanding about what a const data
member actually represents. In general, a const defined at file scope can be
of any type. In C++, it is generally preferable to use const instead of
#define.
const int maxSize = 100; //preferable to "#define maxSize 100"
Now consider const data members of a class. First let's distinguish between
const data members and static const data members. Const (non-static) data
members are certainly allowed in a class, but they are const relative to an
individual object. Each object can have a different value for the const data
member. Consider the following:
class X
{
public:
X(int);
... // other members
private:
const int m_n;
... // other members
};
We could define the constructor as follows:
X::X(int n)
: m_n(n)
{
... // other statements
}
Thus declarations such as
X x1(5), x2(8);
would create two separate objects of class X, each having different values
of the const data member. The data member is const after construction of the
object.
Now let's address data members which are const and static. Such a data
member would be have the same const value for all objects of the class.
Consider the following:
class X
{
public:
X();
... // other members
private:
static const int m_n;
... // other members
};
The static const data member can be defined and initialized once outside the
class definition, usually in a separate source code file (rather than the
header file).
const X::m_n = 3;
Now all objects of the class see the same value (3) for the static const data
member.
All of the above discussion applies to the C++ described in Stroustrup's
1991 book as well as to the current draft standard. What has changed with
the standard is the ability to actually define and initialize the static
const data member within the class definition.
class X
{
public:
X();
... // other members
private:
static const int m_n = 3;
... // other members
};
Before this modification, one had to use enums within the class definition
class X
{
public:
X();
... // other members
private:
enum { int m_n = 3 };
... // other members
};
or else one had to use a constant declared at file scope
const int m_n = 3;
class X
{
public:
X();
... // other members
private:
... // other members
};
The restriction placed in the standard is that the ability to define and
initialize the static const data member within the class definition is
restricted to integral types. You can't do it, for example, with doubles or
user-defined types. As for the reasoning behind this restriction, you'll
have to ask someone on the standardization committee, but I suspect that it
has something to do with efficiency. By the way, you should know that
Stroustrup opposed this modification even for integral types since it could
be accomplished within the existing language by the use of enums as
illustrated above.
--
John I. Moore, Jr. phone: (301) 924-0680
SoftMoore Consulting email: 70672.1744@compuserve.com
16233 Monty Court
Rockville, MD 20853-1344
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Technical Details
Message-ID:
<31379F8F.7659@compuserve.com>Path:
arc1-ca.pugleaf.net!chronicle.mti.sgi.com!austernReferences:
<AD5A0C5196681CA0D@sleipner.nts.mh.se>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!chronicle.mti.sgi.com!austern From: "John I. Moore, Jr." <70672.1744@compuserve.com> Newsgroups: comp.std.c++ Subject: Re: 'const' in header files Date: 01 Mar 1996 17:39:58 PST Organization: SoftMoore Consulting Approved: austern@isolde.mti.sgi.com Message-ID: <31379F8F.7659@compuserve.com> References: <AD5A0C5196681CA0D@sleipner.nts.mh.se> NNTP-Posting-Host: isolde.mti.sgi.com X-Original-Date: Fri, 01 Mar 1996 20:08:31 -0500 X-Mailer: Mozilla 2.0 (Win95; U) X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBVAwUBMTenEUy4NqrwXLNJAQGkigIAmrBgqes3l7nYrA8j4yszoV3jB/zqA2Wv Gujcq44bOSnPkIsSBHI3moxBg6l8j3FaOB98XXoCkhHAz7P8gW91cg== =d4zk Originator: austern@isolde.mti.sgi.com