declaring constants in classes

Posted on 1998-12-16
Last Modified: 2010-04-02

I'm currently porting C++ program from unix to NT and discover the following problem.

With gcc one can declare and initialize a constant value in a class. Apparently VC++ does not allow this.

class foo {
   const int val = 5;

This makes VC++ complain.
How do we define class dependent constants with VC++ ?
What is the standard behaviour ?
Question by:meessen
LVL 22

Accepted Solution

nietod earned 100 total points
ID: 1180285
the ability to declare and initalize a constant in a class was added to the standard, but is not supported by VC yet.
LVL 22

Expert Comment

ID: 1180286
in the meantime, you can use an enum like

class foo {
      enumt valType { val = 5};

or you can use a static constant like

class foo {
      static const int val;

const int foo::val = 5;

Expert Comment

ID: 1180287
You beat me to it nietod (just), oh well  a comment

class foo
 val1 = 5 ;
} ;


static const int val2 ;

}  ;

// then a .cpp
const int foor::val2 = 5 ;

I have read the gcc option is allowable but a recent addition to ANSI/ISO C++, so presumably a version of VC will eventually fix this.  Lot's of compilers currently barf on the const int declarations in a class

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.


Expert Comment

ID: 1180288
since in VC you can't initial a constant, you can initial in the Constructor like in the following.  This is appropiate and should be used.

    class foo {
       // const int val = 5;
    // put constant value in constructor.
       const int val = 5;

LVL 22

Expert Comment

ID: 1180289
that creates a constant that is local to the constructor procedure.  That is not what he wants.  You can declare a constant int member to the class and initialize it in the constructor.  however that is a waste of space as each object will store its own copy of this int.  That is why I suggested using a static int.

Author Comment

ID: 1180290
Ok nietod get the points though I used Answers2000 form:

enum { val = 5 };

I hope VC updates it's compiler really soon.
We have very programs here such constants are used everywhere !
LVL 22

Expert Comment

ID: 1180291
(I also proposed that solution, although I did not use an annonymous enum.)

>>I hope VC updates it's compiler really soon
they produce updates about every 12 to 18 months.  Last one was about 6 months ago...

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Building cUrl in Windows v7.43.0 4 32
Quicksort a dynamic deque 33 72
returning a dereferenced pts in C++ 10 151
How to copy an image file into clipboard C/C++? 1 170
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question