C++ 在不使用静态关键字的情况下实现整个程序中数据成员的一致性

C++ 在不使用静态关键字的情况下实现整个程序中数据成员的一致性,c++,c++11,C++,C++11,在下面的程序中,我使用了一个名为check的布尔变量,它在主函数中由两个对象Tst1和Test2访问。但是check变量的值没有保留在程序中。我们可以使用static,但我想知道一些替代方法。有人能给我一些提示吗? 提前谢谢 内接缝接缝接缝 #pragma once class Jointdetails { public: Jointdetails(void); ~Jointdetails(void); bool check; }; 内部jointdeatils.

在下面的程序中,我使用了一个名为check的布尔变量,它在主函数中由两个对象Tst1和Test2访问。但是check变量的值没有保留在程序中。我们可以使用static,但我想知道一些替代方法。有人能给我一些提示吗? 提前谢谢

内接缝接缝接缝

#pragma once

class Jointdetails
{
public:
    Jointdetails(void);
    ~Jointdetails(void);
    bool check;


};
内部jointdeatils.cpp

#include "Jointdetails.h"

Jointdetails::Jointdetails(void)
{
    check = false ;
}

Jointdetails::~Jointdetails(void)
{
}
内部分析器.h

#pragma once
#include "Jointdetails.h"
class Analyzer
{
public:
    Analyzer(void);
    Jointdetails* GetJointDetails();
    Jointdetails* m_ptheCTJointDetails;
    ~Analyzer(void);
};
内部分析器.cpp

#include "Analyzer.h"
#include "stddef.h"
Analyzer::Analyzer(void)
{
    m_ptheCTJointDetails = new Jointdetails();

}

Analyzer::~Analyzer(void)
{
}
Jointdetails* Analyzer::GetJointDetails()
{

    if(m_ptheCTJointDetails) 
        return m_ptheCTJointDetails;
    else
        return NULL;


}
Analyzer::Analyzer(void)
{
    m_ptheCTJointDetails = Jointdetails::getInstance();
}
内部测试1.h

#pragma once
#include "Analyzer.h"
class Tst1
{
public:
    Tst1(void);
    Analyzer *analyzer1 ;
public:
    ~Tst1(void);
};
内部Test1.cpp

#include "Tst1.h"

Tst1::Tst1(void)
{
    analyzer1 = new Analyzer ;
}

Tst1::~Tst1(void)
{
}
内部测试2.h

#pragma once
#include "Analyzer.h"
class Test2
{
public:
    Test2(void);
    Analyzer *analyzer2 ;
public:
    ~Test2(void);
};
内部Test2.cpp

#include "Test2.h"

Test2::Test2(void)
{
    analyzer2 = new Analyzer ;
}

Test2::~Test2(void)
{
}
内部main.cpp

#include "Test2.h"
#include "Tst1.h"
#include "stdio.h"

int main()
{
    Tst1 *test1 = new Tst1 ; //check = false
    Test2 *test2 = new Test2 ; //check = false
    test1->analyzer1->GetJointDetails()->check = true ;
    if(test2->analyzer2->GetJointDetails()->check )
        printf("Check value is changed");
    else
        printf("Check value is not changed");
        return 0 ;
}

您要么必须使check static,要么将JointDetails设置为singleton(它也使用static关键字)

如果将check设置为static,则表示JointDetails的所有实例都具有相同的检查

如果将JointDetails设置为单例,则表示对JointDetails对象的每个引用都是同一个对象,因此Tst1和Test2都将具有指向同一对象的指针

我认为后者正是你想要的:

联合细节

#pragma once

class Jointdetails
{
public:
    ~Jointdetails(void);
    bool check;

    static Jointdetails* getInstance();
private:
    Jointdetails(void);
};
Jointdetails.cpp

#include "Jointdetails.h"

Jointdetails::Jointdetails(void)
{
    check = false ;
}

Jointdetails::~Jointdetails(void)
{
}

Jointdetails* Jointdetails::getInstance() {
    static Jointdetails s_instance;
    return &s_instance;
}
 std::shared_ptr<JointDetails> JointDetails::Get()
 {
    static std::weak_ptr<JointDetails> s_trackInstance;
    if(s_trackInstance.expired())
    {
       auto instance = std::make_shared<JointDetails>();
       s_trackInstance = instance;
       return instance;
    }

    return s_trackInstance.lock();
 }
Analyzer.cpp

#include "Analyzer.h"
#include "stddef.h"
Analyzer::Analyzer(void)
{
    m_ptheCTJointDetails = new Jointdetails();

}

