|
Labelled as homework because this was a question on a midterm I wrote that I don't understand the answer to. I was asked to explain the purpose of each const in the following statement:
const char const * const GetName() const { return m_name; };
So, ...
Started by 4501 on
, 8 posts
by 8 people.
Answer Snippets (Read the full thread at stackoverflow):
It helps to read things like this from....
Also, the last const must come before the function body.
Remove the "const" after "char" and before the "*".
You have one more const than is syntactically allowed, that code would not compile.
|
|
Hi,
as its legal to convert a pointer to non-const to a pointer to a const.. similarly,why isnt it legal to convert (pointer to pointer to non-const) to a (pointer to pointer to a const) i.e,
char *s1 = 0; const char *s2 = s1; // OK... char *a[MAX]; /...
Started by ashishsony on
, 4 posts
by 4 people.
Answer Snippets (Read the full thread at stackoverflow):
From the standard:
const char c = 'c'; char* pc; const char** pcc = &pc; // not allowed *pcc = &c; *pc = 'C'; // would allow to modify a const object
Ignoring your code and answering the principle which expects a const char....
|
|
Assume I do this operation:
(X / const) * const
with double-precision arguments as defined by IEEE 754-2008 , division first, then multiplication.
const is in the range 0 < ABS(const) < 1 .
Assuming that the operation succeeds (no overflows occur...
Started by Quassnoi on
, 4 posts
by 4 people.
Answer Snippets (Read the full thread at stackoverflow):
If you read those theorems, it looks like taking const = 1/n where n is the sum = 1000.0010101011010001010101111000111101110100001 1
(BTW, the "* const" part of the expression is unnecessary: the....
But there is no explicit proof of that).
|
Ask your Facebook Friends
|
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...
Started by Chris on
, 11 posts
by 11 people.
Answer Snippets (Read the full thread at stackoverflow):
If the methods are overridden....
It's calling a non-const method from a const object that is forbidden.
Calling a const method of a non-const object is fine.
You don't have to do any casting trickery if the function is not overloaded .
|
|
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...
Started by Nick VanderPyle on
, 3 posts
by 3 people.
Answer Snippets (Read the full thread at stackoverflow):
You mustn't release _loadData:(const char *) buffer];.
Just like you would in C:
[self _loadData:(const char *)buffer];
should work.
|
|
As an extension to this question Are const_iterators faster? , I have another question on const_iterators . How to remove constness of a const_iterator ? Though iterators are generalised form of pointers but still const_iterator and iterator s are two...
Started by aJ on
, 11 posts
by 11 people.
Answer Snippets (Read the full thread at stackoverflow):
I don't think(3); vector<int>::const....
You can subtract the begin() iterator from the const_iterator to obtain the position the const_iterator is pointing to and then add begin() back to that to obtain a non-const iterator.
|
|
Using V1.8 z/OS XL C compiler, with warnings jacked-up using INFO(ALL), I get the following warning on line 4 of the code below:
WARNING CCN3196 Initialization between types "const int** const" and "int**" is not allowed. 1 int foo = 0; 2 int *ptr = &...
Started by Robert on
, 4 posts
by 4 people.
Answer Snippets (Read the full thread at stackoverflow):
Consider this code (I shuffled const around a bit to make it clear const** pp = &p; // presumably ok int const c = 123; *pp = &c; // okay, &c is int const*, and *p is int const* lvalue *....
It's a type safety violation.
|
|
While striving for const-correctness, I often find myself writing code such as this
class Bar; class Foo { public: const Bar* bar() const { /* code that gets a Bar somewhere */ } Bar* bar() { return const_cast< Bar* >( static_cast< const Foo*...
Started by Tobias on
, 13 posts
by 13 people.
Answer Snippets (Read the full thread at stackoverflow):
Use following trick:
....
You can do something like this:
class Bar; class Foo { public: const Bar* bar() const { return getBar(); } Bar* bar() { return getBar(); } private: Bar* getBar() const {/* Actual code */ return NULL mutable.
|
|
Let's say you have a class
class C { int * i; public: C(int * v):i(v) {}; void method() const; //this method does not change i void method(); //this method changes i }
Now you may want to define const instance of this class
const int * k = whatever; const...
Started by doc on
, 8 posts
by 8 people.
Answer Snippets (Read the full thread at stackoverflow):
When you are passing by value (as in your....
Verboseness sometimes, but const correctness is a common standard behaviour that you can't just redefine, it is completely irrelevant whether the constructor's parameter is declared const or not.
|
|
I have two getter members:
Node* prev() { return prev_; } int value() { return value_ }
Please note the lack of const identifiers (I forgot them, but now I want to know why this won't work). I am trying to get this to compile:
Node(Node const& other) ...
Started by Hooked on
, 6 posts
by 6 people.
Answer Snippets (Read the full thread at stackoverflow):
This type of operation is strictly forbidden by const....
You're attempting to call two methods which are not const an a const reference (calls to prev and value).
You're not trying to do a non-const to const conversion.
|