Redeclaring an array

Started by
15 comments, last by M2tM 18 years, 2 months ago
Quote:Original post by rip-off
sweet.
specialised templates i assume.
didnt know that.


No. The sized constructor for std::vector<> looks like:

explict vector(size_type n, const T & value = T(), const Allocator & allocator = Allocator());
The vector is constructed with n elements copy constructed from value. When no value is specified, the default argument used is T(), which in the case of T being int, turns into 0.
Advertisement
Quote:Original post by SiCrane
No. The sized constructor for std::vector<> looks like:

explict vector(size_type n, const T & value = T(), const Allocator & allocator = Allocator());
The vector is constructed with n elements copy constructed from value. When no value is specified, the default argument used is T(), which in the case of T being int, turns into 0.


suppose it is my fault for assuming stuff.

i assumed that because:

int i;

doesn't guarentee that i is any value, that int() wouldn't either( not that i've often thought about how "int()" would work )

thanks for the info
Yeah. Weird, isn't it?

int main() {  int i; // not initialized to anything  int j = 0; // initialized to 0  int k(0); // initialized to 0  int l = int(); // initialized to 0  int m(); // not an int, but instead a function declaration :)  m();}int m() {  return 42;}
And it gets better:

float f;int i(f);      // Warning: converting from float to intint j(int(f)); // Let's try an explicit cast... gah, a function declaration! (*int k((int)f); // Okint m(int());  // No bonus, again a function declaration :P


*) I wonder why the grammar allows parentheses around the variable name in a variable declaration in the first place...
Quote:Original post by Sharlin
And it gets better:

float f;int i(f);      // Warning: converting from float to intint j(int(f)); // Let's try an explicit cast... gah, a function declaration! (*int k((int)f); // Okint m(int());  // No bonus, again a function declaration :P


*) I wonder why the grammar allows parentheses around the variable name in a variable declaration in the first place...


That's not... oh, you mean parentheses around the parameter name in a function declaration. I guess that's indeed what's going on there - it couldn't be declaring a function that accepts a function pointer, because that would be int j(int(*)(f)). :S Yeah, a serious annoyance, it is. Any word on whether 0x will clean this mess up?
Quote:Original post by Zahlman
That's not... oh, you mean parentheses around the parameter name in a function declaration.


It seems to work in any variable declaration, not just within function argument lists.

Quote:Any word on whether 0x will clean this mess up?


There certainly exist proposals to that end, the last word in the matter is this paper by Stroustrup and Dos Reis. Basically, they seek to generalize the use of {} initializers and also enable constructing user-defined types via initializer lists. The proposal seems very reasonable and probably has quite a good chance to be included at least in some form.

[Edited by - Sharlin on January 20, 2006 8:38:28 PM]
The C++ way has been mentioned.

In C (or if you really want to just use a plain array) you can do it like so:

to initialize
char *array = (char*)malloc(sizeof(char)*x);

to make bigger
x++;
array = (char*)realloc(sizeof(char)*x);


Let me say right now that if I catch you doing this in C++ (without a good reason) I'll break your kneecaps, but it is important to know how to do it.
_______________________"You're using a screwdriver to nail some glue to a ming vase. " -ToohrVyk

This topic is closed to new replies.

Advertisement