Hello,
Is there a difference between using const:
Cannot change the datatype but can change the value of a or b
int add(const int a, const int b);
Can change the datatype but cannot change the value of a or b
int add(int const a, int const b);
Cannot change the datatype and cannot change the value of a or b
int add(const int con...
I was reading about volatile member function and came across an affirmation that member function can be both const and volatile together. I didn't get the real use of such a thing. Can anyone please share their experience on practical usage of having member function as const and volatile together.
I wrote small class to test the same:
...
I have:
unsigned char *foo();
std::string str;
str.append(static_cast<const char*>(foo()));
The error: invalid static_cast from type ‘unsigned char*’ to type ‘const char*’
What's the correct way to cast here in C++ style?
...
In a project I maintain, I see a lot of code like this for simple get/set methods
const int & MyClass::getFoo() { return m_foo; }
void MyClass::setFoo(const int & foo) { m_foo = foo; }
What is the point in doing that instead of the following?
int MyClass::getFoo() { return m_foo; } // Removed 'const' and '&'
void MyClass::setFoo(...
As far as I know there is no way to do this, but I am going to ask just in case someone else knows how to do this. How can I declare a date as a const in Delphi?
The only solution I have found is to use the numeric equivalent, which is kind of a pain to maintain because it is not human readable.
const
Expire : TDateTime = 39895;...
I am trying to use a return type of const MyClass * const. However, I get a warning:
Warning: #815-D: type qualifier on return type is meaningless.
Is this not a valid type? I want a pointer than cannot be changed, and I want the thing it points to to not be changed either.
...
I need to implement read-only access to a private member container. If I return a constant reference is it possible to const_cast it and obtain a full access to the member? What's the technique to be used?
Thanks.
...
I have a dictionary that goes like this:
typedef struct dictNode {
int key;
char *value;
struct dictNode *next;
} Dict;
And a get() function that goes like this:
char *get(const Dict *dict, int key) {
if(!dict) return NULL;
Dict *currPtr = dict;
while(currPtr) {
if(currPtr->key == key) {
return cu...
Hi,
I want to implement an Observer of a Model class which does not change the Model. Thus, it should be able to use a const-Reference to access the Model. But the Registering of the Observer prohibits this.
Here is how the observer pattern is implemented in my Project:
//Attributes of type Observable are used by classes that want ...
Hi everybody. I was wondering if you guys could help me.
Here are my .h:
Class Doctor {
const string name;
public:
Doctor();
Doctor(string name);
Doctor & Doctor::operator=(const Doctor &doc);
}
and my main:
int main(){
Doctor d1 = Doctor("peter");
Doctor d2 = Doctor();
d2 = d1;
}
I want to ...
I have a class which looks something like this:
class MyClass
{
public:
// some stuff omitted
/***
A function which, in itself, is constant and doesn't change the class
***/
void myFunction( void ) const;
private:
/***
If loaded is true, then internal resources are loaded
***/
boolean loaded;
};
Because I desig...
What is the meaning of const in declarations like these? The const confuses me.
class foobar
{
public:
operator int () const;
const char* foo() const;
};
...
I have a basic question regarding the const pointers. I am not allowed to call any non-const member functions using a const pointer. However, I am allowed to do this on a const pointer:
delete p;
This will call the destructor of the class which by in essense is a non-const 'method'. Why is this allowed ? Is just to support this:
dele...
Is it true that const_cast is just a way to tell the compiler "stop moaning, treat this as a non-const pointer"? Are there any cases when const_cast itself is translated into actual machine code?
...
I have code like this:
NSData *data = [NSData dataWithContentsOfURL:objURL];
const void *buffer = [data bytes];
[self _loadData:buffer];
[data release];
the "_loadData" function takes an argument like:
- (void)_loadData:(const char *)data;
How do I convert "const void " to a "const char" on Objective-C?
...
I have a bit of code I'm running to test multithreading in MATLAB mex functions (I know MATLAB isn't thread safe... I'm just playing around to see what happens). The entry point to MATLAB C code functions has the signature of the mexFunction function in the code snipped at the bottom of the post. Since I want to essentially pass the ar...
I'm learning Ruby in my spare time, and I have a question about language constructs for constants. Does Ruby have an equivalent of the C++ const keyword to keep variables from being modified? Here's some example code:
first_line = f.gets().chomp()
column_count = first_line.split( %r{\s+} ).size()
print column_count, "\n"
I'd like to...
I have the following piece of code:
void TestFunc(const void * const Var1, const float Var2)
{
*(float*)Var1 = Var2;
}
It looks like I am changing the value of the const object the const pointer points to (thanks sharptooth), which should not be allowed. Fact is, none of the compilers I tried issued a warning. How is this possible?...
I know this is probably just a terminology mismatch but if i'm not mistaken i believe c# is? unless i'm missing something obvious??
...
private const uint URL_COUNT = 18;
private string[] _urls;
public Redirector()
{
this._urls = new string[URL_COUNT];
...
}
...
Results in “A constant value is expe...
I've seen some methods like this:
void SomeClass::someMethod() const;
What does this const declaration do, and how can it help optimize a program?
Edit
I see that the first part of this question has been asked before... BUT, it still doesn't answer the second part: how would this optimize the program?
...