If I do this:
// In header
class Foo {
void foo(bar*);
};
// In cpp
void Foo::foo(bar* const pBar) {
//Stuff
}
The compiler does not complain that the signatures for Foo::foo do not match. However if I had:
void foo(const bar*); //In header
void Foo::foo(bar*) {} //In cpp
The code will fail to compile.
What is going on?
I'm usin...
I've been reading through the details of the System libraries set and get methods yet the parameters are usually Strings.
Would you consider the use of String as parameters bad practise since the inclusion of enum?
A better alternative at minimum might be public final String, No?
...
This is related to some other questions, such as: this, and some of my other questions.
In this question, and others, we see we can declare and initialise string arrays in one nice step, for example:
const char* const list[] = {"zip", "zam", "bam"}; //from other question
This can be done in the implementation of a function with no bo...
class String
{
private:
char* rep;
public:
String (const char*);
void toUpper() const;
};
String :: String (const char* s)
{
rep = new char [strlen(s)+1];
strcpy (rep, s);
}
void String :: toUpper () const
{
for (int i = 0; rep [i]; i++)
rep[i] = toupper(rep[i]);
}
int main ()
{
...
What is the difference between a ::const_iterator and an ::iterator and where would you use one over the other?
...
I'm adding some lazy initialization logic to a const method, which makes the method in fact not const. Is there a way for me to do this without having to remove the "const" from the public interface?
int MyClass::GetSomeInt() const
{
// lazy logic
if (m_bFirstTime)
{
m_bFirstTime = false;
Do something once
...
I know the rule-of-thumb to read declarations right-to-left and I was fairly sure I knew what was going on until a colleague told me that:
const MyStructure** ppMyStruct;
means "ppMyStruct is a pointer to a const pointer to a (mutable) MyStructure" (in C++).
I would have thought it meant "ppMyStruct is a pointer to a pointer to a con...
I need to declare an array of pointers to functions like so:
extern void function1(void);
extern void function2(void);
...
void (*MESSAGE_HANDLERS[])(void) = {
function1,
function2,
...
};
However, I want the the array to be declared as constant -- both the data in the array and the pointer to the data. Unfortunately, I do n...
How can I convert an std::string to a char* or a const char*?
...
I have a C project where all code is organized in *.c/*.h file pairs, and I need to define a constant value in one file, which will be however also be used in other files. How should I declare and define this value?
Should it be as static const ... in the *.h file? As extern const ... in the *.h file and defined in the *.c file? In what...
Why must class members declared as const be initialized in the constructor initializer list rather than in the constructor body?
What is the difference between the two?
...
I'm attempting to map a set of key presses to a set of commands. Because I process the commands from several places, I'd like to set up a layer of abstraction between the keys and the commands so that if I change the underlying key mappings, I don't have to change very much code. My current attempt looks like this:
// input.h
enum LOG...
I this post, I've seen this:
class MonitorObjectString: public MonitorObject {
// some other declarations
friend inline bool operator==(/*const*/ MonitorObjectString& lhs,
/*const*/ MonitorObjectString& rhs)
{ return lhs.fVal==rhs.fVal; }
}
Before we can continue, THIS IS VERY IMPORTANT:...
My problem is that in my "Widget" class i've the following declaration:
MouseEvent* X;
In a member function I initialize the pointer with an address the normal way:
X = new MouseEvent;
Ok, this last line makes the compiler stop at:
error C2166: l-value specifies const object
All right, a MouseEvent is declared as a typedef t...
Hi,
I need to call a const function from a non-const object. See example
struct IProcess {
virtual bool doSomeWork() const = 0L;
};
class Foo : public IProcess {
virtual bool doSomeWork() const {
...
}
};
class Bar
{
public:
const IProcess& getProcess() const {return ...;}
IProcess& getProcess() {return ...;}
...
When trying to compile my class I get an error:
The constant 'NamespaceName.ClassName.CONST_NAME' cannot be marked static.
at the line:
public static const string CONST_NAME = "blah";
I could do this all of the time in Java. What am I doing wrong? And why doesn't it let me do this?
...
I am trying to do the following in a way or another:
const char EscapeChar = '\\';
const string EscapeString = EscapeChar.ToString(); // or ("" + EscapeChar)
This does't compile. Is there another way to make it work?
...
Is there a difference between having a private const variable or a private static readonly variable in C# (other than having to assign the const a compile-time expression)?
Since they are both private, there is no linking with other libraries. So would it make any difference? Can it make a performance difference for example? Interned st...
I have a constant value that I only plan to use once in my codebase. I am going to declare it using a const declaration.
If this was a function-level variable I would declare it at the point of usage, but doing so with a constant just seems to clutter my function.
...
Why does the following code in C work?
const char* str = NULL;
str = "test";
str = "test2";
Since str is a pointer to a constant character, why are we allowed to assign it different string literals? Further, how can we protect str from being modified? It seems like this could be a problem if, for example, we later assigned str to a ...