简体   繁体   English

c++ 在 header 中声明的私有成员与在 cpp 文件中声明的 static 变量

[英]c++ private member declared in header vs static variable declared in cpp file

i have a variable that i prefer to declare in a cpp file instead of the header file.我有一个变量,我更喜欢在 cpp 文件而不是 header 文件中声明。 It should be accessible to objects of that class only.它应该只能被该 class 的对象访问。 This variable should have a separate copy for every object of that class.对于该 class 的每个 object,该变量应该有一个单独的副本。 Inheritance is not necessary. Inheritance 不是必需的。

Normally, I'd just declare it in the class definition.通常,我只是在 class 定义中声明它。

Ah:啊:

class A {
    private:
        int number;
}

But, can I do this instead?但是,我可以这样做吗?

Bh:乙:

class B {
    private:
        // nothing
}

B.cpp: B.cpp:

static int number;

No, if you take the second approach, you're making it a static variable, which means you won't have a different copy for each object of that class (they'll all share that variable).不,如果您采用第二种方法,则将其设为 static 变量,这意味着您不会为该 class 的每个 object 拥有不同的副本(他们都共享该变量)。

Either way, if it should only be accessed by that class, it should go in the class declaration, and should not be a global variable.无论哪种方式,如果它只能由 class 访问,则它应该在 class 声明中为 go,并且不应是全局变量。 By making it a static global variable, you're restricting access just to the scope of the file, not to the class.通过将其设为 static 全局变量,您将限制对文件的 scope 的访问,而不是对 class 的访问。 As good programming practice, try using as few global variables as possible.作为良好的编程习惯,请尝试使用尽可能少的全局变量。

If your goal is to hide implementation details from a customer who only sees your header files (either for secrecy, or to avoid dependencies on library internals), the design pattern you're looking for is the pimpl pattern ("pointer to implementation").如果您的目标是向只看到您的 header 文件的客户隐藏实现细节(为了保密,或者避免对库内部的依赖),那么您正在寻找的设计模式是pimpl模式(“指向实现的指针”) .

myclass.h: myclass.h:

class MyClassImpl;

class MyClass
{
    public:
        MyClass();
        ~MyClass();
        int someFunc();

    private:
        MyClassImpl * pimpl;
}

myclass.cpp:我的类.cpp:

class MyClassImpl
{
    public:
        MyClassImpl();
        int someFunc();

    private:
        // whatever members you actually need
}

MyClass::MyClass()
{
    pimpl = new MyClassImpl();
}

MyClass::~MyClass()
{
    delete pimpl;
}

int MyClass::someFunc()
{
    return pimpl->someFunc();
}

// go on implementing MyClassImpl as you would have implemented MyClass

Beware: This example code does not have proper copy semantics.注意:此示例代码没有正确的复制语义。 You might want to use some smart pointer instead, or implement the proper copy constructor / assignment operator shenannigans.您可能想改用一些智能指针,或者实现正确的复制构造函数/赋值运算符恶作剧。

It is possible to hide data members in the cpp file using the private implementation idiom.可以使用私有实现习惯用法隐藏 cpp 文件中的数据成员。

//a.hpp
struct AData;

class A
{
public:
  A();
private:
  AData* data_members;
};

//a.cpp
struct AData
{
   int number;
};

A::A(): data_members(new AData()) {}

Not sure if it pays off with just one integer.不确定它是否只用一个 integer 就可以得到回报。 But this can be used to reduce compile times: you can modify A's "data members" and implementation at will without having to recompile files including a.hpp.但这可以用来减少编译时间:您可以随意修改 A 的“数据成员”和实现,而无需重新编译包括 a.hpp 在内的文件。

Also, prefer a suitable smart pointer instead of the plain pointer (one that has suitable copy behavior and can be declared with an incomplete type).此外,更喜欢合适的智能指针而不是普通指针(具有合适的复制行为并且可以用不完整类型声明的指针)。

This variable should have a separate copy for every object of that class. 

Keeping it static wont let you acheive this.保留它 static 不会让您实现这一目标。 It will be one copy shared by all instances.它将是所有实例共享的一份副本。

If you want each object to have its own copy of number then you will have to make it an member of your class.如果您希望每个 object 都有自己的number副本,那么您必须使其成为 class 的成员。

If you want a separate copy for every object of your class, you need to declare it in the header file or have some sort of static map between the object value and the value for that object that is initialized by the object constructor. If you want a separate copy for every object of your class, you need to declare it in the header file or have some sort of static map between the object value and the value for that object that is initialized by the object constructor.

If you want a variable to be associated with a class object, it has to be declared inside the class.如果您希望变量与 class object 相关联,则必须在 class 中声明它。 All the object data should be inside the class.所有 object 数据都应在 class 内。 That is called Encapsulation.这就是所谓的封装。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 标题中的私有静态const成员变量和cpp中的const变量 - private static const member variable in header vs const variable in cpp 声明私有时的静态成员变量 - static member variable when declared private C ++类声明为静态类成员 - C++ class declared as static class member 无法访问在 header 文件中声明的 cpp 文件中的私有结构 - Cannot access a private struct in cpp file that is declared in a header file 编译 C 程序时出错,该程序调用在 header 文件中声明并在单独的 cpp 文件中定义的 C++ function - Error compiling a C program which calls a C++ function declared in header file and defined in a separate cpp file 在C ++中,如何初始化在Singleton模板中声明的私有类的静态成员? - In C++, how to initialize static member of a private class declared inside a Singleton template? 标头中声明的C ++ extern数组在main.cpp中不可用 - C++ extern array declared in header not usable in main.cpp C++:实例化 header 文件中声明的 object - C++: instantiating object that is declared in a header file c ++错误:(私有数据成员)未在此范围内声明 - c++ error : (private data member) was not declared in this scope C++ 析构函数:无法访问在 class 中声明的私有成员 - C++ Destructor: cannot access private member declared in class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM