const

how to pass a stl vector to a function which takes a const [] (c++)

i have a 3d stl vector, vector<vector<vector<double> > > mdata; i also have a function myfun(const double ya[]); to be more precise, it's a function from the GNU Scientific Library, gsl_spline_init(gsl_spline * spline, const double xa[], const double ya[], size_t size); but this is not related to my problem. so now i want to pa...

C++ and QT4.5 - is passing a const int& overkill? Does pass by reference helps in signals/slots?

Two questions rolled into one here... I have a number of functions which are called multiple times per frame for a real-time video processing application. Taking advice about const and pass by reference, the functions have a signature somewhat like this void processSomething(const int& value); As I keep typing the few extra character...

Passing a constant matrix

Referring to this question and especially the accepted answer of litb, I wonder why the gcc complain about this: void func(const int (*ip)[3]) { printf("Value: %d\n", ip[1][1]); } int main() { int i[3][3] = { {0, 1, 2} , {3, 4, 5}, {6, 7, 8} }; func(i); return 0; } If I eliminate the const the compiler keeps still. Di...

Switchable Unique Identifier in C#

I'm implementing a system to send Messages between different parts of a program I'm writing. There are some generic message types as well as some specific to each part of the program. I would like to avoid the hierarchy rot inherent in deriving from a base message class for each type, So i'm encapsulating this type in an int or ushort....

Why is const-correctness specific to C++?

Disclaimer: I am aware that there are two questions about the usefulness of const-correctness, however, none discussed how const-correctness is necessary in C++ as opposed to other programming languages. Also, I am not satisfied with the answers provided to these questions. I've used a few programming languages now, and one thing that b...

How do I pass a const reference in C#?

In C++, passing const references is a common practice - for instance : #include <iostream> using namespace std; class X { public : X() {m_x = 0; } X(const int & x) {m_x = x; } X(const X & other) { *this = other; } X & operator = (const X & other) { m_x = other...

Should const functionality be expanded?

EDIT: this question could probably use a more apropos title. Feel free to suggest one in the comments. In using C++ with a large class set I once came upon a situation where const became a hassle, not because of its functionality, but because it's got a very simplistic definition. Its applicability to an integer or string is obvious, ...

C: Behaviour of the `const` keyword

I've been told that if I'm coding in ANSI-C to declare in the order that the variables will be used, assert that pointers are not null and that indices are within bounds, and to initialize just before usage of the variable. If I declare a const can I initialize it after a block of assertions and code ? In Java final initializations must...

Can/Why using char * instead of const char * in return type cause crashes?

I read somewhere that if you want a C/C++ function to return a character array (as opposed to std::string), you must return const char* rather than char*. Doing the latter may cause the program to crash. Would anybody be able to explain whether this is true or not? If it is true, why is returning a char* from a function so dangerous? Th...

How to initialize a const field in constructor?

Imagine I have a C++ class Foo and a class Bar which has to be created with a constructor in which a Foo pointer is passed, and this pointer is meant to remain immutable in the Bar instance lifecycle. What is the correct way of doing it? In fact, I thought I could write like the code below but it does not compile.. class Foo; class ...

Equivalent of C++ reference type in C#

Hi all I am using a dll written in C++ in a C# application. What is the equivalent for char const * unsigned short in C# thanks ...

C#: static Guid as argument of an attribute

How can I use a static Guid as argument in an attribute? static class X { public static readonly Guid XyId = new Guid("---"); } [MyAttribute(X.XyId)] // does not work public class myClass { } It does not work because Guid must be readonly, it can not be const. The string and byte[] representation would also be readonly. Is there a...

Should I return bool or const bool?

Which is better: bool MyClass::someQuery() const; const bool MyClass::someQuery() const; I've been using 'const bool' since I'm sure I remember hearing it's "what the ints do" (for e.g. comparison operators) but I can't find evidence of that anywhere, mostly due to it being difficult to Google and Intellisense not helping out any ;) ...

Static const string won't get initialized

I have some static const strings as private members of my C++ class. I am aware of the declaration in .h and definition (and initialization) in .cpp practice. In the class constructor I invoke a function that uses these static strings. Surprisingly when in constructor, the strings remain uninitialized (empty strings) which is creating a ...

Why isn't the [] operator const for STL maps?

Contrived example, for the sake of the question: void MyClass::MyFunction( int x ) const { std::cout << m_map[x] << std::endl } This won't compile, since the [] operator is non-const. This is unfortunate, since the [] syntax looks very clean. Instead, I have to do something like this: void MyClass::MyFunction( int x ) const { ...

const_cast in template. Is there a unconst modifier ?

I have a template class like this: template<T> class MyClass { T* data; } Sometimes, I want to use the class with a constant type T as follows: MyClass<const MyObject> mci; but I want to modify the data using const_cast<MyObject*>data (it is not important why but MyClass is a reference count smart pointer class which keeps the re...

C++ copy constructor causing code not to compile ( gcc )

I have the following code which doesn't compile. The compiler error is: "error: no matching function to call B::B(B)", candidates are B::B(B&) B::B(int)" The code compiles under either of the following two conditions: Uncommenting the function B(const B&) change 'main' to the following int main() { A a; B b0; ...

Declaring a non static const array as class member

How can we declare a non static const array as an attribute to class. Following code produces compilation error (“'Test::x' : member could not be initialized”)? class Test { public: const int x[10]; public: Test() { } }; ...

Is using const_cast for read-only access to a const object allowed?

In C++ I have a function that only requires read-only access to an array but is mistakenly declared as receiving a non-const pointer: size_t countZeroes( int* array, size_t count ) { size_t result = 0; for( size_t i = 0; i < count; i++ ) { if( array[i] == 0 ) { ++result; } } return result...

What does const mean following a function/method signature?

According to MSDN: "When following a member function's parameter list, the const keyword specifies that the function does not modify the object for which it is invoked." Could someone clarify this a bit? Does it mean that the function cannot modify any of the object's members? bool AnalogClockPlugin::isInitialized() const { retu...