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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:49:38+00:00 2026-05-22T22:49:38+00:00

I have a collection of worker classes, and I need to be able to

  • 0

I have a collection of worker classes, and I need to be able to construct instances of these classes dynamically with a single factory. The reasoning behind this is that new worker classes are written frequently and I’d rather not have to update a factory class per worker type every time I add a new worker class.

The way that this currently works is as follows: I’ve got a class called WorkerImplementationList with a static method:

template <typename T>
    WorkerImplementationList::registerWorkerFactoryMethod<T>(const std::string&)

that stores a pointer to T::newInstance in an internal data structure that will be retrieved by the factory later. In each of the worker classes have a static int called _dummyInstanceRegistrationVariable. In each of the worker classes’ .cc files, I have the following line (using FooWorker as an example):

int FooWorker::_dummyInstanceRegistrationVariable =
    WorkerImplementationList::registerWorkerFactoryMethod<FooWorker>("FooWorker");

What I’d like to have happen is for the static variable to be initialized before any instances of the class are constructed. This seems to work just fine when the worker class is compiled into the same binary that contains main(). However, when FooWorker is in a library (say libblahapp_workers.a) and the main executable links that library, it looks like _dummyInstanceRegistrationVariable isn’t initialized until after main() starts (I assume it’s initialized when the first instance of FooWorker is constructed), which is too late for my purposes.

I’ve also tried constructing a WorkerImplementationRegisterer<T> object in global scope that registers the appropriate worker type when it’s constructed, but here again I run into problems when the worker class is in a library external to main(); that globally-scoped object isn’t constructed before main() begins.

Would statically linking my worker libraries solve this problem? Is there a more elegant solution that I’m missing? Any help you could give me would be greatly appreciated.

  • 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-05-22T22:49:38+00:00Added an answer on May 22, 2026 at 10:49 pm

    This sounds like the linker pulling in only the objects it needs, and not pulling in the one with the global variable. Then the global simply doesn’t exist, so initialization order is a moot point.

    It’s a real problem with static libraries, with no universal solution.

    Armed with the knowledge that the linker grabs an entire compilation unit from the library at once (on all systems I’ve seen), you can probably figure out some way to put this global variable into a compilation unit that’s needed.

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

Sidebar

Related Questions

I have Collection List<Car> . How to compare each item from this collection with
I have a collection of Objective-C classes that get sub-classed at a variety of
I have a worker class that does stuff with a collection of objects. I
I have several classes that I need to format. I need to place the
Lets say I have collection of documents with specified longitude and latitude. type is
I'm planning to have collection of items stored in a TCollection. Each item will
for example I have collection foo of documents like that: {tag_cloud:[{value:games, count:10}, {value:girls, count:500}]}
I have Class and Student objects. Both have collection of another as property. Which
We have a collection of entities of type called Unit , collection of entities
I have a collection that use as a cache. How can I limit the

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.