Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Developer
javayoung
Posts: 313
Registered: ‎05-31-2010
My Device: Alpha 10, Bold 9900, Storm 9530, Tour 9630, Curve 9320, Curve 8900
My Carrier: All
Accepted Solution

How to implement Singleton class by Cascade C++

[ Edited ]

I have tried to use standard C++ to implement Singleton class. It can be properly compiled though, it always has a link error "Description    Resource Path Location Type cannot declare member function 'static globalsettings* globalsettings::instance()' to have static linkage [-fpermissive] C/C++ Problem"

 

Can someone have a look of my source code to find out what is wrong of it? Thank you.

 

/*
 * globalsettings.h
 */

#ifndef GLOBALSETTINGS_H_
#define GLOBALSETTINGS_H_

class Globalsettings {
public:
    Globalsettings();
    virtual ~Globalsettings();

    int get_value();
    void set_value(int v);

    static Globalsettings &instance();

private:
    int m_value;

    static Globalsettings *s_instance;
};

#endif

 Wrong cpp:

  

/*
 * globalsettings.cpp
 *
 */

#include "globalsettings.h"

Globalsettings*  Globalsettings::s_instance = 0;

Globalsettings::Globalsettings() {
	s_instance = 0;
	m_value = 0;
}

Globalsettings::~Globalsettings() {
	s_instance = 0;
}

static Globalsettings& Globalsettings::instance() {
	if (!s_instance)
	  s_instance = new Globalsettings();
	return *s_instance;
}

int Globalsettings::get_value()
{
	return m_value;
}
void Globalsettings::set_value(int v)
{
	m_value = v;
}

 

Revised cpp (it works)

 

/*
 * globalsettings.cpp
 *
 */

#include "globalsettings.h"

Globalsettings*  Globalsettings::s_instance = 0;

Globalsettings::Globalsettings() {
	s_instance = 0;
	m_value = 0;
}

Globalsettings::~Globalsettings() {
	s_instance = 0;
}

Globalsettings& Globalsettings::instance() {
	if (!s_instance)
	  s_instance = new Globalsettings();
	return *s_instance;
}

int Globalsettings::get_value()
{
	return m_value;
}
void Globalsettings::set_value(int v)
{
	m_value = v;
}

 




p(^_^)q
Good good study, day day up
Please use plain text.
Developer
Zmey
Posts: 1,510
Registered: ‎12-18-2012
My Device: PlayBook, Z10, DAC

Re: How to implement Singleton class by Cascade C++

[ Edited ]

 

UPD:

1)

static globalsettings *s_instance;

This should be in private section.

 

2)

static globalsettings *instance();

 This should be public so user can call it.

3)

Remove 'static' keyword from .cpp file.

 

 

 

 

Even easier approach with lazy initialization:

 

1) in .hpp:

 

static MyClass *sharedInstance();

 

2) in .cpp:

 

MyClass *MyClass::sharedInstance()
{
  static MyClass instance;
  return &instance;
}

 

 


Andrey Fidrya, @zmeyc on twitter
Please use plain text.
Developer
javayoung
Posts: 313
Registered: ‎05-31-2010
My Device: Alpha 10, Bold 9900, Storm 9530, Tour 9630, Curve 9320, Curve 8900
My Carrier: All

Re: How to implement Singleton class by Cascade C++

Yes, your solution works. Why no need to declare static in cpp?

I just switch from Java to C++.  Thank you anyway.




p(^_^)q
Good good study, day day up
Please use plain text.
Developer
Zmey
Posts: 1,510
Registered: ‎12-18-2012
My Device: PlayBook, Z10, DAC

Re: How to implement Singleton class by Cascade C++

[ Edited ]

Static keyword in global context has a different meaning than in class declaration. It means that the function has static linkage i.e. the function is local to this file. So omit 'static' when actually implementing the function.

Btw, static variables inside of functions are created and initialized on first call to the function and continue to exist after that. This is used in example above to simplify the code a bit. Member variable isn't really needed.


Andrey Fidrya, @zmeyc on twitter
Please use plain text.
Developer
javayoung
Posts: 313
Registered: ‎05-31-2010
My Device: Alpha 10, Bold 9900, Storm 9530, Tour 9630, Curve 9320, Curve 8900
My Carrier: All

Re: How to implement Singleton class by Cascade C++

Thank you for your patient reply. I got it. :Rolleyes:




p(^_^)q
Good good study, day day up
Please use plain text.
Developer
ekke
Posts: 1,428
Registered: ‎04-08-2010
My Device: Z10 (red Limited Edition), Q10, Z30
My Carrier: Telekom.de, O2, Vodafone

Re: How to implement Singleton class by Cascade C++

I'm using this header file (no cpp only header):

#ifndef SINGLETON_HPP_
#define SINGLETON_HPP_

#include <QObject>

// http://www.qtcentre.org/wiki/index.php?title=Singleton_pattern
template <class T>
class Singleton
{
public:
    static T& Instance()
    {
        static T _instance; // create static instance of our class
        return _instance;   // return it
    }

private:
    Singleton();	// hide constructor
    ~Singleton();	// hide destructor
    Singleton(const Singleton &); // hide copy constructor
    Singleton& operator=(const Singleton &); // hide assign op
};


#endif /* SINGLETON_HPP_ */

 then I can use Singletons of Classes without the need to change the classes itself in this way:

mServiceSettings = &Singleton<ServiceSettings>::Instance();

 now I have a Singleton of ServiceSettings without any changes on ServiceSettings.cpp or ServiceSettings.hpp

-------------------------------------------------------------------------------
ekke (independent software architect, rosenheim, germany)

BlackBerry Elite Developer
International Development Mobile Apps BlackBerry 10 Cascades
Cascades - Workshops / Trainings / Bootcamps

Open Source Enthusiast
blog: http://ekkes-corner.org videos: http://www.youtube.com/user/ekkescorner
bb10-development: http://appbus.org Twitter: @ekkescorner
Please use plain text.
Developer
javayoung
Posts: 313
Registered: ‎05-31-2010
My Device: Alpha 10, Bold 9900, Storm 9530, Tour 9630, Curve 9320, Curve 8900
My Carrier: All

Re: How to implement Singleton class by Cascade C++

It is the C++ template. I didn't know C++ can also make a template class. Thank you Ekke.




p(^_^)q
Good good study, day day up
Please use plain text.