Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

declaring constants in classes

Posted on 1998-12-16
7
Medium Priority
?
317 Views
Last Modified: 2010-04-02
Hello,

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 ?
0
Comment
Question by:meessen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 400 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.
0
 
LVL 22

Expert Comment

by:nietod
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;
0
 
LVL 8

Expert Comment

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

class foo
{
enum
{
 val1 = 5 ;
} ;

//or

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


0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:midnitelite
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.
    foo::foo()
    {
       const int val = 5;
    }


0
 
LVL 22

Expert Comment

by:nietod
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.
0
 
LVL 1

Author Comment

by:meessen
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 !
0
 
LVL 22

Expert Comment

by:nietod
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...
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

610 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