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){}
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.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.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.
Last edited by Hunter2; 05-04-2004 at 09:40 PM.
Just Google It. √
(\ /)
( . .)
c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.
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.
Last edited by skorman00; 05-04-2004 at 11:16 PM.
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.
I have been able to use static functions where ever I see fit. You can whip up a quick little app to convince yourself of that.
I'm getting my information from the C++ standard, sections 3.5 (Program and linkage) and Annex D to be precise.Originally Posted by skorman00
— When a name has internal linkage, the entity it denotes can be referred to by names from other scopes in the same translation unit.
3 A name having namespace scope (3.3.5) has internal linkage if it is the name of
— an object, reference, function or function template that is explicitly declared staticAnd because namespaces must either be at file scope or nested within another namespace, your example of static member functions doesn't apply. Perhaps everyone who thought otherwise should do more research before trying to make a correction.D.2 static keyword
1 The use of the static keyword is deprecated when declaring objects in namespace scope (see 3.3.5).
>I have been able to use static functions where ever I see fit.
Deprecated means that a feature is still supported, but may not be in a future revision.
Without further context, I have no choice but to assume that the original question was referring to a function declared at file scope. And without further context, you have no basis for arguing otherwise.
>I have been able to use static functions where ever I see fit.
When I said that, I meant that a static function does not have to be used in the same file you declared it. If a variable is static, then you need to use extern to use it elsewhere, but this does not apply to functions.
I've never gotten such an error. Like I said, write your own quick app to find out yourself
in function.hCode:#include <iostream> using namespace std; #include "functions.h" int main(void) { int x = 0; cout << Next(x); return 0; }
This works perfectly fine for me.Code:static int Next(int i) { return i + 1; }
skorman, both main() and Next() are effectively in the same file. When a file is #included, the header is copied into the source file.
Kip was referring to different .cpp files, or compilation units.
Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling