简体   繁体   English

是否需要在复制构造函数中复制静态成员,如果是,该怎么做?

[英]Does static member need to be copied in copy constructor and if yes, how to do it?

I have a class with a container that is declared static: 我有一个带有声明为static的容器的类:

class test {

public: 
  test(const ClassA& aRef, const std::string& token); 
  test(const test& src); 
  ~test();

private: 
  ClassA& m_ObjRef;
  static std::vector<std::string> s_toks; 
};

The s_toks container is initialized as follows in the constructor defined in test.cpp: s_toks容器在test.cpp中定义的构造函数中初始化如下:

std::vector<std::string> test::s_toks; 

    test::test(const ClassA& aRef, const std::string& token) 
       : m_ObjRef(aRef)
    {
       my_tokenize_function(token, s_toks);
    }

    test::test(const test& src)
       : m_ObjRef(src.m_ObjRef)
    {   
       /* What happens to s_toks; */
    }

If I do not copy s_toks, and s_toks is accessed from the new copied object, it is empty. 如果我不复制s_toks,并且从新复制的对象访问s_toks,则它为空。 What's the correct way to handle this? 处理这个问题的正确方法是什么?

A static data member is not bound to a single instance of your class. 静态数据成员未绑定到类的单个实例。 It exists for all instances, and to attempt to modify it in the class copy constructor makes little sense (unless you are using it to keep some kind of counter of instances). 它存在于所有实例中,并且尝试在类复制构造函数中修改它没有多大意义(除非您使用它来保留某种实例计数器)。 By the same token, it makes little sense to "initialize" it in any of the class constructors. 出于同样的原因,在任何类构造函数中“初始化”它都没有意义。

静态成员在类的所有实例之间共享,因此在构造函数中初始化它是没有意义的,也不会在复制构造函数中复制它。

Supporting other people's comments, this link provide a good explanation with examples: http://www.learncpp.com/cpp-tutorial/811-static-member-variables/ 支持其他人的评论,这个链接提供了一个很好的解释与例子: http//www.learncpp.com/cpp-tutorial/811-static-member-variables/

Unless you wish to access the static variable in all instances of the class, there is no need to declare it static. 除非您希望在类的所有实例中访问静态变量,否则无需将其声明为静态。

暂无
暂无

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

相关问题 当 push_back 到向量中时,我是否需要复制构造函数来修改静态成员? - Do I need a copy constructor to modify static member when push_back into a vector? 即使由于C ++中的RVO而未调用复制构造函数,也如何复制成员变量的值 - How values of member variables are getting copied even though copy constructor is not getting called due to RVO in C++ C ++如何确保副本构造函数不会修改原始对象的指针数据成员? - C++ How do I ensure a copy constructor does not modify an original object's pointer data member? 您如何在类的成员函数中调用复制构造函数? - How do you call the copy constructor within a member function of a class? 在C ++ 0x中,非静态数据成员初始值设定项是否会覆盖隐式复制构造函数? - In C++0x, do non-static data member initializers override the implicit copy constructor? 我需要复制构造函数吗? 以及如何使用复制构造函数将const对象复制到非const对象 - do i need copy constructor ? and how copy const object to non const object using copy constructor 如何从静态成员函数返回副本? - How do you return a copy from a static member function? 复制构造函数是否重新初始化文件成员 - Does copy constructor reinitialize the file member 为什么默认的复制构造函数可以接收未定义的静态成员变量? - Why default copy constructor can receive undefined static member variable? 如何用非平凡的构造函数初始化静态类成员? - How to initialize static class member with nontrivial constructor?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM