I'm reading through some codes, and I see this function is declared as
I don't understand what this mean. Especially with the * next to the name and the data. Please explain... thanks!!Code:static void *name (void *data){}
Printable View
I'm reading through some codes, and I see this function is declared as
I don't understand what this mean. Especially with the * next to the name and the data. Please explain... thanks!!Code:static void *name (void *data){}
name is a function. It returns a pointer to void and uses a deprecated feature of C++ that ensures that the name is visible only in the file it was declared in. It takes a single parameter called data of type pointer to void. The asterisk in a declaration means a pointer, and a pointer to void is the generic pointer to anything except functions.
Pointers are a simple topic, but everyone has difficulty with them at first. For more information I suggest you consult your nearest book on C++.
The function doesn't return anything static. The function itself is static, thus invisible to other files. This is the deprecated feature I was speaking of.Quote:
Originally Posted by C+++C_forever
A declaration should match the definition, so it would be static as well. The static qualifier can be omitted from all but the first declaration, but this would still make the function hidden. Please note that you should not be using static on your functions. The standard C++ method for creating local functions is an unnamed namespace.Quote:
Originally Posted by C+++C_forever
Code:namespace {
void *name(void *data)
{
[...]
}
}
>1) OK, but if i use a declaration of that function in another file, will i be able to use that function in the other file?
No, you would have a linker error due to an undefined reference.
>2) Why does the Standard prefers a unnamed namespace instead of 'static' for local functions?
Because static is already overused for completely different purposes. The unnamed namespace for local names eliminates one source of confusion.
Almost forgot that I posted this. LOL
Got it! Thanks!
so I assumed that this method doesn't need to return anything, and the pass-in arguement for this method (void *data) can be anything such as an object reference?
Am I correct? If not, please correct me. Thanks!!! :)
>>this method doesn't need to return anything
Wrong, it returns a void* (pointer to *something*) :)
>>can be anything such as an object reference?
If by 'reference' you mean pointer, yes. If you mean a reference, no. There's a distinction, you know ;) The parameter passed in can be a pointer to anything, except perhaps a pointer to a reference, which I have no idea how it would work.
I wouldn't say static is deprecated.
It is used alot when you need data members or functions in a class accessible without having to instantiate an object.
Code:#include <iostream>
using namespace std;
class Example
{
public:
.
.
.
static int max( int n1, int n2 )
{
return ( n1 >= n2 ? n1 : n2 );
}
.
.
};
int main()
{
// Do not need to create an object, just call method
cout << Example::max( 4, 6 ) << endl;
return 0;
}
you also need static memeber functions if you need to return a static class variable. It is by no means deprecated. And I'm not sure where Kip is getting his information as far as "static makes a function visible only in the file it is created," because that is not the case.
No, he means that static has a couple of different uses, and the use in the above example (i.e. limiting a function to file scope) is deprecated. Not that static in general has been deprecated.
Although, I don't actually have the standard handy to check this. :)