-
November 5th, 2010, 08:49 AM
#1
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.
-
November 5th, 2010, 09:16 AM
#2
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 .
-
November 5th, 2010, 09:21 AM
#3
Re: static functions
Originally Posted by joebar
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
-
November 5th, 2010, 09:40 AM
#4
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.
-
November 5th, 2010, 09:48 AM
#5
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.
-
November 5th, 2010, 10:52 AM
#6
Re: static functions
Originally Posted by D_Drmmr
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.
-
November 5th, 2010, 11:14 AM
#7
Re: static functions
Originally Posted by monarch_dodra
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|