Analyzer::~Analyzer(void)
{
}
Jointdetails* Analyzer::GetJointDetails()
{

    if(m_ptheCTJointDetails) 
        return m_ptheCTJointDetails;
    else
        return NULL;


}
Analyzer::Analyzer(void)
{
    m_ptheCTJointDetails = Jointdetails::getInstance();
}

您要么必须使check static,要么将JointDetails设置为singleton(它也使用static关键字)

如果将check设置为static,则表示JointDetails的所有实例都具有相同的检查

如果将JointDetails设置为单例,则表示对JointDetails对象的每个引用都是同一个对象,因此Tst1和Test2都将具有指向同一对象的指针

我认为后者正是你想要的:

联合细节

#pragma once

class Jointdetails
{
public:
    ~Jointdetails(void);
    bool check;

    static Jointdetails* getInstance();
private:
    Jointdetails(void);
};
Jointdetails.cpp

#include "Jointdetails.h"

Jointdetails::Jointdetails(void)
{
    check = false ;
}

Jointdetails::~Jointdetails(void)
{
}

Jointdetails* Jointdetails::getInstance() {
    static Jointdetails s_instance;
    return &s_instance;
}
 std::shared_ptr<JointDetails> JointDetails::Get()
 {
    static std::weak_ptr<JointDetails> s_trackInstance;
    if(s_trackInstance.expired())
    {
       auto instance = std::make_shared<JointDetails>();
       s_trackInstance = instance;
       return instance;
    }

    return s_trackInstance.lock();
 }
Analyzer.cpp

#include "Analyzer.h"
#include "stddef.h"
Analyzer::Analyzer(void)
{
    m_ptheCTJointDetails = new Jointdetails();

}

Analyzer::~Analyzer(void)
{
}
Jointdetails* Analyzer::GetJointDetails()
{

    if(m_ptheCTJointDetails) 
        return m_ptheCTJointDetails;
    else
        return NULL;


}
Analyzer::Analyzer(void)
{
    m_ptheCTJointDetails = Jointdetails::getInstance();
}
0)您不必经常使用
new

1) 可以将关节细节作为构造函数中的参数来构造对象

举例说明:

class Tst1 {
public:
  Tst1(Jointdetails& pJointdetails) : analyzer1(pJointdetails) {
  }

  Analyzer analyzer1;
public:
  ~Tst1(void);
};

int RunProgram(Jointdetails& pJointdetails) {
  Tst1(pJointdetails);
  ...
}

int main() {
  Jointdetails jointdetails;
  const int result(RunProgram(jointdetails));
  return result;
}
0)您不必经常使用
new

1) 可以将关节细节作为构造函数中的参数来构造对象

举例说明:

class Tst1 {
public:
  Tst1(Jointdetails& pJointdetails) : analyzer1(pJointdetails) {
  }

  Analyzer analyzer1;
public:
  ~Tst1(void);
};

int RunProgram(Jointdetails& pJointdetails) {
  Tst1(pJointdetails);
  ...
}

int main() {
  Jointdetails jointdetails;
  const int result(RunProgram(jointdetails));
  return result;
}

