What is this mean? static void *name (void *data)

This is a discussion on What is this mean? static void *name (void *data) within the C++ Programming forums, part of the General Programming Boards category; I'm reading through some codes, and I see this function is declared as Code: static void *name (void *data){} I ...

  1. #1
    Distributed Programming beyonddc's Avatar
    Join Date
    Dec 2001
    Location
    Mass
    Posts
    31

    Question What is this mean? static void *name (void *data)

    I'm reading through some codes, and I see this function is declared as

    Code:
    static void *name (void *data){}
    I don't understand what this mean. Especially with the * next to the name and the data. Please explain... thanks!!

  2. #2
    Registered User
    Join Date
    May 2004
    Posts
    127
    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++.

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    127
    Quote Originally Posted by C+++C_forever
    Buthow can a function return something 'static' ? Isn't just a value that is being returned?
    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.

  4. #4
    Registered User
    Join Date
    May 2004
    Posts
    127
    Quote Originally Posted by C+++C_forever
    Ah, that means that any other file can not see that function in NO WAY ? Even a declaration will not make the function visible?
    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.
    Code:
    namespace {
      void *name(void *data)
      {
        [...]
      }
    }

  5. #5
    Registered User
    Join Date
    May 2004
    Posts
    127
    >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.

  6. #6
    Distributed Programming beyonddc's Avatar
    Join Date
    Dec 2001
    Location
    Mass
    Posts
    31
    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!!!

  7. #7
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>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.

  8. #8
    Registered User biosx's Avatar
    Join Date
    Aug 2001
    Posts
    230
    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;
    }

  9. #9
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    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.

  10. #10
    Registered User dalek's Avatar
    Join Date
    May 2003
    Posts
    135
    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.

  11. #11
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    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.

  12. #12
    Registered User
    Join Date
    May 2004
    Posts
    127
    Quote Originally Posted by skorman00
    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.
    I'm getting my information from the C++ standard, sections 3.5 (Program and linkage) and Annex D to be precise.
    — 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 static
    D.2 static keyword
    1 The use of the static keyword is deprecated when declaring objects in namespace scope (see 3.3.5).
    And 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.

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

  13. #13
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    >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.

  14. #14
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    I've never gotten such an error. Like I said, write your own quick app to find out yourself

    Code:
    #include <iostream>
    using namespace std;
    
    #include "functions.h"
    
    int main(void)
    {
         int x = 0;
        cout << Next(x);
    
        return 0;
    }
    in function.h
    Code:
    static int Next(int i)
    {
        return i + 1;
    }
    This works perfectly fine for me.

  15. #15
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. [GLUT] Pointers to class methods
    By cboard_member in forum C++ Programming
    Replies: 13
    Last Post: 02-16-2006, 03:03 PM
  4. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM
  5. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21