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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:37:33+00:00 2026-05-22T21:37:33+00:00

Consider the following code: struct I { SomeInternalState m_internalState; }; struct S { I

  • 0

Consider the following code:

struct I
{
    SomeInternalState m_internalState;
};

struct S
{
    I * m_i;
    set_I (I * i)
    {
        m_i = i;
        makeSomeChangesToTheInternalStateOfI(m_i);
    }
};

struct S_1 : S { ... };
struct S_2 : S { ... };
...
struct S_n : S { ... };

It is given that an arbitrary count of instances of S_1, … S_n may be created, and all of them will call set_I() only once.

Now, I want the instances of S_1, … S_n to makeSomeChangesToTheInternalStateOfI() only once per instance of I per type of S_x, so that I could call set_I() from different instances of the same class S_x with the same instance of I and be sure that the internal state of I will be modified only during the first call.

The likely decision is to put some dispatch table into I, but I can’t think of a sensible key for it, based solely on the type of S_x instance and not involving any hand-written “runtime type id” constants for all possible types S_1, … S_n.

How do I do it?

EDIT:

The points that I should have stressed:

1) There may be more than one instance of I at a time, and the S_x-classes should be able to change the state of multiple instances of I, but only once per each instance. That is:

I i1, i2;
S_1 s1a, s1b;
S_2 s2a, s2b;

// all possible combinations:
s1a.changeStateOfI(i1);
s1b.changeStateOfI(i1);
s2a.changeStateOfI(i1);
s2b.changeStateOfI(i1);
s1a.changeStateOfI(i2);
s1b.changeStateOfI(i2);
s2a.changeStateOfI(i2);
s2b.changeStateOfI(i2);

In this fragment, the states of both i1 and i2 should only be changed once by S_1‘s method (via s1a) and once by S_2‘s (via s2a).

2) I suppose, reference-counting could be used to solve the problem – there’s no need to know exactly, how many times the initialisation occurred, it’s enough to know if it did or not.

EDIT2

I’ve marked n.m.‘s suggestion as the answer, though my final solution differs a bit. Here it is, so that others may use it too:

struct AbstractS
{
    I * m_i;
    virtual void set_I (I * i) = 0;
};


template <typename self_T>
struct RegS : AbstractS
{
    static std::set<I *> s_registeredContexts;

    virtual void set_I (I * i)
    {
        m_i = i;

        if (i == NULL || s_registeredContexts.count(i) > 0) return;

        makeSomeChangesToTheInternalStateOfI(i);
        contexts.insert(i);
    }
};

template <typename self_T>
std::set<I *> InterpreterState<self_T>::s_registeredContexts;


struct S_1 : RegS<S_1> { ... };
struct S_2 : RegS<S_2> { ... };
...
struct S_n : RegS<S_n> { ... };

The difference compared to n.m.‘s variant is that I’ve used the CRTP pattern here instead of enumerating the instantiations, the thing I wanted to avoid too.

  • 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-22T21:37:33+00:00Added an answer on May 22, 2026 at 9:37 pm

    You can use typeinfo as a key, but it’s a bad idea. You should not count types in your program. Let me explain with a simple example.

    Let’s say you have a Vehicle type and its descendants Car, Truck and Bike. You call your function once per each of these classes. So far so good. Now you need, for a completely unrelated reason, to handle SUVs, RacingCars, GarbageTrucks, Trikes, RedCars, ReddishCars and YellowishReddishWithGreenishTintCars. Your decision on the number of times your function is going to be called should be completely orthogonal to your decision about introducing or not introducing separate classes for each of these cases.

    So you need something to tag your Vehicles as distinct or similar, solely for the purpose of calling your function once per a bunch of similar objects. One way to achieve that is with a class template and a bunch of type parameters (any kind of type parameters).

    class ChangerOfInternalStateOfI
    {
      public:
        ChangerOfInternalStateOfI (I* i) { 
          makeSomeChangesToTheInternalStateOfI(i); 
        }
    };
    
    template <int n>
    class S_N : public S
    {
      public:
        S_N() {
           static ChangerOfInternalStateOfI changer;
        }
    };
    
    typedef S_N<1> S_1;
    typedef S_N<2> S_2;
    

    You can use enum instead of int, or a typename, doesn’t really matter. The point is that all of your ChangerOfInternalStateOfI are distinct because they belong to distinct classes, and each of the constructors is going to be called once.

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

Sidebar

Related Questions

Consider the following code: template <int dim> struct vec { vec normalize(); }; template
Consider a following code: struct X { void MethodX() { ... } }; struct
Consider the following code: struct Calc { Calc(const Arg1 & arg1, const Arg2 &
Consider the following code: struct Foo { mutable int m; template<int Foo::* member> void
Let's consider the following 3 code lines: struct stat buffer; status = lstat(file.c_str(), &buffer);
Consider the following code. using boost::shared_ptr; struct B; struct A{ ~A() { std::cout <<
Consider the following code: #include <vector> struct A { explicit A(int i_) : i(i_)
Consider the following code: #include <iostream> struct foo { // (a): void bar() {
Consider the following code: template<class T, class F> struct X {}; template<class T, class
Consider the following code: #include <vector> struct S { int a; double b; };

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.