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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:09:03+00:00 2026-05-13T17:09:03+00:00

This program: test_header.hpp #include <boost/signal.hpp> #include <utility> class Sensor; class Recorder : public ::boost::signals::trackable

  • 0

This program:

test_header.hpp

#include <boost/signal.hpp>
#include <utility>

class Sensor;

class Recorder : public ::boost::signals::trackable {
 public:
   explicit Recorder(int id) : id_(id) {}

   // Cannot be copied
   Recorder(const Recorder &) = delete;
   Recorder &operator =(const Recorder &) = delete;

   // But can be moved so it can be stored in a vector.
   // There's a proposal for having a compiler generated default for this that would be
   // very convenient.
   Recorder(Recorder &&b) : id_(b.id_) {
      b.id_ = -1;
   }
   Recorder &operator =(Recorder &&b) {
      id_ = b.id_;
      b.id_ = -1;
      return *this;
   }

   void recordSensor(const Sensor &s);
   void addSensor(Sensor &s);

 private:
   int id_;
   char space_[1312];
};

class Sensor {
 public:
   typedef ::boost::signal<void (const Sensor &)> sigtype_t;

   explicit Sensor(int id) : val_(0), id_(id) { }

   void notify(const sigtype_t::slot_type &slot) { signal_.connect(slot); }
   void updateSensor(double newval) { val_ = newval; signal_(*this); }
   double getValue() const { return val_; }
   int getId() const { return id_; }

 private:
   sigtype_t signal_;
   double val_;
   const int id_;
};

test_body.cpp

#include "test_header.hpp"
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <iostream>
#include <vector>
#include <string>
#include <sstream>

void Recorder::addSensor(Sensor &s)
{
   ::std::cout << "Recorder #" << id_
               << " now recording Sensor #" << s.getId() << '\n';
   ::std::cout.flush();
   s.notify(::boost::bind(&Recorder::recordSensor, this, _1));
}

void Recorder::recordSensor(const Sensor &s)
{
   ::std::cout << "Recorder #" << id_ << " - new value for sensor named Sensor #"
               << s.getId() << ": " << s.getValue() << '\n';
   ::std::cout.flush();
}


int main(int argc, const char *argv[])
{
   using ::boost::shared_ptr;
   using ::std::vector;
   vector<Recorder> recorders;
   vector<shared_ptr<Sensor> > sensors;
   double val = 0.1;
   static const unsigned int recorder_every = 4;
   static const unsigned int sensor_every = 2;

   for (unsigned int i = 0; i < 9; ++i) {
      if (i % recorder_every == 0) {
         recorders.push_back(Recorder(i / recorder_every));
      }
      if (i % sensor_every == 0) {
         shared_ptr<Sensor> sp(new Sensor(i / sensor_every));
         sensors.push_back(sp);
         for (auto r = recorders.begin(); r != recorders.end(); ++r) {
            r->addSensor(*sp);
         }
      }
      for (auto s = sensors.begin(); s != sensors.end(); ++s, val *= 1.001) {
         (*s)->updateSensor(val);
      }
   }
}

And I get this output:

Recorder #0 now recording Sensor #0
Recorder #0 - new value for sensor named Sensor #0: 0.1
Recorder #0 - new value for sensor named Sensor #0: 0.1001
Recorder #0 now recording Sensor #1
Recorder #0 - new value for sensor named Sensor #0: 0.1002
Recorder #0 - new value for sensor named Sensor #1: 0.1003
Recorder #0 - new value for sensor named Sensor #0: 0.100401
Recorder #0 - new value for sensor named Sensor #1: 0.100501
Recorder #0 now recording Sensor #2
Recorder #1 now recording Sensor #2
Recorder #0 - new value for sensor named Sensor #2: 0.100803
Recorder #1 - new value for sensor named Sensor #2: 0.100803
Recorder #0 - new value for sensor named Sensor #2: 0.101106
Recorder #1 - new value for sensor named Sensor #2: 0.101106
Recorder #0 now recording Sensor #3
Recorder #1 now recording Sensor #3
Recorder #0 - new value for sensor named Sensor #2: 0.101409
Recorder #1 - new value for sensor named Sensor #2: 0.101409
Recorder #0 - new value for sensor named Sensor #3: 0.101511
Recorder #1 - new value for sensor named Sensor #3: 0.101511
Recorder #0 - new value for sensor named Sensor #2: 0.101815
Recorder #1 - new value for sensor named Sensor #2: 0.101815
Recorder #0 - new value for sensor named Sensor #3: 0.101917
Recorder #1 - new value for sensor named Sensor #3: 0.101917
Recorder #0 now recording Sensor #4
Recorder #1 now recording Sensor #4
Recorder #2 now recording Sensor #4
Recorder #0 - new value for sensor named Sensor #4: 0.102428
Recorder #1 - new value for sensor named Sensor #4: 0.102428
Recorder #2 - new value for sensor named Sensor #4: 0.102428

I’m kind of confused. When I add a Recorder it seems like all the old sensors are forgotten about.

  • 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-13T17:09:03+00:00Added an answer on May 13, 2026 at 5:09 pm

    Recorders can move in memory because you are adding to a vector as you create them, but you are binding to them for signaling as you are doing this.

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

Sidebar

Related Questions

Having a program like this: #include <iostream> #include <string> using namespace std; class test
Consider the following program: #include <iostream> #include boost/filesystem.hpp int main() { boost::filesystem::directory_entry d(test.txt); boost::filesystem::directory_entry
this program is for class to display the database in Listview.i want to add
I installed Xcode 4.3 and want to test this C++11 program: #include <type_traits> int
I am using CPPUnit to test a class in my program. This class (
What is wrong with this code: Header: #include <map> using namespace std; template<class T>
This is my test program. The variable s1 contains a vector that contains objects
I'm running this small C# test program launched from a pre-commit batch file private
Lets say I have hierarchy like this (This is just a test program. Please
I need to run this line from my c++ program: java -jar test.jar text1

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.