0
#include "Generator.h"
#include "Proxy.h"

Proxy::Proxy(int inputbits):Generator(inputbits)
{

}

Proxy::~Proxy()
{

}

Generator * Proxy::operator ->()
{
    if(counter<=10)
    return rPointer;
    else
    return 0;
}

//Proxy* Proxy::instance = 0;

Proxy* Proxy::getInstance()
{
    static Proxy* instance;
    return instance;
}

.

#ifndef PROXY_H
#define PROXY_H
#include "Generator.h"

class Proxy: private Generator
{
    public:
        ~Proxy();
        static Proxy* getInstance();
        Generator * operator ->();
    private:
        Proxy();
        Proxy(int);
        int bits;
        int counter;
        Generator * rPointer;
};

#endif // GENERATORPROXY_H

These are my code for a singleton, what I am trying to do is I'd like to pass some argument to the constructor after I make an object of Proxy in main function as Proxy::Proxy(int inputbits):Generator(inputbits) I was going to use getInstance function, but it didn't work. Please enlighten me if you have any idea. Thanks, what I am expecting to be able to do is, for example, in main function, Proxy px(3); <- I know this is not working, but I want to use something like this with any way.

Gideok Seong
  • 65
  • 3
  • 15

1 Answers1

1

You can do it with help of another function, that provide required bits on its call.

int get_bits() {
    return 3;
}

Proxy *Proxy::getInstance() {
    static Proxy* instance = new Proxy(get_bits());
    return instance;
}

Or do it in a single Proxy default constructor (it is better)

Proxy::Proxy():Generator(get_bits()) {
}
273K
  • 29,503
  • 10
  • 41
  • 64