C# to C++ static class
I am porting some code from C# to C++. I am 开发者_StackOverflow中文版not sure how do I create a class like the static class in C#.
// in C#
public static temperatureClass{
private static int offset = 50;
private static Context context;
public static calculateTemperature(){
//use a;
//use context;
}
public static Context con{
set{
context = value;
}
}
}
int main() {
Context con1;
temperatureClass.con = con1; //con1 is a
temperatureClass.calculateTemperature();
}
Basically the temperatureClass is a utility class to perform calculation that no instances would be created. I have a few questions:
- Should the C++ version of calculateTemperature remain as static?
- How could I initialize the int offset in C++ if I keep it as static because it is used by static calculateTempearture function?
- Should I keep the con accessor as static in C++ as I need to set the context?
or more generally, what is the way of implementing an utility class like that in C++?
- The C++ version could very well be a class containing only static members
- The
offset
variable being of integral type, it can be initialized the same way as in C# if declared static (but you might want to add aconst
) - If
context
is static, the accessor must also be static as static member functions cannot access non-static member variables
Note that I don't think that this is good design. From my point of view, there isn't much sense in "whole static classes" in C++, and I'd rather use a set a free functions in a isolated namespace. If you choose the "static class" approach anyway, I'd recommend declaring the default constructor private to make it clear that the class has not point in being instantiated.
In C++, you don't need static classes, because you can have functions at namespace scope. In C#, static classes are needed because all functions have to be at class scope.
I'd do something like this:
// temp.h
namespace temperature {
void calculateTemperature(const Context& context);
}
// temp.cpp
namespace { // private stuff
int offset = 50;
}
namespace temperature {
void calculateTemperature(Context context){
//use a;
//use context;
}
}
// programm.cpp
#include "temp.h"
int main() {
Context con1;
temperature.calculateTemperature(con1);
}
Static classes in C# are simply classes that can not be instantiated, and contains only static method.
There's no explicit static class in C++, but you can simply achieve the same in C++ by hiding the constructors as private, and providing the public static methods.
By static initialization:
class temperatureClass { private: static int offset; };
int temperatureClass::offset = 50;
You can leave it static, so you will follow the original as much it's possible.
Some air code, without letting it go through the compiler (so if there's an error, please tell me, I will fix it, but I think most of the stuff should have been translated correctly straightforward from C# to C++).
class temperatureClass
{
private:
static int offset = 50;
static Context context;
temperatureClass(){}; // avoid instantiation
public:
static void calculateTemperature()
{
//use a;
//use context;
}
static void SetCon(Context c)
{
context = c;
}
}; // dont forget semicolon in C++ :-)
int main()
{
Context con1;
temperatureClass::SetCon(con1);
temperatureClass::calculateTemperature();
}
(if this is a good design or not is another question, but that's independent of C# or C++)
精彩评论