I have two questions about C++ programming.
1)int a[10] = new int*; is this statement valid?
2) extern void test(int a,int b) throw(const char *, RangeErr);
what does test() do, throw(const char*, RangeErr) means throw two exceptions? and what is the RangeErr? and why extern? what does this function do?
1) Not valid. An array is not a pointer (though it can be converted implicitly to one in some contexts). Instead, try:
int* a = new int[10];
2) This is a function declaration with a (now deprecated & ill-advised) throw specification. The extern is extraneous and unnecessary, but would indicate "external linkage", meaning that the function can be called from other compilation units. External linkage is the default for functions that are not defined in the anonymous namespace or otherwise declared "static".
The throw specification indicates that this function can throw two different types of exception (a C-style string and a RangeErr exception object that is likely defined in the library that provided this function). If the function attempts to throw any other kind of exception, std::unexpected() is called, which by default terminates the program. That said, no compiler that I know of does more than simply ignoring a non-empty throw specification, and I believe a different mechanism was devised for C++11.
No. An array is allocated automatically. You can use a pointer instead of an array, but not an array instead of a pointer.
It's not possible to tell what this function does, since you've only posted the declaration, not the definition. The throw shows what sorts of exceptions it can throw, which in this case would be a C-type string or a RangeErr, although only one at a time. RangeErr is presumably already defined. extern means the function is defined in some other file.
The throw syntax for functions turned out to be a mistake, after years of practical experience. It's possible that throw () will be somewhat useful, depending on situation and compiler, but exception specifications in general are confusing and sometimes prevent compiler optimizations. See this Guru of the Week post from Herb Sutter, who does know a thing or two about C++.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With