Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8906139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:34:26+00:00 2026-06-15T02:34:26+00:00

I need several unique objects which are always available as program runs. I followed

  • 0

I need several unique objects which are always available as program runs. I followed the Singleton design pattern and it suggests using method getInstance()to fetch the objects. But I prefer to retrieve the objects immediately without calling method. So I wrote a class which provides several unique objects returned by reference type instead of pointer.

class Priority
{
public:
    static Priority &High;
    static Priority &Medium;
    static Priority &Low;

    std::string getName(void);
    int getLevel(void);

private:
   Priority(int level, std::string const& name);
   ~Priority();
   Priority(Priority const&);
   const Priority &operator = (Priority const&);

   int level_;
   std::string name_;
};

Priority &Priority::High   = Priority(3, "High");
Priority &Priority::Medium = Priority(2, "Medium");
Priority &Priority::Low    = Priority(1, "Low");

Priority::Priority(int level, std::string const& name)
   : level_(level), name_(name)
{ }

Priority::~Priority() { }

inline std::string Priority::getName(void)
{
    return name_;
}

inline int Priority::getLevel(void)
{
    return level_;
}

I wrote an example program that uses the above class.

int main()
{
    Priority &m = Priority::High;
    std::string name = m.getName();
    int level = m.getLevel();
    return 0;
}

The program worked fine. Hence I assumed the memory allocated for variable
Priority &Priority::High = Priority(3, "High");won’t be wiped out until the program stops or dll file storing the class is unloaded. Am I correct?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T02:34:27+00:00Added an answer on June 15, 2026 at 2:34 am

    Problem.

    The code you’ve shown shouldn’t compile: you can’t bind a reference to non-const to an rvalue.

    Examples with Visual C++ 11.0 and g++ 4.7.1:

    
    [D:\dev\test]
    > cl foo.cpp
    foo.cpp
    foo.cpp(27) : warning C4239: nonstandard extension used : 'initializing' : conversion from 'Priority' to 'Priority &'
            A non-const reference may only be bound to an lvalue
    foo.cpp(28) : warning C4239: nonstandard extension used : 'initializing' : conversion from 'Priority' to 'Priority &'
            A non-const reference may only be bound to an lvalue
    foo.cpp(29) : warning C4239: nonstandard extension used : 'initializing' : conversion from 'Priority' to 'Priority &'
            A non-const reference may only be bound to an lvalue
    
    [D:\dev\test]
    > gnuc foo.cpp
    foo.cpp:27:48: error: invalid initialization of non-const reference of type 'Priority&' from an rvalue of type 'Priority'
    foo.cpp:28:50: error: invalid initialization of non-const reference of type 'Priority&' from an rvalue of type 'Priority'
    foo.cpp:29:47: error: invalid initialization of non-const reference of type 'Priority&' from an rvalue of type 'Priority'
    
    [D:\dev\test]
    > _
    

    Tool usage.

    With Visual C++, use option /W4 (warning level 4) to get the warnings shown above.

    You can put this and other “make it standard-conforming” options in the environment variable CL (and ditto, LINK for the Microsoft linker, e.g. /entry:mainCRTStartup as a default linker option).

    [D:\dev\test]
    > echo %CL%
    /nologo /EHsc /GR /W4 /FI"progrock/cppx/macro/c++11.for_msvc_11.h"
    
    [D:\dev\test]
    > _
    

    C++ fix for the problem.

    Instead of lvalue references you could use rvalue references, but although Visual C++ is happy with that, g++ then complains about the private destructor.

    So intead of the reference idea – what purpose was it meant to serve? – you can simply use ordinary static data members.

    However, with ordinary static data members, as opposed to e.g. Meyers’ singletons, you run the risk of banging into the static initialization order fiasco. So if you absolutely must provide globals, I recommend using singletons for that. And as for the kind of singleton, using the simplest possible solution is often the best, which means Meyers’ singletons (I just linked the first reasonably-looking google result).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need use Insert Ignore for my table, which have Unique index on several
We need to create several SQL reports, each of which depends upon the results
I have a string which has several html comments in it. I need to
I need to analyse several sets of data which are associated with different parameter
I have a one page document that has several images which need to load
I need to run one script from several machines but each machine requires unique
I have several sparsely populated tables in an Excel workbook from which I need
I need to copy several files to remote server. for database in `mysql -Bseshow
I need to put several folders into artifact archive. Now I am doing it
I need to multiply several 1000s digits long integers as efficiently as possible in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.