只有两种方法可以做到这一点:

  • 使用
    静态
    存储数据
  • 将自动或动态存储数据作为参数传递给目标函数/CTOR
  • 方法#1更方便,因为您可以直接从任何函数访问此类数据。但它应该被认为是糟糕的设计,因为它几乎和邪恶的全局变量一样

    方法#2更为正确(参见示例的答案),但可能有点令人恼火-您需要将所需数据作为参数传递给每个所需函数和/或将数据存储为类数据成员。在许多类/嵌套调用的情况下,这不是一项令人愉快的工作

    不过,如果你不关心方法的缺点,则1考虑<强>单按需<强>概念。它允许以更动态的方式使用静态数据——按需创建,由多个用户共享访问,并在无人使用时销毁。参见示例(为了简洁起见,跳过了几个细节,包括etc):

    联合细节

     class JointDetails
     {
        // Actual class definition
        // ...
     public:
    
        // Function accessing to JointDetails instance
        static std::shared_ptr<JointDetails> Get();
     };
    
    类连接细节
    {
    //实际类定义
    // ...
    公众:
    //函数访问JointDetails实例
    静态std::shared_ptr Get();
    };
    
    JointDetails.cpp

    #include "Jointdetails.h"
    
    Jointdetails::Jointdetails(void)
    {
        check = false ;
    }
    
    Jointdetails::~Jointdetails(void)
    {
    }
    
    Jointdetails* Jointdetails::getInstance() {
        static Jointdetails s_instance;
        return &s_instance;
    }
    
     std::shared_ptr<JointDetails> JointDetails::Get()
     {
        static std::weak_ptr<JointDetails> s_trackInstance;
        if(s_trackInstance.expired())
        {
           auto instance = std::make_shared<JointDetails>();
           s_trackInstance = instance;
           return instance;
        }
    
        return s_trackInstance.lock();
     }
    
    std::shared\ptr JointDetails::Get()
    {
    静态std::弱_ptr s_trackInstance;
    如果(s_trackInstance.expired())
    {
    auto instance=std::make_shared();
    s_trackInstance=实例;
    返回实例;
    }
    返回s_trackInstance.lock();
    }
    
    分析仪.h

    // All instances of Analyzer share the same instance of JointDetails.
    // But JointDetails instance is created dynamically only when first instance of
    // Analyzer is created and destoyed when the last instance of Analyzer is destroyed.
    class Analyzer
    {
       std::shared_ptr<JointDetails> m_details;
    public:
       Analyzer(): m_details(JointDetails::Get()) {}
       const JointDetails& GetDetails() const { return *m_details; }
    };
    
    //Analyzer的所有实例共享同一个JointDetails实例。
    //但JointDetails实例仅在
    //当Analyzer的最后一个实例被销毁时,Analyzer将被创建和销毁。
    类分析器
    {
    std::共享的ptr m_详细信息;
    公众:
    Analyzer():m_详细信息(JointDetails::Get()){}
    const JointDetails&GetDetails()const{return*m_details;}
    };
    
    只有两种可能的方法:

  • 使用
    静态
    存储数据
  • 将自动或动态存储数据作为参数传递给目标函数/CTOR
  • 方法#1更方便,因为您可以直接从任何函数访问此类数据。但它应该被认为是糟糕的设计,因为它几乎和邪恶的全局变量一样

    方法#2更为正确(参见示例的答案),但可能有点令人恼火-您需要将所需数据作为参数传递给每个所需函数和/或将数据存储为类数据成员。在许多类/嵌套调用的情况下,这不是一项令人愉快的工作

    不过,如果你不关心方法的缺点,则1考虑<强>单按需<强>概念。它允许以更动态的方式使用静态数据——按需创建,由多个用户共享访问,并在无人使用时销毁。参见示例(为了简洁起见,跳过了几个细节,包括etc):

    联合细节

     class JointDetails
     {
        // Actual class definition
        // ...
     public:
    
        // Function accessing to JointDetails instance
        static std::shared_ptr<JointDetails> Get();
     };
    
    类连接细节
    {
    //实际类定义
    // ...
    公众:
    //函数访问JointDetails实例
    静态std::shared_ptr Get();
    };
    
    JointDetails.cpp

    #include "Jointdetails.h"
    
    Jointdetails::Jointdetails(void)
    {
        check = false ;
    }
    
    Jointdetails::~Jointdetails(void)
    {
    }
    
    Jointdetails* Jointdetails::getInstance() {
        static Jointdetails s_instance;
        return &s_instance;
    }
    
     std::shared_ptr<JointDetails> JointDetails::Get()
     {
        static std::weak_ptr<JointDetails> s_trackInstance;
        if(s_trackInstance.expired())
        {
           auto instance = std::make_shared<JointDetails>();
           s_trackInstance = instance;
           return instance;
        }
    
        return s_trackInstance.lock();
     }
    
    std::shared\ptr JointDetails::Get()
    {
    静态std::弱_ptr s_trackInstance;
    如果(s_trackInstance.expired())
    {
    auto instance=std::make_shared();
    s_trackInstance=实例;
    返回实例;
    }
    返回s_trackInstance.lock();
    }
    
    分析仪.h

    // All instances of Analyzer share the same instance of JointDetails.
    // But JointDetails instance is created dynamically only when first instance of
    // Analyzer is created and destoyed when the last instance of Analyzer is destroyed.
    class Analyzer
    {
       std::shared_ptr<JointDetails> m_details;
    public:
       Analyzer(): m_details(JointDetails::Get()) {}
       const JointDetails& GetDetails() const { return *m_details; }
    };
    
    //Analyzer的所有实例共享同一个JointDetails实例。
    //但JointDetails实例仅在
    //当Analyzer的最后一个实例被销毁时,Analyzer将被创建和销毁。
    类分析器
    {
    std::共享的ptr m_详细信息;
    公众:
    Analyzer():m_详细信息(JointDetails::Get()){}
    const JointDetails&GetDetails()const{return*m_details;}
    };
    
    所以。。。您正在寻找设计气味吗?
    check
    是一个实例变量,因此每个ob