如何将INT参数放入C 中的Sigleton构造器中

How to put int parameter into Sigleton constructor in C++

本文关键字:中的 Sigleton 构造器 INT 参数      更新时间:2023-10-16
#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

这些是我的单身人士的代码,我想做的是我想在Main函数中作为Proxy::Proxy(int inputbits):Generator(inputbits)的代理对象将某些论点传递给构造函数,我将使用GetInstance函数,但是它不起作用。如果您有任何想法,请启发我。谢谢,我期望能够做的就是例如,在主要功能中,代理PX(3(;&lt; - 我知道这是不起作用的,但是我想以任何方式使用类似的东西。

您可以在另一个功能的帮助下完成,该功能在其呼叫上提供所需的位。

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

或在单个Proxy默认构造函数中进行(更好(

Proxy::Proxy():Generator(get_bits()) {
}