-1

I want to share an instance of a class globally so that I can access a member function of the class from whatever source file I want. I'm sort of new to this, so I'm wondering about the best way to go about it. For the record, this is for a part of my program that will output text to a log window, so the input will be coming from pretty much every single class in my program. Here's some sample code:

MyClass.h

class MyClass
{

public:
  MyClass();

private:
  int start, end;
  void parse(std::string);
};

MyClass.cpp

#include "myclass.h"    
MyClass::MyClass()
{
}

void MyClass::parse(std::string){
// do some stuff here
}

OtherClass1.h

#include "myclass.h"    
class OtherClass1
{
  public:
    OtherClass1();

  private:
    MyClass someInstance1;
};

OtherClass1.cpp

#include "otherclass1.h"
OtherClass1::OtherClass1()
{
  someInstance1.parse("This is a string.")
}

Ok, great. But now I want to be able to use that same someInstance1 instance in yet ANOTHER class (i.e. OtherClass2). I don't want to have to create a MyClass someInstance2 in OtherClass2. How do I use the someInstance1 located in OtherClass1 that I have already created?

Here's what I have for OtherClass2. I can't use someInstance1 because it is private.

OtherClass2.h

#include "myclass.h"
#include "otherclass1.h" 
class OtherClass2
{
  public:
    OtherClass2();

  private:
    MyClass someInstance2;
};

OtherClass2.cpp

#include "otherclass2.h"
OtherClass2::OtherClass2()
{
  someInstance2.parse("This is a string.")
}

I only want one instance of MyClass throughout my entire program. Is there a better way to do this? I always read that having global anything is bad practice, but how else should I go about it?

EDIT: I wound up following the answer here: https://stackoverflow.com/a/2798624/2574608

earth
  • 883
  • 1
  • 8
  • 26
  • 3
    The simplest solution is something that most people don't like: Global variables. There are also other possibilities like for example shared pointers. – Some programmer dude Mar 20 '16 at 09:53
  • 1
    you probably want to use a [singleton pattern](https://en.wikipedia.org/wiki/Singleton_pattern). [See this answer](http://stackoverflow.com/a/1008289/264047) for code and a bunch of useful link to read – Alexander Malakhov Mar 20 '16 at 10:05
  • I wound up following the answer here: http://stackoverflow.com/a/2798624/2574608 – earth Apr 05 '16 at 00:48

2 Answers2

0

One option would be to make the class MyClass a singleton. Singleton is one of the creational Gang of Four patterns.

Here is a naive C++11 implementation:

MyClass.h

#include <string>

class MyClass
{
private:
    /* private constructor */
    MyClass();
    int start, end;

public:
    /* static method to get a reference to the MyClass instance */
    static MyClass& getInstance();       
    void parse(const std::string& str);
};

MyClass.cpp

#include "MyClass.h"
#include <iostream>

MyClass::MyClass() {}

MyClass& MyClass::getInstance() {
    static MyClass instance;
    return instance;
}

void MyClass::parse(const std::string& str) {
    // do some stuff here
    std::cout << __func__ << ": " << str << std::endl;
}

Now you can access the MyClass instance throughout your entire program using MyClass::getInstance():

#include "MyClass.h"

int main() {
    MyClass::getInstance().parse("foo");
}
sergej
  • 14,042
  • 6
  • 38
  • 75
  • One should not mention Singleton in an answer without adding a big warning and linking to something like http://stackoverflow.com/questions/137975/what-is-so-bad-about-singletons – Christian Hackl Mar 20 '16 at 11:42
-1

There are Singleton classes but they are like hidden global variables. I think you should just pass your needed class (in the constructor for instance) to all the class that need it.