Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

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


Inside jointdeatils.cpp

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


Inside analyzer.h

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


Inside analyzer.cpp

C++
#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;
    	
    
    }


Inside Test1.h

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


Inside Test1.cpp

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


Inside Test2.h

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


Inside Test2.cpp

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


Inside main.cpp



C++
#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 ;
    }
Posted
Updated 2-Sep-12 9:00am
v2
Comments
pasztorpisti 2-Sep-12 16:24pm    
Why do you want to avoid the static keyword? If you want to store only a single bool value inside your program than the use of static is reasonable here.

1 solution

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:
C++
class Jointdetails
{
public:
    Jointdetails();
    ~Jointdetails();
    bool IsChecked() const
    {
        return Checked;
    }
    void SetChecked(bool checked)
    {
        Checked = checked;
    }
private:
    bool Checked;
};

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:
C++
class Jointdetails
{
public:
    static bool IsChecked();
    static void SetChecked(bool checked);
};

jointdetails.cpp:
C++
#include "Jointdetails.h"

static bool g_Checked = false;    

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

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

This way you can call Jointdetails::IsChecked() anywhere in you application.
 
Share this answer
 
v2
Comments
vikuseth 3-Sep-12 1:03am    
Thanks a lot for giving some nice suggestions on design part . As i mentioned above i don't want to use the static keyword .
Is there any other way to solve this without using the static keyword ?
Thanks in advance .
pasztorpisti 3-Sep-12 2:38am    
jointdetails.h:

bool IsChecked();

jointdetails.cpp:

#include "Jointdetails.h"

namespace {
bool g_Checked = false;
}

bool IsChecked()
{
return g_Checked;
}

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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900