开发者

how to explain the following code?

I have two questions about C++ programming.

1)开发者_C百科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.


  1. No. An array is allocated automatically. You can use a pointer instead of an array, but not an array instead of a pointer.

  2. 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++.


1) No. You cannot assign a value to a bare array.

2) test() is the name of the function. throw(...) lists the sorts of exceptions it can throw. RangeErr is some type or another. extern means that the function is externally defined, and is accessible outside of the compilation unit (i.e. from other files.)


1)so No

2) that means that that function can and will only throw exceptions of type "const char *" and "RangeErr". As for what RangeErr is I would thing it would be defined else where in the code, most likely deriving from exception. Extern mains that the code for the function is externally linked (dll, so, or whatever) and you will have to check the api documents for that lib to find out what it does.^^ Hope that helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