Avatar of CipherIS
CipherISFlag for United States of America asked on

C++ Constants

I want to create a static class that will hold constants.  Then I want to create another class that can get the value of static class constants.

Can anyone provide a code sample?

Thx
C++

Avatar of undefined
Last Comment
evilrix

8/22/2022 - Mon
SOLUTION
Fabrice Lambert

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
SOLUTION
phoffric

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
pepr

I am not sure what is the real intention behind the question. You may be used to Java or C# where creating a static class for the purpose is necessary. In C++, you can create a "module" that contains only the constants. If you want to group them somehow, you can use namespace. If the situation is even simpler, you can define the constants in the form of a single header file with literals defined via the #define preprocesor macro.
Fabrice Lambert

Plus, I do not recommend making a class for the purpose of returning values from another one.

A class's purpose is to provide its own services, not the services of its neighbourg.
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Fabrice Lambert

We're pretty much all ending at the same point:
A structure or enumeration (enclosed in a namespace or not, it is your choice).

@Sara:
Better use std::string instead of char* IMO.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
ASKER CERTIFIED SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
phoffric

>> just use a struct.
Yeah, that's what I said.
evilrix

Sorry, Paul. I missed that.
phoffric

Np. Actually thank you for reinforcing the idea and being more explicit with a piece of code.

I may actually get a chance to use C++11 in about a year. I thought I read that there is improvement in initializing static const float within a class. Is that right?
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
sarabande

Better use std::string instead of char* IMO.
for a constant literal there is nothing as efficient than a  char or byte array. there is no advantage in using a std::string for code like i posted it, only overhead.

Sara
evilrix

>>Better use std::string instead of char* IMO
I agree with Sara for the reasons she's stated. If you're dealing with const literal strings there's really no reason to consume the expense of using std::string, which will most likely cost a heap allocation and incurs the unnecessary cost of including the string header where it may very well not be needed. Use std::string when it makes sense to do so. Here it doesn't.