0

This code:

#include "stdafx.h"
#include <iostream>
using namespace std;
class singleTon
{
    float testVal;
public:
    singleTon()
    {
        cout << "Singleton created\n";
        testVal = 0.0;
    }
    ~singleTon()
    {
        cout << "Singleton deleted\n";
    }
    void setTest(float x)
    {
        testVal = x;
    }
    float getTest()
    {
        return testVal;
    }

};

class myClass
{
    singleTon s;

public:
    myClass()
    {
        cout << "myClass created\n";
    }
    ~myClass()
    {
        cout << "myClass deleted\n";
    }

    singleTon getSingleTon()
    {
        return s;
    }

};

int _tmain(int argc, _TCHAR* argv[])
{
    myClass m;
    m.getSingleTon().setTest(100);
    cout << "\ngetting" << m.getSingleTon().getTest();
    cout << "\nSetting:";
    m.getSingleTon().setTest(200);
    cout << "\ngetting" << m.getSingleTon().getTest();

    getchar();
    return 0;
}

After the first setTest():

m.getSingleTon().setTest(100);      

The singleton class's destructor is getting called.

But, why?

My understanding was that it will still hold the singleton instance in class myClass. I know, if I use heap allocation, it may work. But what is wrong with this style? Where is the issue?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Nidhin KR
  • 45
  • 9
  • 1
    You are returning by value, which means you are making a copy **every time**. You'll want to return by reference. – Ben Steffan May 17 '17 at 16:01
  • Make the copy constructor of your singleton deleted, you'll see your error. – Guillaume Racicot May 17 '17 at 16:02
  • 2
    You seem to misunderstand how a [real singleton pattern](http://stackoverflow.com/questions/1008019/c-singleton-design-pattern) should be implemented – EdChum May 17 '17 at 16:03

2 Answers2

1
singleTon getSingleTon()
{
    return s;
}

The return type of this function is neither a reference nor a pointer, so you will return s by value, which means you're making a copy of it and returning a copy. However, if you want to modify the data members of the singleTon stored in myClass, you'll want to return by reference instead (returning by pointer is also possible, but usually discouraged if its not necessary). You code would have to change to look something like this:

singleTon& getSingleTon()
{
    return s;
}
Ben Steffan
  • 1,095
  • 12
  • 16
  • Yes, but... what OP has is not a singleton. This needs to be addressed first. – user4581301 May 17 '17 at 18:16
  • @user4581301 It certainly needs to be addressed, but it is not what OP asked, and the reason his code isn't working is not that he didn't understand the concept of a singleton correctly, but that he actually made a mistake which could have happened in any non-singleton related code as well, which is what I'm trying to address. Educating OP about singletons should not, to my mind, be the main point of an answer to this question. – Ben Steffan May 17 '17 at 20:03
0

When you call getSinglton(), you are actually getting a copy of the singleton inside myClass.

AppWriter
  • 247
  • 2
  • 13