const

C# Const field in abstract class

I'm having trouble declaring a const field in an abstract class. Why is this? edit I should have clarified. My problem is that my child classes can't see the const field: protected const string Prefix = "dynfrm_"; If I remove the const keyword, I can get to it from a grandchild class. ...

What is the best practice regarding const instance methods?

In light of the accepted answer pointing out that returning a non-const reference to a member from a const instance method won't compile (without a cast or making the member variable mutable), the question has become more a general best-practices discussion of const instance methods. For posterity, here's the original question: If I ha...

Determine the name of a constant based on the value

Is there a way of determining the name of a constant from a given value? For example, given the following: public const uint ERR_OK = 0x00000000; How could one obtain "ERR_OK"? I have been looking at refection but cant seem to find anything that helps me. ...

const/readonly vs. programs like Cheat Engine

I have a program, and in that program there is some variables (username and "privilege-level") that are only changed when the user logs on. Is there a way to "secure" these varaibles from memory-editing etc while the program runs, but the program is still able to change them if the user logs on with an other username. I thought it would...

CONST variable removed. Member can no longer be accessed outside class.

Hi All, I have a Class named "Constants" that contains all the "constant" variable in my application (mostly Strings). The Class is written like so: public class Constants { public const string DATABASE="myDatabase"; public const string whatever="whatever"; public enum Colors { Red Blue Or...

Why does removing const give me linker errors?

I have a global variable: const std::string whiteSpaceBeforeLeadingCmntOption = "WhiteSpaceBeforeLeadingComment"; When I remove the const on this variable declaration, I get many occurrences of the following linker error: error LNK2005: "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > whiteSpac...

Declaring a const double[] in C# ?

Hello! I have several constants that I use, and my plan was to put them in a const array of doubles, however the compiler won't let me. I have tried declaring it this way: const double[] arr = {1, 2, 3, 4, 5, 6, 73, 8, 9 }; Then I settled on declaring it as static readonly: static readonly double[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9...

New to C++. Question about constant pointers.

I am trying to learn C++ via some web tutorials. I don't have a compiler available to me, otherwise I would try this out. I'm not sure what is meant by a const pointer. Does that just mean it always points to the same memory address? Why would you ever want to do that? Would the following code be legal? ... int * const aPointer = ne...

Is the sizeof(enum) == sizeof(int), always ?

Is the sizeof(enum) == sizeof(int), always ? Or is it compiler dependent? Is it wrong to say, as complier are optimized for word lengths (memory alignment) ie y int is the word-size on a particular complier.Does it means that there is no processing penalty if i use enums, as they would be word aligned. Is it not better if i put all th...

Can i declare member variable as const in class of c++?if yes,how?

Can i declare member variable as const in class of c++?if yes,how? ...

Pedantic gcc warning: type qualifiers on function return type

When I compiled my C++ code with GCC 4.3 for the first time, (after having compiled it successfully with no warnings on 4.1, 4.0, 3.4 with the -Wall -Wextra options) I suddenly got a bunch of errors of the form warning: type qualifiers ignored on function return type. Consider temp.cpp: class Something { public: const int getConstT...

How can I make an instance of my singleton const?

Hello, I'm trying to make a wrapper for Ogre (an open source 3D engine) 's standard logging class. I want it to have the same syntax as std::cerr, and also output to cerr when running on Linux. Here's what I have: #ifndef _LOGGER_H_ #define _LOGGER_H_ #ifndef _XSTRING_ #include <xstring> #endif #ifndef __LogManager_H__ #inclu...

Why won't C++ allow non-const to const conversion in copy ctor?

I have two getter members: Node* prev() { return prev_; } int value() { return value_ } Please note the lack of const identifiers (I forgot them, but now I want to know why this won't work). I am trying to get this to compile: Node(Node const& other) : prev_(other.prev()), value_(other.value()) { } The compiler rejects this. I tho...

Initializing aggregate unions

I've got a union : union my_union { short int Int16; float Float; }; I'd like to create : const my_union u1 = ???; const my_union u2 = ???; and initialize their values to be of different types respectively : u1 -> int16 u2 -> float How do I do that ? If the above is not possible, are there any workarounds? ...

C++: access const member vars through class or an instance?

In C++, is there any reason to not access static member variables through a class instance? I know Java frowns on this and was wondering if it matters in C++. Example: class Foo { static const int ZERO = 0; static const int ONE = 1; ... }; void bar(const Foo& inst) { // is this ok? int val1 = inst.ZERO; // or should I p...

Is it legal C++ to pass the address of a static const int with no definition to a template?

I'm having trouble deciding whether not this code should compile or if just both compilers I tried have a bug (GCC 4.2 and Sun Studio 12). In general, if you have a static class member you declare in a header file you are required to define it in some source file. However, an exception is made in the standard for static const integrals. ...

Why is a const variable available within a static method?

I have been writing code without realizing WHY I can access constant values within static methods. Why is it possible to access const values without declaring it as static? E.g.) It's legal to call IMAGE_FILE_EXTENSION within AddImageToDocument(...) public abstract class ImageDocumentReplacer : DocumentReplacer { private const ...

C++ and const - accessing a member function of a const reference

I have this snippet of code here. The intention is to make a copy of initialData. Since I am not modifying initialData in any way, I figure that I should pass it as a const reference. However, I keep getting this message when compiling. .\src\Scene\SceneAnimationData.cpp(23) : error C2662: 'SceneTrackerData::getRect' : cannot c...

How to declate a wide char constant in an IDL

We are migrating our C++ COM application to be unicode, and as part of this migration we want to migrate the constant strings in our IDL to unicode as well. The problem is that at the moment, we still compile it both in ANSI and in UNICODE, which means that we can't use the L"String" construct to declare wide charts. At the moment, our...

Do all C++ compilers allow using a static const int class member variable as an array bound?

In VC++ when I need to specify an array bound for a class member variable I do it this way: class Class { private: static const int numberOfColors = 16; COLORREF colors[numberOfColors]; }; (please don't tell me about using std::vector here) This way I have a constant that can be used as an array bound and later in the c...