We help IT Professionals succeed at work.

C++ Constants

CipherIS
CipherIS asked
on
249 Views
Last Modified: 2017-12-27
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
Comment
Watch Question

Fabrice LambertConsulting
CERTIFIED EXPERT
Distinguished Expert 2017
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT

Commented:
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 LambertConsulting
CERTIFIED EXPERT
Distinguished Expert 2017

Commented:
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.
CERTIFIED EXPERT
Top Expert 2016
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Fabrice LambertConsulting
CERTIFIED EXPERT
Distinguished Expert 2017

Commented:
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.
Senior Software Engineer (Avast)
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT

Commented:
>> just use a struct.
Yeah, that's what I said.
evilrixSenior Software Engineer (Avast)
CERTIFIED EXPERT

Commented:
Sorry, Paul. I missed that.
CERTIFIED EXPERT

Commented:
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?
CERTIFIED EXPERT
Top Expert 2016

Commented:
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
evilrixSenior Software Engineer (Avast)
CERTIFIED EXPERT

Commented:
>>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.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.