const

Help me debug this - invalid conversion from 'const char*' to 'char*'

I simply don't see why this error's popping up. Widget.cpp: In constructor 'Widget::Widget(Generic, char*, int, int, int, QObject*)': Widget.cpp:13: error: invalid conversion from 'const char*' to 'char*' Nowhere do I have a 'const char*' in terms of Widget's constructor. class Widget: public QObject { Q_OBJECT Q_PROPERTY(ch...

C++ const keyword - use liberally?

In the following C++ functions: void MyFunction(int age, House &purchased_house) { ... } void MyFunction(const int age, House &purchased_house) { ... } Which is better? In both, 'age' is passed by value. I am wondering if the 'const' keyword is necessary: It seems redundant to me, but also helpful (as an extra indication th...

Const Correctness Question in C++

I have a random question about const correctness. Lets say i have a class that is a singleton. class Foo : public Singleton<Foo> { friend class Singleton<Foo>; public: std::wstring GetOrSet(const int id) const; private: Foo(); ~Foo(); void LoadStringIntoMap(const int id, const std::wstring &msg); std::map<int...

How do I escape the const_iterator trap when passing a const container reference as a parameter

I generally prefer constness, but recently came across a conundrum with const iterators that shakes my const attitude annoys me about them: MyList::const_iterator find( const MyList & list, int identifier ) { // do some stuff to find identifier return retConstItor; // has to be const_iterator because list is const } The idea t...

C++ static constant string (class member)

Hi guys. I'd like to have a private static constant for a class (in this case a shape-factory). I'd like to have something of the sort. class A { private: static const string RECTANGLE = "rectangle"; } Unfortunately I get all sorts of error from the C++ (g++) compiler, such as: ISO C++ forbids initialization of member ...

Returning a const reference to vector of an object

Hi there, I have 2 questions related to the same problem: 1) How to return a reference to a vector which belongs to a class. I have this class: class sys{ protected: vector<int> s; public: sys(); vector<int>& getS() {return s;} //(1) }; (1) should return the reference of the vector s. However in the main: main(){ ...

C++: how to deal with const object that needs to be modified?

I have a place in the code that used to say const myType & myVar = someMethod(); The problem is that: someMethod() returns const myType I need to be able to change myVar later on, by assigning a default value if the object is in an invalid state. So I need to make myVar to be non-const. I assume I need to make myVar be non-referen...

Should useless type qualifiers on return types be used, for clarity?

Our static analysis tool complains about a "useless type qualifier on return type" when we have prototypes in header files such as: const int foo(); We defined it this way because the function is returning a constant that will never change, thinking that the API seemed clearer with const in place. I feel like this is similar to expli...

Trouble with const

I should get this by now, but I'm just not getting it yet. The trouble is operator='s argument could be non-const, but that breaks std::vector::push_back because it makes the item const, so operator= has to accept a const object. Well, I'm not certain on how I'm supposed to modify the this object working like this. #include <vector> #in...

In C#, can a method return List such that clients can only read it, but not write to it?

Let's say I have a C# class: class Foo { private List<Bar> _barList; List<Bar> GetBarList() { return _barList; } ... } A client can call it: var barList = foo.GetBarList(); barList.Add( ... ); Is there a way to make the Add method fail because only a read-only version of _barList is returned? ...

Confused on const correctness with static array of pointers to const objects

I'm still not sure I totally get how this particular case should work out. So if I want to declare an array of NSStrings that won't change, is this correct? static NSString * const strings[] = {@"String 1", @"String 2", ...}; Is the static necessary? (what does it do?) Am I missing an extra const somewhere? There's just too many pl...

Is type specifier required for const?

Is a type specifier required here? const c = 7; Bjarne Stroustrup's 'The C++ Programming Language' on page 80 says that this is illegal. However, I've been practicing some brainbench tests, and one of the questions states that the type defaults to int. Brainbench is usually correct, so I'm unsure of which reference is right, and I've ...

pointer-to-const conversion in C

The following code compiles without warning on GCC but gives a warning in Visual Studio 2005. const void * x = 0; char * const * p = x; x points to a constant object of unknown type, and p points to a constant pointer to char. Why should the assignment to p result in a warning? Again, this is C, not C++. Thanks. ...

STL algorithms and const_iterators

Hello, Today I wrote a small predicate to find matching symbols in a container. But I'm faced to a problem: I want to use this predicate in a std::find_if call inside a const-method of a class, searching in a container that is a member of this class. But I just noticed that neither std::find nor std::find_if are able to operate on con...

C++ : struggle with generic const pointer

I've run into some annoying issues with const-correctness in some templated code, that ultimately boils down to the following observation: for some reason, given an STL-ish Container type T, const typename T::pointer does not actually seem to yeild a constant pointer type, even if T::pointer is equivalent to T::value_type*. The followin...

How to convert 'const wchar*' to 'const char*' on Mac OS X?

Hello, Is there a elegant way to convert 'const wchar *' to 'const char *' on Mac OS X? Kat ...

const char * as a function parameter in C++

NOTE: I know there are many questions that talked about that but I'm still a beginner and I couldn't understand the examples. I got a function prototype that goes like this: int someFunction(const char * sm); Here, as you know, const char* means that this function can accept const or non-const pointer-to-char. I tried something like ...

C: const vs no const ..how come this compiles?

I have a simple C function which I declare as: int strLen(const char* str_) { str_ = (char*) 0; return 0; } I was very surprised that that compiles! Why is that? Whereas this onedoesn't compile (which makes sense): int str(const int i_) { i_ = 0; return 0; } ...

initializing a const multidimensional array in c++

I'm currently working through some exercises in a c++ book, which uses text based games as its teaching tool. The exercise I am stuck on involves getting the pc to select a word from a const array of words (strings), mixing the letters up and asking the player to guess the word. This was easy, but as a follow on the book asks to add the ...

How much memory does a constant take in C?

when doing like this: const int a = 5; I wonder if a will get 4-byte of memory just like a variable ? (in 32 bit system) ...