Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
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;
    
    
    };
 
Inside jointdeatils.cpp
 
#include "Jointdetails.h"
    
    Jointdetails::Jointdetails(void)
    {
    	check = false ;
    }
    
    Jointdetails::~Jointdetails(void)
    {
    }
 
Inside analyzer.h
 
 #pragma once
    #include "Jointdetails.h"
    class Analyzer
    {
    public:
    	Analyzer(void);
    	Jointdetails* GetJointDetails();
    	Jointdetails* m_ptheCTJointDetails;
    	~Analyzer(void);
    };
 
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;
    	
    
    }
 
Inside Test1.h
 
  #pragma once
    #include "Analyzer.h"
    class Tst1
    {
    public:
    	Tst1(void);
    	Analyzer *analyzer1 ;
    public:
    	~Tst1(void);
    };
 
Inside Test1.cpp
 
#include "Tst1.h"
    
    Tst1::Tst1(void)
    {
    	analyzer1 = new Analyzer ;
    }
    
    Tst1::~Tst1(void)
    {
    }
 
Inside Test2.h
 
#pragma once
    #include "Analyzer.h"
    class Test2
    {
    public:
    	Test2(void);
    	Analyzer *analyzer2 ;
    public:
    	~Test2(void);
    };
 
Inside Test2.cpp
 
#include "Test2.h"
    
    Test2::Test2(void)
    {
    	analyzer2 = new Analyzer ;
    }
    
    Test2::~Test2(void)
    {
    }
 
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 ;
    }
Posted 2-Sep-12 9:57am
Edited 2-Sep-12 10:00am
v2
Comments
pasztorpisti at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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;
};
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:
#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.
  Permalink  
v2
Comments
vikuseth at 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 at 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)

  Print Answers RSS
0 OriginalGriff 495
1 Maciej Los 340
2 Richard MacCutchan 265
3 BillWoodruff 225
4 Mathew Soji 200
0 OriginalGriff 8,804
1 Sergey Alexandrovich Kryukov 7,457
2 DamithSL 5,689
3 Maciej Los 5,279
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web02 | 2.8.1411028.1 | Last Updated 2 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100