CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Aug 2006
    Posts
    144

    static functions

    Hi,

    I have a function which I need in different source files again and again. So I thought implementing it in an own class (Defaults) as static function. I have

    Code:
    //Defaults.h
    #ifndef DEFAULTS_H
    #define DEFAULTS_H
    
    class Defaults
    {
      public:
      static const double c;
      static void read();
    };
    
    #endif
    and

    Code:
    //defaults.cpp
    #include "defaults.h"
    #include <iostream>
    
    using namespace std;
    
    static void Defaults::read() //this is line no. 6
    {
      cout << "static read";
    }
    And then somewhere in my code I want to do:

    Code:
    //some other class.cpp
    #include "mainwindow.h"
    #include "defaults.h"
    #include <iostream>
    
    using namespace std;
    
    MainWindow::MainWindow() 
    {
    cout << "construtctor mainwindow"; 
    cout << Defaults::c; // c was set before this works
    
    Defaults::read(); // this doesn't work
    }
    When trying to compile I get

    defaults.cpp:6: error: cannot declare member function ‘static void Defaults::read()’ to have static linkage
    So what is wrong here?

    Apart from the bug itself here, is that the right way to achieve what I want to achieve?

    Thanks, J.

  2. #2
    Join Date
    May 2001
    Location
    Germany
    Posts
    1,158

    Re: static functions

    Putting helper functions in a separate class and make them static is what I do sometimes as well, so I'd say, it's not a bad idea.

    As to your problem: omit the 'static' in the .cpp file. It is only needed in the header file .

  3. #3
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: static functions

    Quote Originally Posted by joebar View Post
    So what is wrong here?
    You don't need to specify the static keyword before the function definition, only before the declaration in the header file.
    Apart from the bug itself here, is that the right way to achieve what I want to achieve?
    I don't know what you want to achieve. However, what you have here is nothing but a glorified global variable. There is little use for a class with only static members (unless the class is designed to be used as a template argument, e.g. in policy based design). Instead you can use a namespace to prevent polluting the global namespace. But then still, you are using a global variable, which is often a sign of bad design.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  4. #4
    Join Date
    Aug 2006
    Posts
    144

    Re: static functions

    Thanks, the bug itself is resovled.

    I want to use this class to define glober helper functions I need again and again in different classes, files, etc. So if there is a better way of doing so I am open to it.

  5. #5
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: static functions

    Typically the way to design reusable helper functions is to carefully distill the interface to be as general as possible, and then stick it in an appropriate header file.

  6. #6
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: static functions

    Quote Originally Posted by D_Drmmr View Post
    But then still, you are using a global variable, which is often a sign of bad design.
    Global const variable. AFAIK, there is nothing wrong with those. For example: std::string::npos, std::ios::in, etc...
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  7. #7
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: static functions

    Quote Originally Posted by monarch_dodra View Post
    Global const variable. AFAIK, there is nothing wrong with those. For example: std::string::npos, std::ios::in, etc...
    You're right. I didn't notice the const and assumed the read() function would load the value from file or something.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured