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 ...
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? 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...
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...
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 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...
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:
...
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...
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?
...
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 (...
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 ...
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...
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 ...
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 )
...
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 *'
...
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 = . ....
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...
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?
...
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());...
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...