Program ConstTest;
Const constVar = 1;
Begin
constVar := 3;
WriteLn(constVar);
End.
It's pretty obvious that the above code will not compile, because it's not right to change the value of a constant.
However, following code will compile, and will return "1; 5; 3;", even though the array is a const:
Program ConstTest;
...
I am trying to define the equivalent of DateTime.MaxValue (C#) but in Objective C.
I don't want to keep creating NSDates every time I use it so I wish I had it as const.
The problem, the compiler returns "Initializer element is not constant"
Here is the code
static NSDate* DateTimeMinValue = [NSDateFormatter dateFromString:@"00:00:0...
Consider a class like this one:
class MyReferenceClass
{
public:
MyReferenceClass();
const double ImportantConstant1;
const double ImportantConstant2;
const double ImportantConstant3;
private:
void ComputeImportantConstants(double *out_const1, double *out_const2, double *out_const3);
}
There is a routine (ComputeIm...
Possible Duplicate:
Sell me on using const correctness
I'm eager to know the answer. [to "What is the benefit of const keyword in programming?"]
...
Hello everybody
1) const int[] array={1,2,3,4}; //this gives below error
"Error 1 'ConsoleApplication1.Main.array' is of type 'int[]'.
A const field of a reference type other than string can only be initialized with null"
In my opinion according to error messeagge, it is not meaningfull to use const for reference types.Am i wron...
Say we have a class with 10000 const string members.
class Schema
{
//Average string length is 20
public const string ID1 = "some.constant.value";
public const string ID2 = "some.other.constant.value";
//...
}
Not all fields are referenced in the rest of the code. Only 10% of them is accessed on startup - their reference is as...
I have a problem regarding 'static const' member initialization. In a templated class I define a const member and initialize it outside the class.
When I include the .h file where this class is implemented in multiple .cpp files, I get an LNK2005 error (I'm using VS2010) that says the constant is already defined.
// List.hpp
template <...
Why can I not do this:
char* p = new char[10];
void SetString(char * const str)
{
p = str;
}
SetString("Hello");
I have a const pointer to a char, why can I not assign the const pointer to another pointer?
It just seems illogical, as by assigning it to another pointer, you are not essentially violating the const-ness of the ch...
Suppose in C I have the functions
type* func (type*);
const type* func_const (const type*);
such that they both have the exact same internal logic.
Is there a way I can merge the two into one function, where if given a const type, it returns a const type; and if given a non-const type, it returns a non-const type? If not, what is a g...
From Effective C++, Item 3
/* case1 */ const std::vector<int>::iterator i // i acts like a T* const
/* case2 */ std::vector<int>::const_iterator ci // ci acts like a const T*
To remember how const applies, I used to remember the following from this article
Basically ‘const’ applies to whatever
is on its immediate left (other t...
class A{
private:
int a;
public:
A() {a = 4;}
const int& random1() const {return a; }
//int& random2() const {return a; }
const int* random3() const {return &a;}
//int* random4() const {return &a;}
};
int main(){
A objA;
cout<<objA.random1()<<"\n";
cout<<*objA.random3()<<"\n";
}
random2() an...
I have a problem with what appears to be some sort of implicit casting to const when I use iterators. I'm not really sure which code is relevant (if I did I probably wouldn't be asking this question!) so I will try my best to illustrate my problem.
typedef set<SmallObject> Container; //not const
void LargeObject::someFunct...
I heard that const members must be explicitly intialized, but the following compiles for me:
class someClass
{
int const x;
};
int main()
{
return 0;
}
...
What is the proper way to implement a getter method for a lazily-initialized member variable and maintain const-correctness? That is, I would like to have my getter method be const, because after the first time it is used, it's a normal getter method. It is only the first time (when the object is first initialized) that const does not ap...
I've noticed Resharper suggestion under "Common Practices and Code Improvements": Convert local variable or field to constant.
I've also noticed that in Bill Wagner's book "Effective C#: 50 Specific Ways to Improve Your C#" there is a langauge idiom "Prefer readonly to const" in which author explain risks of using consts.
My question i...
To add const to a non-const object, which is the prefered method? const_cast<T> or static_cast<T>. In a recent question, someone mentioned that they prefer to use static_cast, but I would have thought that const_cast would make the intention of the code more clear. So what is the argument for using static_cast to make a variable const?
...
Why does the following program give a warning?
Note: Its obvious that sending a normal pointer to a function requiring const pointer does not give any warning.
#include <stdio.h>
void sam(const char **p) { }
int main(int argc, char **argv)
{
sam(argv);
return 0;
}
I get the following error,
In function `int main(int, char **...
I usually use C type casting in C/C++ code. My question is, does adding the "const" keyword in the casting type mean anything to the result?
For example, I can think up several scenarios:
const my_struct *func1()
{
my_struct *my_ptr = new my_struct;
// modify member variables
return (const my_struct *)my_ptr;
// return my...
As has been discussed in several recent questions, declaring const-qualified variables in C (as opposed to const variables in C++, or pointers to const in C) usually serves very little purpose. Most importantly, they cannot be used in constant expressions.
With that said, what are some legitimate uses of const qualified variables in C? ...
The const modifier in C++ before star means that using this pointer the value pointed at cannot be changed, while the pointer itself can be made to point something else. In the below
void justloadme(const int **ptr)
{
*ptr = new int[5];
}
int main()
{
int *ptr = NULL;
justloadme(&ptr);
}
justloadme function should not be...