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

1
2
3
4
5
6
7
8
9
10
11
#pragma once
    
    class Jointdetails
    {
    public:
    	Jointdetails(void);
    	~Jointdetails(void);
    	bool check;
    
    
    };


Inside jointdeatils.cpp

1
2
3
4
5
6
7
8
9
10
#include "Jointdetails.h"
    
    Jointdetails::Jointdetails(void)
    {
    	check = false ;
    }
    
    Jointdetails::~Jointdetails(void)
    {
    }


Inside analyzer.h

1
2
3
4
5
6
7
8
9
10
#pragma once
    #include "Jointdetails.h"
    class Analyzer
    {
    public:
    	Analyzer(void);
    	Jointdetails* GetJointDetails();
    	Jointdetails* m_ptheCTJointDetails;
    	~Analyzer(void);
    };


Inside analyzer.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#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

1
2
3
4
5
6
7
8
9
10
#pragma once
    #include "Analyzer.h"
    class Tst1
    {
    public:
    	Tst1(void);
    	Analyzer *analyzer1 ;
    public:
    	~Tst1(void);
    };


Inside Test1.cpp

1
2
3
4
5
6
7
8
9
10
#include "Tst1.h"
    
    Tst1::Tst1(void)
    {
    	analyzer1 = new Analyzer ;
    }
    
    Tst1::~Tst1(void)
    {
    }

Inside Test2.h

1
2
3
4
5
6
7
8
9
10
#pragma once
    #include "Analyzer.h"
    class Test2
    {
    public:
    	Test2(void);
    	Analyzer *analyzer2 ;
    public:
    	~Test2(void);
    };

Inside Test2.cpp

1
2
3
4
5
6
7
8
9
10
#include "Test2.h"
    
    Test2::Test2(void)
    {
    	analyzer2 = new Analyzer ;
    }
    
    Test2::~Test2(void)
    {
    }

Inside main.cpp


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    #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 ;
    }



Last edited on
Use static. That's what it's there for.
But i dont want to consume memory till the end of my programme .
The only solution I can see is to keep one static pointer and one static instance counter. Both start out at zero. When the first instance is constructed, the pointer is initialized to a dynamically allocated object. Once the last instance is destructed, the pointer is destructed and reset. This approach has three problems:
1. It's complex.
2. It makes no sense to do this unless your static object is big or it has special semantics.
3. It's not thread-safe.
@helios Are you pointing towards singleton pattern ?
I don't know. I don't think so.
Topic archived. No new replies allowed.