George said:
[...about exceptions in constructors...]
Thanks.
BTW this is another question do you know how can I return a pointer to an
array not allowing to change the values it stores?
I hope so ;-)
ie: (the use of char is just as an example, don't change it to string

)
)
class A {
char *getName();
}
main() {
A a();
char *data = a.getName();
data[2] = 0x34;
}
Supposing everything is well initialize. I want that the values cannot be
changed. By setting const char *getName() I manage to do so, but doing
the following trick you can override it:
const char *data = a.getName();
char *data2 = (char *) data;
data[2] = 0x34;
Any idea?
Several. First, as is discussed very often on this newsgroup, there are
differences between:
char *
const char *
char * const
const char * const
char* defines a pointer variable, where the pointer and the pointee may
be changed.
const char* defines a pointer variable, where only the pointer, but not
the content pointed to may be changed.
char* const defines a pointer, where you may change the content, but not
the pointer.
and finally, with const char* const you may not modify anything at all.
But, there are some exceptions: char* gimpf= "blub"; is allowed. But,
don't try to modify the string. It's undefined behaviour. Short note:
just don't modify anything literal in source-code. This is probably the
same in C, at least it's the same in Lisp, but I don't know.
And again: do not use arrays, when you can use a vector<>. A vector is
guaranteed to have continuous memory, and you can therefore pass it's
content to an ordinarily C-function with like this:
std::vector<int> some_data;
some_data.resize(the_length_you_want);
long retval= some_c_func(&some_data[0], some_data.size());
The vector object cares for itself, i.e. deleting it's content when
going out of scope. And you can use all those STL algorithms and iterators.
for_each(some_data.begin(), some_date.end(), some_unary_function);
If you want to return a vector, which contents may not be altered,
return a const&. Still, the caller may just copy the vector, but then,
you don't have to take care about internal datastructures anymore.
Finally, I want to encourage you to read the C++ FAQ on
http://www.parashift.com/c++-faq-lite/
It discusses many troubles you may encounter when coming from C to C++.
Definitely a must-read before writing serious programs, as C++ tends to
be a very bad experince when learning-by-doing-without-reading. On the
other side, once you got the basic ideas (RAII, for the single most
important when you come from C (only my opinion)), it can be quite easy.
Well, I don't see you, but text, but that's ok for the place we are meeting.
You too.
Bye,
-- Markus