#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    8
    Rep Power
    0

    Making consistency of data member in the whole programme without using static keyword


    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

    Code:
    #pragma once
        
        class Jointdetails
        {
        public:
        	Jointdetails(void);
        	~Jointdetails(void);
        	bool check;
        
        
        };
    Inside jointdeatils.cpp

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

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

    Code:
      #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

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

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

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

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



    Code:
    #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 ;
        }
  2. #2
  3. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,074
    Rep Power
    1802
    You could encapsulate check in a class of its own using the singleton pattern. But it is a sledgehammer to crack a nut, and hardly worth it; what is your objection to the use of static?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    8
    Rep Power
    0
    I want to use this concept in my project and i dont want to use memory till the end of my programme . This is just a sample programme to put my thought in this forum .

IMN logo majestic logo threadwatch logo seochat tools logo