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

You could new a JointDetails object in main before you make your other classes, then add a constructor Analyzer that takes a pointer to a JointDetails and constructors to Test1 and Test2 that also take pointers to JointDetails. The Test1 and Test2 constructors would pass the pointers to JointDetails that they receive to the constructor of Analyzer. This way both would be pointing to the same JointDetails and, therefore, the same Check. So, you could have:

class Analyzer
{
public :
    Analyzer( JointDetails* jd ) : m_ptheCTJointDetails( jd ) { assert( jd != NULL; }

    /* The rest of the class goes here */
};

class Test1
{
public :
    Test1( JointDetails* jd ) : m_Analyzer( new Analyzer( jd ) ) {}

    /* The rest of the class goes here */
};

class Test2
{
public :
    Test2( JointDetails* jd ) : m_Analyzer( new Analyzer( jd ) ) {}

    /* The rest of the class goes here */
};

Then in main:

int main()
{
    JointDetails* jd = new JointDetails;
    Test1 t1( jd );
    Test2 t2( jd );

    t1->m_Analyzer->GetJointDetails()->Check = true;

    bool b = t2->m_Analyzer-GetJointDetails();

    std::cout << "Value is " << ( b ? "un" : "" ) << "changed" << std::endl;

    return 0;
}

This is obviously only one way, there are many others. This way is robust to you making your classes a bit safer ( making variables private with accessors etc. ).

Have fun

On another note, your destructors should delete the pointer that the object owns. You could be leaking memory otherwise.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.