Can you define a static member function in a class?

This is a discussion on Can you define a static member function in a class? within the C++ Programming forums, part of the General Programming Boards category; Is there any regulation about where to define a static member function? My question is, can you define the static ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    284

    Can you define a static member function in a class?

    Is there any regulation about where to define a static member function? My question is, can you define the static member function in a separated .cpp file? Or you have to define it in its .h file?

    suppose you have a class in a test.h file

    Code:
    class A{
    public:
      static void foo();
    };
    Then, in the main.cpp file, you include the test.h and give foo() a definition.

    Code:
    void A::foo(){
    ....//do something
    }
    It works in my PC, but my friend says that's incorrect.

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Yes, you can put the implementation in a .cpp file.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    In fact, you should. If you don't, and instead put the implementation in the .h file, you can't include the header file more than once, lest you get a linker error about defining the same function twice.
    Code:
    $ cat header.h
    #ifndef HEADER_H
    #define HEADER_H
    
    #include <iostream>
    
    class C {
    public:
        static void function();
    };
    
    void C::function() {
        std::cout << "Hello, World!\n";
    }
    
    #endif
    $ cat header.cpp
    #include "header.h"
    
    int main() {
        C::function();
        return 0;
    }
    $ cat header2.cpp
    #include "header.h"
    
    void another_function() {
        C::function();
    }
    $ g++ header.cpp header2.cpp -o header
    /tmp/ccTTAxaC.o: In function `C::function()':
    header2.cpp:(.text+0x11c): multiple definition of `C::function()'
    /tmp/cca6d8S9.o:header.cpp:(.text+0x11c): first defined here
    collect2: ld returned 1 exit status
    $
    Incidentally, you can put the method body in a header file if it's inline:
    Code:
    dwk@cypress:~/c/cb$ cat header.h
    #ifndef HEADER_H
    #define HEADER_H
    
    #include <iostream>
    
    class C {
    public:
        static void function() { std::cout << "Hello, World!\n"; }
    };
    
    #endif
    $ g++ header.cpp header2.cpp -o header
    $ ./header
    Hello, World!
    $
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 04:06 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Help getting multiple keypresses in a DOS compiler
    By Nongan in forum Game Programming
    Replies: 2
    Last Post: 04-01-2005, 10:07 PM
  4. LISP (DrScheme) any one?
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 03-31-2004, 12:52 PM
  5. FAQ: Directional Keys - Useing in Console
    By RoD in forum FAQ Board
    Replies: 38
    Last Post: 10-06-2002, 05:42 PM

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