const

Functions with const arguments and Overloading

Was tryin out the stackeroverflow qn so it got me thinking why not overload the the function and I came up with a slightly different code but it says the function cannot be overloaded. My question is why? or is there a another way? #include <iostream> using std::cout; class Test { public: Test(){ } int foo...

C++: const reference, before vs after type-specifier

What is the difference between the arguments in: int foo1(const Fred &arg) { ... } and int foo2(Fred const &arg) { ... } ? I don't see this case covered in the parashift FAQ. ...

static members and consts

class a { protected: const int _ID; public: a::a(int id){}; a::top(int num); }; class b : public a { static int ok; b::b(int id):a(id){}; a::top(ok); } int main() { int t=5; b opj=b(t); } first why i get this compile error that solved only when i remove the const non-static const member ‘const int Student::_ID’, can...

Adding const-ness after the fact in C++

Possible Duplicate: Is there some ninja trick to make a variable constant after its declaration? Consider the following minimal example: void MutateData(std::string&); int main() { std::string data = "something that makes sense to humans."; ::MutateData(data); // Mutates 'data' -- e.g., only changes the order of the cha...

how do i back insert to a vector with a const pointer

hello i have an error with the following code: in my h file i got the following vector: vector<Vehicale*> m_vehicalesVector; and in my cpp file i got the following function: void Adjutancy:: AddVehicale(const Vehicale* vehicaleToAdd) { m_vehicalesVector.push_back(vehicaleToAdd); } seems like the const Vehicale* vehicaleToAdd is...

Avoiding code duplication (const-correctness) redux

I was reading this question here here regarding const-correctness. The Scott Meyer solution seems like a good work-around, but what if you have a member function (which requires both a const and non-const version) that makes use of the this pointer. If the member function is const then this automatically means const this, which can mak...

C++: Trouble Using String as Argument of a Function

Okay, I am having trouble with the following piece of code (in a header file): #ifndef XML_H_INCLUDED #define XML_H_INCLUDED #include "libxml/parser.h" #include "libxml/xmlwriter.h" #include <string> class XmlFile{ public: XmlFile(string filename){ file = xmlParseFile(filename); } xmlDocPtr file; //Pointer to xml...

a problem about using const char* to pass parameter

I first store the 3 value into a pair of map like this: void AddMenuAtlasTexture( int tag, const char* filename, const char* textureName ) { map<const char*, const char*> _item; _item.insert(pair<const char*, const char*>(filename, textureName)); m_texturesToLoad.insert(pair<int, map<const char*, const char*> >(tag, _item)); }; th...

constant references with typedef and templates in c++

I heard the temporary objects can only be assigned to constant references. But this code gives error #include <iostream.h> template<class t> t const& check(){ return t(); //return a temporary object } int main(int argc, char** argv){ const int &resCheck = check<int>(); /* fine */ typedef int& ref; const ref error = check<int...

Initializing a static const char* array

Hi, here is my question I have this in my .h file static const char *Title[]; How do I initialize the array in my .C file the array to lets say "first", "second", "third" ...

Calling map::find with a const argument

I have an object: map<A*, string> collection; I would like to call the map::find function, but the value I have for the key is const, like in the following code, which does not compile: const A* a = whatever(); collection.find(a); The following code works and performs the equivalent of the find operation: const A* a = whatever(); ...

c# readonly (const?) function parameters

Coming from a c++ background, I'm used to sticking the const keyword into function definitions to make the object being passed in a read-only value. However, this is not possible in C#, as I found out (please correct me if I'm wrong). So, after some googling, I came to the conclusion that the only way to make a read only object is to wri...

Problem with const array changing c++

Hi, I have this code that tries to protect the user from array boundary errors. I don't get why this will compile, tho i've declared the array to be const, therefore, i'm suppose to get a compilation error! thanks a lot. /************ file: SafeAccessArray.h ********************/ template<typename T> class SafeAccessArray { private: i...

Doubt on a C++ interview question

I have read Answers to C++ interview questions among which there is one that puzzles me: Q: When are temporary variables created by C++ compiler? A: Provided that function parameter is a "const reference", compiler generates temporary variable in following 2 ways. a) The actual argument is the correct type, but it isn't Lv...

Why doesn't scala have C++-like const-semantics?

In C++. I can declare most things as const, for example: Variables: const int i=5; Scala has val i=5, however this will only prevent reassigning, not changing the object as the following exampe shows: C++: const int i[]={1,2,3,4}; i[2]=5; //error Scala: val a=Array(1,2,3,4) a(2)=5 //a is now Array(1, 2, 5, 4) It gets even worse with mem...

Does declaring C++ variables const help or hurt performance?

I understand the behavior of const-qualified data types. I am curious, though, if there is any performance gain or loss from over- or under-zealousness of qualifying variables as const. I am thinking particularly of variables declared and used exclusively within an isolated code block. For example, something like: const qreal padding = ...

How can I list all the const properties defined in a class

How can i list all the names (and values) of public (and private / protected) const defined in a class ? public class Layers { public const BACKGROUND:String = "background"; public const PARENT:String = "parent"; public const MAP:String = "map"; public const LINES:String = "lines"; public const POINTS:String = "poin...

Initialization of const variables

Hi all, I have code like this: bool doSomething() { std::cout << "I'm here!" return true; } const bool x = doSomething(); If placed in a cpp-file in my Visual C++ console application, the code is executed as expected before entering the main() method. However, if I place this code in a .cpp-file inside a static link library pro...

C++: Modifying array values for const

Hi, Consider the following: int a[2]; cin >> a[0] >> a[1]; const int D = a[1] - a[0]; cout << D << "\n"; a[1] = 5; a[0] = 2; cout << D << "\n"; I'm a bit confused now. Why does it print the same value for D? Why doesn't changing the array values change the value of D? At what point in time is the value of D determined and stored?...

c++ const member function that returns a const pointer.. But what type of const is the returned pointer?

I apologize if this has been asked, but how do I create a member function in c++ that returns a pointer in the following scenerios: 1. The returned pointer is constant, but the junk inside can be modified. 2. The junk inside is constant but the returned pointer can be modified. 3. Neither the junk, nor the pointer can be modified. Is i...