const

Is there a way to forbid casting to subclass that is non-const in C++?

Here is a complete example. I want to forbid using A::set from objects casted from B to A by allowing only casting B to const A. How to do it? (I can't use virtual functions) #include <iostream> #include <cassert> using namespace std; class A { public: int get() const { return i_; } void set(int i) { i_ = i; } protected: int ...

Static Constants in C#

I have this code; using System; namespace Rapido { class Constants { public static const string FrameworkName = "Rapido Framework"; } } Visual Studio tells me: The constant 'Rapido.Constants.FrameworkName' cannot be marked static How can I make this constant available from other classes without having to create...

Why does C# not allow const and static on the same line?

Why does C# not allow const and static on the same line? In Java, you must declare a field as 'static' and 'final' to act as a constant. Why does C# not let you declare const's as final? I make the further distinction that in Java, every interface is public and abstract, whether this is explicitly declared or not. Aren't const's ef...

Elegant solution to duplicate, const and non-const, getters?

Don't you hate it when you have class Foobar { public: Something& getSomething(int index) { // big, non-trivial chunk of code... return something; } const Something& getSomething(int index) const { // big, non-trivial chunk of code... return something; } } We can't implement either of t...

c++ constant in library; does not work

Hi, anyone knows why this does not work when I try to include a library with the following declarations: namespace wincabase { const char* SOMESTRING = "xx"; } While this is perfectly fine: namespace wincabase { const int X = 30; } I get a "multiple definitions" error with gcc for the first case when I link the lib. Thanks! ...

How to use constant strings in C# designer.cs code?

How do I reference a constant string in my .designer.cs file? A straight forward answer is to create a private string variable in my .cs file, then edit the designer.cs file to use this variable instead of hardcoding the string. But the designer doesn't like this throws an error. I understand why this can't work, but I'm not sure of w...

Dealing with lazy computation in C++ classes

Let's say I have a class: class NumberCollection { public: typedef std::set<int> SetType; typedef SetType::iterator iterator; void insert(int n); iterator begin(); iterator end(); size_t size() const; iterator difficultBegin(); iterator difficultEnd(); size_t difficultSize() const; private: ...

STL container assignment and const pointers.

This compiles: int* p1; const int* p2; p2 = p1; This does not: vector<int*> v1; vector<const int*> v2; v2 = v1; // Error! v2 = static_cast<vector<const int*> >(v1); // Error! What are the type equivalence rules for nested const pointers? I thought the conversion would be implicit. Besides, I'd rather not implement point-wise as...

Resharper always suggesting me to make const string instead of string.

which one is good: string sQuery = "SELECT * FROM table"; or const string sQuery = "SELECT * FROM table"; And why resharper always suggest me to do this? ...

Why do some const variables referring to some exported const variables get the value 0?

Consider the following. I have two exported constants as follows: // somefile.h extern const double cMyConstDouble; extern const double cMyConstDouble2; and // somefile.cpp const double cMyConstDouble = 3.14; const double cMyConstDouble2 = 2.5*cMyConstDouble; These constants are now referenced some place else to define two static (...

declare a array of const ints in C++

Hi, I have a class and I want to have some bit masks with values 0,1,3,7,15,... So essentially i want to declare an array of constant int's such as: class A{ const int masks[] = {0,1,3,5,7,....} } but the compiler will always complain. I tried: static const int masks[] = {0,1...} static const int masks[9]; // then initializing ...

Declaring a Const Variable in R

I'm working in R, and I'd like to define some variables that I (or one of my collaborators) cannot change. In C++ I'd do this: const std::string path( "/projects/current" ); How do I do this in the R programming language? Edit for clarity: I know that I can define strings like this in R: path = "/projects/current" What I really wa...

Const double initialised from Lua

I have a global variable: const double myvar = 5.1; Now, I'm converting this to read these values from Lua. However, I can't simply do: const double myvar = lua_tonumber(L,1); Since main() must first execute to start the Lua intepreter etc., but if I declare myvar afterwards, it will not be global. Is there any way to do achieve ...

Why can't I assign a const value, and what should I do instead?

I have a piece of code with the following rough signature: void evaluate(object * this) { static const int briefList[] = { CONSTANT_A, CONSTANT_Z }; static const int fullList[] = { CONSTANT_A, CONSTANT_B, ..., CONSTANT_Z}; const int const * pArray; const int nElements; int i; if ( this->needDeepsEvaluation ) ...

CArray and const template parameter

Is it possible to use const parameter to CArray I am currently using CArray like this but it won't compile: typedef CArray<const CString, const CString&> data_container; And I always get this compile error : error C2664: 'ATL::Checked::memcpy_s' : cannot convert parameter 1 from 'const CString *' to 'void *' ...

Copy const array to dynamic array in Delphi

I have a fixed constant array constAry1: array [1..10] of byte = (1,2,3,4,5,6,7,8,9,10); and a dynamic array dynAry1: array of byte; What is the easiest way to copy the values from constAry1 to dynAry1? Does it change if you have a const array of arrays (multidimensional)? constArys: array [1..10] of array [1..10] of byte = . ....

Are strtol, strtod unsafe?

It seems that strtol() and strtod() effectively allow (and force) you to cast away constness in a string: #include <stdlib.h> #include <stdio.h> int main() { const char *foo = "Hello, world!"; char *bar; strtol(foo, &bar, 10); // or strtod(foo, &bar); printf("%d\n", foo == bar); // prints "1"! they're equal *bar = 'X'; // seg...

How do I convert something of "string*" to "const string&" in C++?

For example, if I have the following: void foo(string* s) { bar(s); // this line fails to compile, invalid init. error } void bar(const string& cs) { // stuff happens here } What conversions do I need to make to have the call the bar succeed? ...

C++ static const variable and destruction

Hi, I have encountered a strange behavior with a simple C++ class. classA.h class A { public: A(); ~A(); static const std::string CONST_STR; }; classA.cpp #include "classA.h" #include <cassert> const std::string A::CONST_STR("some text"); A::A() { assert(!CONST_STR.empty()); //OK } A::~A() { assert(!CONST_STR.empty());...

Global const string& smells bad to me, is it truly safe?

I'm reviewing a collegue's code, and I see he has several constants defined in the global scope as: const string& SomeConstant = "This is some constant text"; Personally, this smells bad to me because the reference is referring to what I'm assuming is an "anonymous" object constructed from the given char array. Syntactically, it's le...