473,657 Members | 2,550 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Singleton and static function

Hi,

I have a singleton class defined like this :

class UIManager : public CSingleton<UIMa nager>,
public CObject
{
protected:
DECLARE_DYNAMIC (UIManager)
friend class CSingleton<UIMa nager>;

UIManager();
virtual ~UIManager();

public:
....
};

and I was using this code like this :

A)
UIManager* l_pUiMgr = UIManager::GetI nstance();
ASSERT (l_pUiMgr != NULL);
l_pUiMgr->GetResText( a_ResId, bStripHtml);
But I was fed up with always typing this so I have declared below my
UIManager class a static function :

static inline UIManager& UIManager() { return *(UIManager::Ge tInstance()); }
and I wanted to be able to call it like that :

UIManager().Get ResText( a_ResId, bStripHtml);

The problem is I get some compilations errors with the code in A)

5>c:\wce_v42\in c\BaseView.h(22 7) : error C2065: 'l_pUiMgr' : undeclared
identifier

Why I cannot write UIManager* now ?

Oct 31 '08 #1
4 2820
On Oct 31, 3:27*pm, John Doe <mos...@anonymo us.orgwrote:
Hi,

I have a singleton class defined like this :

class UIManager : public CSingleton<UIMa nager>,
* * * * * * * * * * * * * * * * * public CObject
{
protected:
* * * * DECLARE_DYNAMIC (UIManager)
* * * * friend class CSingleton<UIMa nager>;

* * * * UIManager();
* * * * virtual ~UIManager();

public:
...

};
What do you need that CSingleton<for? Can you not just have a global
object?
and I was using this code like this :

A)
UIManager* l_pUiMgr = UIManager::GetI nstance();
ASSERT (l_pUiMgr != NULL);
l_pUiMgr->GetResText( a_ResId, bStripHtml);
That ASSERT is probably useless because if UIManager is allocated on
the heap (using new) it throws an exception. It it it allocated as a
function static variable, than it can never be NULL.
But I was fed up with always typing this so I have declared below my
UIManager class a static function :

static inline UIManager& UIManager() { return *(UIManager::Ge tInstance()); }

and I wanted to be able to call it like that :

UIManager().Get ResText( a_ResId, bStripHtml);
Which looks better.
The problem is I get some compilations errors with the code in A)

5>c:\wce_v42\in c\BaseView.h(22 7) : error C2065: 'l_pUiMgr' : undeclared
identifier

Why I cannot write UIManager* now ?
This error says that you are accessing l_pUiMgr object, which is not
available from the current scope. Either you misspelled it or did not
include the necessary header files.

--
Max
Oct 31 '08 #2
On Oct 31, 10:27*am, John Doe <mos...@anonymo us.orgwrote:
Hi,

I have a singleton class defined like this :

class UIManager : public CSingleton<UIMa nager>,
* * * * * * * * * * * * * * * * * public CObject
{
protected:
* * * * DECLARE_DYNAMIC (UIManager)
* * * * friend class CSingleton<UIMa nager>;

* * * * UIManager();
* * * * virtual ~UIManager();

public:
...

};

and I was using this code like this :

A)
UIManager* l_pUiMgr = UIManager::GetI nstance();
ASSERT (l_pUiMgr != NULL);
l_pUiMgr->GetResText( a_ResId, bStripHtml);

But I was fed up with always typing this so I have declared below my
UIManager class a static function :

static inline UIManager& UIManager() { return *(UIManager::Ge tInstance()); }

and I wanted to be able to call it like that :

UIManager().Get ResText( a_ResId, bStripHtml);

The problem is I get some compilations errors with the code in A)

5>c:\wce_v42\in c\BaseView.h(22 7) : error C2065: 'l_pUiMgr' : undeclared
identifier

Why I cannot write UIManager* now ?
Why don't you tell us? l_pUiMgr is an undeclared identifier. You are
doing something wrong, what that might be we don't know. Put together
a simple, compileable case forth without MS-specific macros.
Oct 31 '08 #3
Salt_Peter a écrit :
On Oct 31, 10:27 am, John Doe <mos...@anonymo us.orgwrote:
>Hi,

I have a singleton class defined like this :

class UIManager : public CSingleton<UIMa nager>,
public CObject
{
protected:
DECLARE_DYNAMIC (UIManager)
friend class CSingleton<UIMa nager>;

UIManager();
virtual ~UIManager();

public:
...

};

and I was using this code like this :

A)
UIManager* l_pUiMgr = UIManager::GetI nstance();
ASSERT (l_pUiMgr != NULL);
l_pUiMgr->GetResText( a_ResId, bStripHtml);

But I was fed up with always typing this so I have declared below my
UIManager class a static function :

static inline UIManager& UIManager() { return *(UIManager::Ge tInstance()); }

and I wanted to be able to call it like that :

UIManager().Ge tResText( a_ResId, bStripHtml);

The problem is I get some compilations errors with the code in A)

5>c:\wce_v42\i nc\BaseView.h(2 27) : error C2065: 'l_pUiMgr' : undeclared
identifier

Why I cannot write UIManager* now ?

Why don't you tell us? l_pUiMgr is an undeclared identifier. You are
doing something wrong, what that might be we don't know. Put together
a simple, compileable case forth without MS-specific macros.

I mean that the following line doesn't compile anymore :
UIManager* l_pUiMgr = UIManager::GetI nstance();
but maybe I forgot one include ...
Oct 31 '08 #4
On Oct 31, 3:50*pm, Mosfet <mos...@anonymo us.orgwrote:
Salt_Peter a écrit :


