如何在C++;? 如何在C++中创建静态类?我应该能够做一些类似的事情: cout << "bit 5 is " << BitParser::getBitAt(buffer, 5) << endl; C++中的代码> CUT要创建一个类(不是静态类)的静态函数。 你可以使用BITPARSE::GETBITAUTE调用函数,而不实例化一个我假定是期望结果的对象。 < P>如果你想把一个“静态”关键字应用到一个类,比如你可以在C语言中,那么你就不能使用托管C++。
但是从示例的外观来看,您只需要在BitParser对象上创建一个公共静态方法。像这样: BitParser.h如何在C++;? 如何在C++中创建静态类?我应该能够做一些类似的事情: cout << "bit 5 is " << BitParser::getBitAt(buffer, 5) << endl; C++中的代码> CUT要创建一个类(不是静态类)的静态函数。 你可以使用BITPARSE::GETBITAUTE调用函数,而不实例化一个我假定是期望结果的对象。 < P>如果你想把一个“静态”关键字应用到一个类,比如你可以在C语言中,那么你就不能使用托管C++。,c++,oop,class,syntax,static,C++,Oop,Class,Syntax,Static,但是从示例的外观来看,您只需要在BitParser对象上创建一个公共静态方法。像这样: BitParser.h class BitParser { public: static bool getBitAt(int buffer, int bitIndex); // ...lots of great stuff private: // Disallow creating an instance of this object BitParser() {} }; class
class BitParser
{
public:
static bool getBitAt(int buffer, int bitIndex);
// ...lots of great stuff
private:
// Disallow creating an instance of this object
BitParser() {}
};
class TheDataToBeHidden
{
public:
static int _var1;
static int _var2;
};
namespace SharedData
{
void SetError(const char *Message, const char *Title);
void DisplayError(void);
}
#include "SharedModule.h"
BitParser.cpp
bool BitParser::getBitAt(int buffer, int bitIndex)
{
bool isBitSet = false;
// .. determine if bit is set
return isBitSet;
}
//Init the data (Link error if not done)
int TheDataToBeHidden::_var1 = 0;
int TheDataToBeHidden::_var2 = 0;
//Implement the namespace
namespace SharedData
{
void SetError(const char *Message, const char *Title)
{
//blah using TheDataToBeHidden::_var1, etc
}
void DisplayError(void)
{
//blah
}
}
//Call the functions using the hidden variables
SharedData::SetError("Hello", "World");
SharedData::DisplayError();
您可以使用此代码以与示例代码相同的方式调用该方法
如果您正在寻找一种将“static”关键字应用于类的方法,比如在C#中 静态类只是编译器的一只手,握着你,阻止你编写任何实例方法/变量
如果只编写一个没有任何实例方法/变量的普通类,也是一样的,这就是在C++中所做的,您还可以在命名空间中创建一个自由函数:
// header
namespace MyNamespace
{
void myMethod() ;
}
// source
namespace MyNamespace
{
void myMethod()
{
// etc.
}
}
在BitParser.h中
namespace BitParser
{
bool getBitAt(int buffer, int bitIndex);
}
在BitParser.cpp中
namespace BitParser
{
bool getBitAt(int buffer, int bitIndex)
{
//get the bit :)
}
}
一般来说,这是编写代码的首选方式。当不需要对象时,不要使用类。考虑
你想要的是用C++语义表示,在命名空间中放置你的函数(对于它<强>是/强>函数)。< /P> 编辑2011-11-11 <> C++中没有“静态类”。最接近的概念是一个只有静态方法的类。例如:
// header
class MyClass
{
public :
static void myMethod() ;
} ;
// source
void MyClass::myMethod()
{
// etc.
}
但您必须记住,“静态类”是类Java语言(例如C#)中的黑客,它们无法拥有非成员函数,因此它们必须将它们作为静态方法移动到类中
<>在C++中,你真正想要的是一个非成员函数,你将在命名空间中声明:
// header
namespace MyNamespace
{
void myMethod() ;
}
// source
namespace MyNamespace
{
void myMethod()
{
// etc.
}
}
为什么呢?
<>在C++中,命名空间比“java静态方法”模式更强大,因为:
- 静态方法可以访问类私有符号
- 私有静态方法仍然对每个人可见(如果不可访问),这在某种程度上破坏了封装
- 不能正向声明静态方法
- 在不修改库头的情况下,类用户不能重载静态方法
- 静态方法所能做的,没有什么比同一命名空间中的(可能是友元)非成员函数做得更好的了
- 名称空间有自己的语义(它们可以组合,也可以匿名,等等)
- 等等
// CPP
std::string Foo::myGlobal ; // You MUST declare it in a CPP
void Foo::barA()
{
// I can access Foo::myGlobal
}
void Foo::barB()
{
// I can access Foo::myGlobal, too
}
void barC()
{
// I CAN'T access Foo::myGlobal !!!
}
乍一看,从封装的角度来看,免费函数barC无法访问Foo::myGlobal似乎是件好事。。。这很酷,因为查看HPP的人将无法(除非采取破坏行动)访问Foo::myGlobal
但是如果你仔细观察它,你会发现这是一个巨大的错误:不仅你的私有变量必须在HPP中声明(因此,尽管是私有的,但对全世界都可见),而且你必须在同一HPP中声明所有有权访问它的函数
因此,使用一个私人静态成员就像裸体走在外面,在你的皮肤上纹身你的爱人名单:没有人被授权触摸,但每个人都可以偷看。还有一个好处:每个人都可以拥有授权玩你的私人游戏的人的名字
private
的确。。。
:-D
“匿名名称空间”解决方案
匿名名称空间的优点是使事物私有化
首先,HPP标题
// HPP
namespace Foo
{
void barA() ;
}
为了确保你的评论:没有无用的barB或myGlobal声明。这意味着没有人阅读标题知道巴拉背后隐藏着什么
然后,CPP:
// CPP
namespace Foo
{
namespace
{
std::string myGlobal ;
void Foo::barB()
{
// I can access Foo::myGlobal
}
}
void barA()
{
// I can access myGlobal, too
}
}
void barC()
{
// I STILL CAN'T access myGlobal !!!
}
如您所见,与所谓的“静态类”声明一样,fooA和fooB仍然能够访问myGlobal。但是没有其他人可以。除了CPP之外,没有人知道fooB和myGlobal的存在
与裸体行走、在皮肤上纹身地址簿的“静态类”不同,“匿名”名称空间是完全覆盖的,这似乎更好地封装在AFAIK中
这真的重要吗?
除非代码的用户是破坏者(作为练习,我将让您了解如何使用肮脏行为未定义的黑客访问公共类的私有部分…),否则private
就是private
,即使它在标题中声明的类的private
部分可见
尽管如此,如果您需要添加另一个访问私有成员的“私有函数”,您仍然必须通过修改头向全世界声明它,这对我来说是一个矛盾:如果我更改代码的实现(CPP部分),那么接口(HPP部分)就不应该更改。引用列奥尼达斯的话:“这是封装!”
编辑2014-09-20
什么时候类静态方法实际上比具有非成员函数的名称空间更好
当您需要将函数分组并将该组提供给模板时:
namespace alpha
{
void foo() ;
void bar() ;
}
struct Beta
{
static void foo() ;
static void bar() ;
};
template <typename T>
struct Gamma
{
void foobar()
{
T::foo() ;
T::bar() ;
}
};
Gamma<alpha> ga ; // compilation error
Gamma<Beta> gb ; // ok
gb.foobar() ; // ok !!!
名称空间alpha
{
无效
#include "SharedModule.h"
//Call the functions using the hidden variables
SharedData::SetError("Hello", "World");
SharedData::DisplayError();
public ref class BitParser abstract sealed
{
public:
static bool GetBitAt(...)
{
...
}
}
static struct S { // valid C, invalid in C++
int i;
};
class BitParser final
{
public:
BitParser() = delete;
static bool GetBitAt(int buffer, int pos);
};
bool BitParser::GetBitAt(int buffer, int pos)
{
// your code
}
class Class {
public:
void foo() { Static::bar(*this); }
private:
int member{0};
friend class Static;
};
class Static {
public:
template <typename T>
static void bar(T& t) {
t.member = 1;
}
};
class Foo {
public:
static int someMethod(int someArg);
private:
virtual void __dummy() = 0;
};
// C++11 ONLY
class Foo final {
public:
static int someMethod(int someArg);
private:
virtual void __dummy() = 0;
};
// C++11 ONLY
class Foo final {
public:
static int someMethod(int someArg);
private:
// Other private declarations
virtual void __dummy() = 0 final;
}; // Foo now exhibits all the properties of a static class
class A final {
~A() = delete;
static bool your_func();
}