在C ++中调用单例对象的正确方法

时间:2015-04-22 23:40:42

标签: c++ singleton

我根据匹配posted here构建了一个单例类。

我用getMessage()函数扩展了它,它会检索内部字典消息 - 字典只需要在整个应用程序上加载一次,这就是单例的原因。

我的代码:

Singleton.hpp

class Singleton {

public:

    static Singleton& getInstance();
    std::string getMessage(std::string code);

private:

    Singleton() {}; 
    Singleton(Singleton const&) = delete;
    void operator=(Singleton const&) = delete;
};

Singleton.cpp

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


std::string Singleton::getMessage(std::string code)
{
    /// Do something
    return "Code example.";
}

主要代码:

的main.cpp

int main()
{
        Singleton* my_singleton;
        my_singleton = Singleton::getInstance(); **<-- ERROR HERE**

        cout << my_singleton->getMessage("a"); << endl

}

主要给我一个错误:Cannot convert 'Singleton' to 'Singleton*' in assignment

&#34;实例化&#34;的正确方法是什么?单例并使用getMessage函数。

非常感谢帮助...

2 个答案:

答案 0 :(得分:3)

你怎么称呼这个函数:

Singleton::getInstance().getMessage("a");

而不是将其分配给变量。

答案 1 :(得分:0)

您希望存储对单例的引用,而不是指针。

    <div class="modal-header">
    <a class="close" data-dismiss="modal">&times;</a>
    <?php if ( $modalToOpen == "1") { ?>
        <h4>Aplicar abono a la cuenta #<?php echo $cardInformation->CardNumber ?></h4>
    <?php } ?>
    <?php if ($modalToOpen == "2") { ?>
        <h4>Aplicar cargo a la cuenta #<?php echo $cardInformation->CardNumber ?></h4>
    <?php } ?>

</div>

<div class="modal-body">
    <p>Saldo Actual: <?php echo $lastBalance ?></p>
    <?php echo $modalToOpen ; if($modalToOpen == 1){ ?>
    <?php $form = $this->beginWidget('bootstrap.widgets.TbActiveForm', array('id' => 'inlineForm', 'type' => 'inline', 'htmlOptions' => array('class' => 'well'))); ?>
    <?php echo $form->TextField($auxModel, 'Payment', array('size' => 8, 'maxlength' => 8, 'placeholder' => "$. Abono", 'class' => 'span1')); ?>
    <?php echo $form->TextField($auxModel, 'Coment', array('size' => 250, 'maxlength' => 250, 'placeholder' => "Comentario", 'class' => 'span3')); ?>
    <?php
    $this->widget(
            'bootstrap.widgets.TbButton', array(
            'buttonType' => 'ajaxButton',
            'type' => 'primary',
            'label' => 'Guardar',
            'url' => Yii::app()->createUrl('BusinessTargetCollectionCard/ApplyPayment'),
            'htmlOptions' => array('onclick' => 'showWait();','id' =>'payment-btn'.  uniqid()),
            'ajaxOptions' => array(
                'type' => 'POST',
                'dataType' => 'json',
                'data' => array(
                    'cardNumber' => $cardInformation->CardNumber,
                    'lastBalance' => $lastBalance,
                    'coment' => 'js:$("#BusinessTargetCollectionCardMovement_Coment").val()',
                    'payment' => 'js:$("#BusinessTargetCollectionCardMovement_Payment").val()',
                    'businessTargetCollectionCardId' => $cardInformation->Id,
                    'collectorId' => $cardInformation->CollectorId),
                'success' => 'js:function(data){
                        alert(data.message);
                        if(data.status == "OK"){
                            $("#PaymentModal").modal("hide");
                            hideWait();
                        }
                    }'
            ),
        )
    );
    $this->endWidget(); ?>
    <?php } ?>
    <?php if($modalToOpen == 2){ ?>
    <?php $form = $this->beginWidget('bootstrap.widgets.TbActiveForm', array('id' => 'inlineForm', 'type' => 'inline', 'htmlOptions' => array('class' => 'well'))); ?>
    <?php echo $form->TextField($auxModel, 'BalanceAfter', array('size' => 8, 'maxlength' => 8, 'placeholder' => "Cantidad a aplicar", 'class' => 'span1')); ?>
    <?php
    $this->widget(
            'bootstrap.widgets.TbButton', array(
                'buttonType' => 'ajaxButton',
                'type' => 'primary',
                'label' => 'Guardar',
                'url' => Yii::app()->createUrl('BusinessTargetCollectionCard/ApplyCharge'),
                'htmlOptions' => array('onclick' => 'showWait();', 'id' => 'charge-btn' . uniqid()),
                'ajaxOptions' => array(
                    'type' => 'POST',
                    'dataType' => 'json',
                    'data' => array(
                        'cardNumber' => $cardInformation->CardNumber,
                        'charge' => 'js:$("#BusinessTargetCollectionCardMovement_BalanceAfter").val()'),
                    'success' => 'js:function(data){
                            alert(data.message);
                            if(data.status == "OK"){
                                $("#myModal").modal("hide");
                                hideWait();
                            }
                        }'
                ),
            )
    ); 
    $this->endWidget();
    ?>
    <?php } ?>

</div>