On Oct 31, 10:27 am, John Doe <mos...@anonymo us.orgwrote:
Hi,
I have a singleton class defined like this :
class UIManager : public CSingleton<UIMa nager>,
* * * * * * * * * * * * * * * * * public CObject
{
protected:
* * * * DECLARE_DYNAMIC (UIManager)
* * * * friend class CSingleton<UIMa nager>;
* * * * UIManager();
* * * * virtual ~UIManager();
public:
...
};
and I was using this code like this :
A)
UIManager* l_pUiMgr = UIManager::GetI nstance();
ASSERT (l_pUiMgr != NULL);
l_pUiMgr->GetResText( a_ResId, bStripHtml);
But I was fed up with always typing this so I have declared below my
UIManager class a static function :
static inline UIManager& UIManager() { return *(UIManager::Ge tInstance()); }
and I wanted to be able to call it like that :
UIManager().Get ResText( a_ResId, bStripHtml);
The problem is I get some compilations errors with the code in A)
5>c:\wce_v42\in c\BaseView.h(22 7) : error C2065: 'l_pUiMgr' : undeclared
identifier
Why I cannot write UIManager* now ?
Why don't you tell us? l_pUiMgr is an undeclared identifier. You are
doing something wrong, what that might be we don't know. Put together
a simple, compileable case forth without MS-specific macros.

I mean that the following line doesn't compile anymore :
UIManager* l_pUiMgr = UIManager::GetI nstance();
but maybe I forgot one include ...- Hide quoted text -

- Show quoted text -
I had no problem, then again, i don't need MS code to do any of it.
i'ld suggest declaring a pointer only (which does not construct
anything) just to find out if UIManager is recognized as a complete
type.

UIManager* p_mgr;

If that fails then includes are indeed the issue and you should get an
error specifically stating what is not recognized as a known type. I
have no clue what header CSingleton is found in (only a Singleton can
access UIManager's protected constructor).

I will say that your setup should look like the following, which does
work, although my Singleton type needs a little more tightening to
prevent side-effects.

[OT]
MS and MFC as well as other languages like Java live in an 'everything
is an Object or CObject' world. As if an instance was somehow
otherwise NOT an object. That mentality is bad, nasty, buggy, insane,
dumb, stupid, etc.
[/OT]

#include <iostream>

class Object { };

template< typename T >
class Singleton
{
protected:
Singleton() { std::cout << "Singleton()\n" ; }
~Singleton() { std::cout << "~Singleton()\n "; }
private:
Singleton(const Singleton& copy);
Singleton& operator= (const Singleton&);
public:
static T* const GetInstance()
{
static T t;
std::cout << "&t = " << &t << std::endl;
return &t;
}
};

class Manager : public Singleton< Manager >, public Object
{
friend class Singleton<Manag er>;
protected:
Manager() { std::cout << "Manager()\ n"; }
virtual ~Manager() { std::cout << "~Manager() \n"; }
};

int main()
{
Manager* p_mgr = Manager::GetIns tance();
std::cout << "Manager* p_mgr = ";
std::cout << p_mgr << std::endl;
}

Nov 1 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2431
by: Phil Powell | last post by:
Consider this: class ActionHandler { /*------------------------------------------------------------------------------------------------------------------------------------------------------------------- This class will be a singleton class structure by calling its constructor by reference only (prefixed by '&'). Is primarly used to reference its errorArray Array property as a single reference to allow for static usage
11
2346
by: [Mystic] | last post by:
I was just wondering something about this. I wanted to do something similar, like having a mysql class that there can only be one of. The purpose of this was not to have hundereds to mysql connections open, and also to track how many queries are being sent/second. When creating the singleton, I assume you use this syntax: $var = &new singelton;
3
2479
by: Alicia Roberts | last post by:
Hello everyone, I have been researching the Singleton Pattern. Since the singleton pattern uses a private constructor which in turn reduces extendability, if you make the Singleton Polymorphic what sort of problems/issues should be considered? Also, I see that a singleton needs to be set up with certain data such as file name, database URL etc. What issues are involved in this, and how would you do this? If someone knows about the...
3
3919
by: Harry | last post by:
Hi ppl I have a doubt on singleton class. I am writing a program below class singleton { private: singleton(){}; public: //way 1
5
4703
by: LinuxGuy | last post by:
Hi, I have come across singleton class with some member variables are declared as static with public scope. As singleton class always return only one instance. ie. single copy of object is maintained all the time. can someone tell me the reason behind declaring those variables as static one.
10
1472
by: A_StClaire_ | last post by:
hi, I have a singleton Evaluation class that I'm calling repeatedly in one sequence. would someone plz have a look at the code below and tell me if one instance of the singleton can ever "clash" with another? I'm asking because I'm getting unexpected results. I've looked for the problem the past several days and can't attribute the anomalies to logic errors.
6
2265
by: Manuel | last post by:
Consider the classic singleton (from Thinking in C++): ----------------------------------------------------- //: C10:SingletonPattern.cpp #include <iostream> using namespace std; class Singleton { static Singleton s; int i; Singleton(int x) : i(x) { }
5
21154
by: Damien | last post by:
Hi all, I'm using a pretty standard C++ Singleton class, as below: template <typename T> class Singleton { public: static T* Instance() {
10
3316
by: wkaras | last post by:
Why aren't "Singletons" done like this in C++: A.hpp: class A { public: static A theA; // ...
0
8425
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8326
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8845
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8743
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8622
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6177
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5647
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4333
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2745
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.