We help IT Professionals succeed at work.
Get Started

object visibility / access issue

forums_mp
forums_mp asked
on
233 Views
Last Modified: 2010-04-01

Consider:

// test.h
#ifndef TEST_H
#define TEST_H

struct test_struct;

class test {
  test_struct msg;            // static instance msg
public:
  test();
  void get_data();
  void updateData(test_struct& test_);
};

#endif

// test.cpp
# include "test.h"
# include "test_struct.h"

test m_test;           // global instance

test::test() {}
void test::get_data() {}
void test::updateData(test_struct& test_) {}

// test_struct.h
#ifndef TEST_STRUCT_H
#define TEST_STRUCT_H

# include "test.h"

extern test m_test;
struct test_struct
{
  test_struct() {}
  void test_me() {
    m_test.updateData(*this);
  }
};

#endif


// main.cpp
# include <iostream>
# include <string>
# include "test.h"

using namespace std;
extern test m_test;
int main()
{
  m_test.get_data();
}


Within test.h, there's a forward declaration of test_struct.    A msg object of type test_struct is desired and instantiated privately within 'test'.   Trouble is:  There lies the problem.   It is desired to have a global instance of m_test.  That said, to alleaviate the issue with the msg object, I suspect one solution is to have the move/define the msg object globally within 'test.cpp'.  So now:

// test.cpp
# include "test.h"
# include "test_struct.h"

test m_test;                  // global instance
test_struct msg;            // static instance msg

test::test() {}
void test::get_data() {}
void test::updateData(test_struct& test_) {}

Alternatives are of course welcome.  Note:  m_test needs to be accessible within test_struct and the msg object should not be a pointer.  If there's a way to eliminate the global m_test by passing things around then by all means demonstrate.


Note: The purpose of a singleton is to make sure, that a specific object exists only once (or a specific number of times) in your whole system, hence I dont desire a singleton here.
Comment
Watch Question
This problem has been solved!
Unlock 2 Answers and 12 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE