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 8477211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:23:56+00:00 2026-06-10T18:23:56+00:00

I am trying to create an instance of a singleton class inside of a

  • 0

I am trying to create an instance of a singleton class inside of a wrapper in a multithreaded environment. I am using the wrapper to ease my work and not to write multiple times Lock and Unlock in ManagerSources.

#define ManagerSOURCES() ManagerSources::GetInstance()

// Singleton
class ManagerSources :public Mutex {

protected:

    std::map< std::string , SourcesSPtr > Objects;

    static ManagerSources * Instance; // declared in a cpp file
    ManagerSources() {}
    ManagerSources( const ManagerSources& cpy ) {}
    ManagerSources operator=( ManagerSources& cpy) {}

public:

    static ManagerSources* GetInstance() {
        if ( Instance == NULL )
            Instance = new ManagerSources();
        return Instance;
    }

    ...
};

// This class is a wrapper for ManagerSources in a thread programming environment
template <class T>
class SingletonThreadSafe {

protected:

    T *pointer;

public:

    class proxy {
        T* pointer;

    public:

        proxy(T* _pointer) : pointer(_pointer) {
            // LOCK();            
        }

        ~proxy(){
            // UNLOCK();
        }

        T* operator->() {
            return pointer;

        }
    };


    // Default parameter is needed for containers (eg. insert into a map) where we need
    // a constructor without parameters
    SingletonThreadSafe(T* cpy = NULL ): pointer(cpy) {

    }

    ~SingletonThreadSafe() {
    }

    SingletonThreadSafe(const SingletonThreadSafe & cpy) {

        this->pointer = cpy.pointer;

    }

    SingletonThreadSafe operator=(SingletonThreadSafe cpy) {

            this->pointer = cpy.pointer;

            return *this;

    }

    T operator*() {

        return *pointer;

    }

    proxy operator->() {

        return proxy( pointer );

    }
};

I have the following declaration

typedef SingletonThreadSafe<ManagerSources> aa;

aa( ManagerSources::GetInstance() ); // doesn't work

or 

aa( ManagerSOURCES() ); // the same as above; still not working

The syntax doesn’t work and it gives me the following error “Definition or redeclaration of ‘GetInstance’ not allowed inside a function”. And, I don’t know why. Any ideas of how I can resolve this?

Also, the strange fact for me is if I rewrite the constructor with the default parameter to

SingletonThreadSafe(T* cpy = T::GetInstace() ): pointer(cpy) {

}

the following declaration works

aa()->A_Function(A_Parameter);

And if I declare it still works

aa bb( ManagerSOURCES() ); //  it works

( SmartPtr<ManagerSources>() = ManagerSOURCES() )->A_Function(A_Parameter); // it works; 
// the constructor with the  default parameter is called 

I don’t know why I get the error “Definition or redeclaration of ‘GetInstance’ not allowed inside a function “.

I am using Xcode 4.4.1 and LLVM GCC 4.2 compiler.

  • 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-10T18:23:58+00:00Added an answer on June 10, 2026 at 6:23 pm

    This is a variant of the most vexing parse.

    aa( ManagerSources::GetInstance() ); // doesn't work
    

    is interpreted as a declaration of a function ManagerSources::GetInstance taking no parameters and returning type aa.

    You can work around it with a static_cast:

    static_cast<aa>( ManagerSources::GetInstance() );
    

    A cast to void would also work, as would anything else that prevents the expression being parsed as a declaration:

    (void) aa( ManagerSources::GetInstance() );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create an instance of a generic class using a Type object.
I am trying to create an instance of a class using Linq and XML.
I am trying to write a db util class using the singleton pattern. My
Basically, what I am trying to do is to create a Singleton instance of
I am trying to create a generic class which new's up an instance of
I am trying to use the Class.forName('com.mypack.MyDomain').newInstance() to create an instance of the grails
I'm trying to write a singleton class for maintain game data, It's called GameManager,
I'm trying to create a simple to use singleton class to connect to mysql
I'm trying to create an instance of a class to be used throughout the
I'm trying to create a global (singleton) class that can associate any type of

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.