dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Can you define a static member function in a class?

  1. #1
    Join Date
    Sep 2006
    Posts
    141

    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
    Join Date
    May 2008
    Posts
    96

    Re: Can you define a static member function in a class?

    Your friend is wrong.

    In fact, you cannot do it any other way. Static members must be defined separate from the class declaration. Typically that means that the class declaration goes in a header file and the static member definitions go in a cpp file.

    Hope this helps.

  3. #3
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: Can you define a static member function in a class?

    Quote Originally Posted by Duoas
    Your friend is wrong.

    In fact, you cannot do it any other way. Static members must be defined separate from the class declaration. Typically that means that the class declaration goes in a header file and the static member definitions go in a cpp file.

    Hope this helps.
    Nope. You can do either.

    Pure virtuals have to have a separated definition, if you give them one.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  4. #4
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863

    Re: Can you define a static member function in a class?

    well in any case the friend is wrong and that is what really matters
    Wakeup in the morning and kick the day in the teeth!! Or something like that.

    "i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)