在不使用static关键字的情况下使整个程序中的数据成员保持一致 [英] making consistency of data member in the whole programme without using static keyword

查看:98
本文介绍了在不使用static关键字的情况下使整个程序中的数据成员保持一致的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的程序中,我使用一个名为check的布尔变量,该变量由Tst1和Test2的两个对象在主函数内部访问.但是检查变量的值未在程序中保留.我们可以使用static,但我想知道其他方法..有人可以给我一些提示吗?
在此先感谢.

内部jointdeatils.h

In the below programme i use one boolean variable named check , which is being accessed inside main function by two objects of Tst1 and Test2 . But the value of check variable is not maintained in the programme . we can use static but i want to know some alternative way ..could anyone give me some hints on it ?
Thanks in advance .

Inside jointdeatils.h

#pragma once
    
    class Jointdetails
    {
    public:
    	Jointdetails(void);
    	~Jointdetails(void);
    	bool check;
    
    
    };



内部jointdeatils.cpp



Inside jointdeatils.cpp

#include "Jointdetails.h"
    
    Jointdetails::Jointdetails(void)
    {
    	check = false ;
    }
    
    Jointdetails::~Jointdetails(void)
    {
    }



内分析器.h



Inside analyzer.h

#pragma once
   #include "Jointdetails.h"
   class Analyzer
   {
   public:
       Analyzer(void);
       Jointdetails* GetJointDetails();
       Jointdetails* m_ptheCTJointDetails;
       ~Analyzer(void);
   };



内分析器.cpp



Inside 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;
    	
    
    }



内部Test1.h



Inside Test1.h

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



内部Test1.cpp



Inside Test1.cpp

#include "Tst1.h"
    
    Tst1::Tst1(void)
    {
    	analyzer1 = new Analyzer ;
    }
    
    Tst1::~Tst1(void)
    {
    }



内部Test2.h



Inside Test2.h

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



内部Test2.cpp



Inside Test2.cpp

#include "Test2.h"
    
    Test2::Test2(void)
    {
    	analyzer2 = new Analyzer ;
    }
    
    Test2::~Test2(void)
    {
    }



内部main.cpp





Inside 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 ;
    }

推荐答案

1.在C ++中,如果您的方法没有参数列表,则不必写void代替参数列表.在普通C语言中,您必须执行此操作,因为如果不将void放在此处,则没有任何参数的函数就是vararg函数,但是在C ++中,这只是个杂音,C ++中的空参数列表不是vararg!
2.在C ++中,最好将所有成员变量保持私有状态,并为它们设置公共或私有set/get方法,如下所示:
1. In C++ when your method doesn''t have a parameter list you don''t have to write void in place of the parameter list. In plain C you had to do this because there a function without any parameters is a vararg function if you don''t put the void there but in C++ its just noise, an empty parameter list in C++ isn''t vararg!
2. In C++ its better to keep all member variables private and make public or private set/get methods for them like this:
class Jointdetails
{
public:
    Jointdetails();
    ~Jointdetails();
    bool IsChecked() const
    {
        return Checked;
    }
    void SetChecked(bool checked)
    {
        Checked = checked;
    }
private:
    bool Checked;
};


另一个好的做法是给名词成员变量名称和动词方法名称,如SetXXX(),GetXXX(),DoThis(),DoThat(),唯一的例外是一些以"Is"开头的布尔型吸气剂方法,例如我的示例.
3.如果在每个实例中都需要Checked变量,那么您已经可以了,如果只需要在应用程序中一次使用此Checked变量,则可以将其设置为静态,并可以这样进行:
jointdetails.h:


Another good practice is to give noun member variable names and verb method names like SetXXX(), GetXXX(), DoThis(), DoThat(), the only exception are some bool getter methods that start with "Is" like in my example.
3. If you need the Checked variable in every instances then you are already OK, if you need this Checked variable only onece in your application then you can make it static and you can do it like this:
jointdetails.h:

class Jointdetails
{
public:
    static bool IsChecked();
    static void SetChecked(bool checked);
};


jointdetails.cpp:


jointdetails.cpp:

#include "Jointdetails.h"

static bool g_Checked = false;    

bool Jointdetails::IsChecked()
{
    return g_Checked;
}

void Jointdetails::SetChecked(bool checked)
{
    g_Checked = checked;
}


这样,您可以在应用程序中的任何地方调用Jointdetails::IsChecked().


This way you can call Jointdetails::IsChecked() anywhere in you application.


这篇关于在不使用static关键字的情况下使整个程序中的数据成员保持一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