const

changing the value of const variable in C++

I am trying to change the value of a variable which is defined as int const as below. const int w = 10; int* wp = const_cast <int*> (&w); *wp = 20; The value of w didn't change and was 10 even after the assignment, though it shows as if both w and wp are pointing to the same memory location. But I am able to the change the value of w,...

c++ meaning of the use of const in the signature

Please help me understand the following signature: err_type funcName(const Type& buffer) const; so for the first const, does that mean the contents of Type cannot change or that the reference cannot change? secondly, what does the second const mean? I don't really even have a hint. Thanks in advance, jbu ...

C# Common Data Parameters

Greetings, I'm a beginner to OO and programming and I have the following situation: I have a set of const values and enums that several of the classes I implement share. These classes are independent i.e. other than sharing these values, they do not interact with each other, so inheriting them won't work. I was wondering; If I create...

What's the use of const here

in int salary() const { return mySalary; } as far as I understand const is for this pointer, but I'm not sure. Can any one tell me what is the use of const over here? Thanks ...

c++ returning const reference of local variable

Hi! is it possible/ok to return a const reference even if the value the function returns is a local variable of this function? i know that locals are not valid anymore once the function returns - but what if the function is inlined and the returned value is only used within the callers scope? then the locals of the function should be in...

Usefulness of const (C++)

I'm a const fiend, and I strive to make everything as const as possible. I've tried looking at various dissassembly outputs from const and non const versions of functions, and I've yet to see a marked improvement however. I'm assuming compilers nowadays are able to do smart things with non const functions that could technically be cons...

C++ smart pointer const correctness

I have a few containers in a class, for example, vector or map which contain shared_ptr's to objects living on the heap. For example template <typename T> class MyExample { public: private: vector<tr1::shared_ptr<T> > vec; map<tr1::shared_ptr<T> , int> h; }; I want to have a public interface of this class that sometimes returns sh...

Help deciphering an NSString "passing argument ... from distinct Objective-C type warning"

Hi all, I am having trouble deciphering a "passing argument ... from distinct Objective-C type warning". I have a constant string declared as: extern NSString * const URL_1; and defined as: NSString * const URL_1 = @"http://someurl"; If I, say, assign that constant to an NSString as follows: NSString *URL = nil; ... URL = [[NSSt...

Significance of const keyword positioning in variable declarations

What is the significance of the positioning of the const keyword when declaring a variable in Objective-C, for example: extern const NSString * MY_CONSTANT; versus extern NSString * const MY_CONSTANT; Using the first version in assignments produces warnings about "qualifiers from pointer target type" being discarded so I'm assum...

Converting System::String to Const Char *

I am using Visual C++ 2008's GUI creator to make a user interface. When a button is clicked, the following function is called. The content is supposed to create a file and name the file after the contents of the textbox "Textbox' with '.txt' at the end. However, that leads me to a conversion error. Here is the code: private: System::Vo...

vector and const

Hello Consider this void f(vector<const T*>& p) { } int main() { vector<T*> nonConstVec; f(nonConstVec); } The following does not compile.The thing is that vector<T*> can not be converted to vector <const T*> , and that seems illogically to me , because there exists implicit conversion from T* to const T*. Why is this ? v...

Looping a Const Char

I need to loop a const char, and I've used a simple example of string loop: const char *str; for(int i = 0; i < 10; ++i) { str += " "; } But when I tried to compile, I got this: ubuntu@eeepc:~/Test_C_OS$ gcc -o kernel.o -c kernel.c -Wall -Wextra -nostdlib -nostartfiles -nodefaultlibs kernel.c:26: error: ‘for’ loop initial dec...

How to code Const and Mutable overloads?

I seem to have this pattern occuring pretty often in my code, with two functions performing the same task apart from the constness of their parameters/returns. int& myClass::getData() { return data; } // called for const objects const int& myData::getData() const { return data; } This offends my sense of DRY. It's no...

PHP 5.3 How to autoload constants?

Hey all. I was hoping that if I were to define constants in a separate namespace, like: <?php namespace config\database\mysql; const HOST = 'localhost'; const USER = 'testusr'; const PASSWORD = 'testpwd'; const NAME = 'testdb'; ?> That I would be able to use __autoload to automatically include them: <?php function __autoload($clas...

IDL in ATL/COM: Can I publish a const of a complex type?

I know how to publish a const of a simple type in IDL, for example: const long blah = 37 But I want to publish consts of complex types, with methods, or at least readable struct-like member fields. For example, perhaps a type called CarType, which has accessor fields like "get_Make", "get_Model", "get_Year", "get_BasePrice", et ceter...

Can I set a PHP class property from an existing variable?

I am trying to figure out how I want to handle settings in my PHP app. I have pretty much decide that I would like to use a Confg class file so it will be autoloaded and flexible in the future. Below is some stuff I was playing with. I know you cannot set a variable to popluate a Constant so I then try to use a public static propert...

What's the technical reason why class constants can't be arrays in PHP?

Anybody knows the technical reason why this constraint is placed on PHP classes (at least in v5.1x)? ...

Which overloaded version of operator will be called

Suppose i have declared subscript operators in a class char& operator[] (int index); const char operator[](int index) const; In what condition the second overload is called. Is it only called through a const object. In the following scenarios which version of operator will be called. const char res1 = nonConstObject[10]; nonCons...

Why catch an exception as reference-to-const?

I've heard and read many times that's better to catch an exception as reference-to-const rather than as reference. Why is try { // stuff } catch (const std::exception& e) { // stuff } better than try { // stuff } catch (std::exception& e) { // stuff } ...

const and pointers

Edit1: I realize this is hard to understand this question without having an insight of what I'm trying to do. The class A is not complete but it essentially stand for a C-array "proxy" (or "viewer" or "sampler"). One interesting usage is too present a C-array as a 2d grid (the relevant function are not shown here). The property of this c...