1

this is more a generell C++ beginner Question:

I have 2 classes:

  • Class A, including a 'ReadData' Method, which is called as soon as new Data is received by a TCP Socket
  • Class B, including a Method 'Start' which is sending big amounts of data via TCP.

Due to the architecture, its not possible to have both methods in one class.

What I want to do:

  1. Start a Timer as soon as 'Start' in Class B is invoked.
  2. Stopp the Timer as soon as the 'ReadData'in Class A is invoked.
  3. Then i will calc the difference to see how long it took...

My Question:

  • Where do I create the Object:

    QTimer transferTimer;
    
  • How can I pass the Object to my both Classes?

How is the proper way in C++ to handle this?

Thank you.

  • You might have a third class C that will hold the timer (`QTimer`). Both classes A and B will keep the references to class C instance. Class B will invoke C::start() to start the timer, class A will call C::stop() to stop the timer. Function C::diff() will return the time elapsed (`QTime::elapsed ()`). – vahancho Feb 02 '18 at 11:33
  • can you maybe give an example for "..keep the references to class C"? btw: Class A is located in another namespace than B – Chris Nelson Feb 02 '18 at 11:35
  • Do you have an access to both classes? Can you modify them (code)? – vahancho Feb 02 '18 at 11:40
  • Yes, that should be no prob – Chris Nelson Feb 02 '18 at 11:42

1 Answers1

4

Here is one of the possible solutions. It's simplified to demonstrate the idea:

class C
{
public:
  void start()
  {
    m_startTime = QTime::currentTime();
  }

  void stop()
  {
    m_endTime = QTime::currentTime();
  }

  int difference() const
  {
    return m_startTime.secsTo(m_endTime);
  }

private:
  QTime m_startTime;
  QTime m_endTime;
};

class A
{
public:
  A(std::shared_ptr<C> c) : m_c(c)
  {}

  void ReadData()
  {
    // ...
    m_c->stop();

    int transferTime = m_c->difference(); // seconds
  }

private:
  std::shared_ptr<C> m_c;
};

class B
{
public:
  B(std::shared_ptr<C> c) : m_c(c)
  {}

  void start()
  {
    // ...
    m_c->start();
  }

private:
  std::shared_ptr<C> m_c;
};

int main(int argc, char ** argv)
{
  auto c = std::make_shared<C>();
  // a and b keep reference to instance of class C
  A a(c);
  B b(c);

  [..]
  return 0;
}
vahancho
  • 20,808
  • 3
  • 47
  • 55
  • Thank you, i understand the idea. The thing is: The constuctor of Class A is called from another Class D (here, the objects of A are created). Okay, my first idea was to create the Timer in Class D then and pass the object to A, but the Objects of Class B are not created in Class D so iam running into problems. Is there another way than passing a pointer in constuctor? – Chris Nelson Feb 02 '18 at 12:56
  • You can pass the pointer afterwards. For example with a function call that will set the timer object. This should happen before B::start() call, of course. – vahancho Feb 02 '18 at 13:12
  • Thank you, i will try i implement the function call and write if i fail :) – Chris Nelson Feb 02 '18 at 14:23
  • Nope, sorry i dont get it. I have now a Class C like you gave in the solution. In Class C i created a object as a pointer an set it to null: ClassC* transferTimer = nullptr; Then i implementet a function in Class C : void set Timer() { transferTimer = new ClassC() }; But how do i call the Function now from Class B, where my timer should start? I tried to include Class C but dont know how to start the function then. And how can i use Stop() from Class A (same object)? Some C++ basics are missing here ;_; – Chris Nelson Feb 02 '18 at 14:35
  • You can use class C as is. Then you have to 1) Create an instance of class C (a shared pointer), 2) Pass that pointer to class A object 3) pass the same pointer to class B object, 4) Use class C functions when needed. – vahancho Feb 02 '18 at 14:43
  • Where do i create that shared pointer of Class C? In Class C itself? And where do i pass the pointer to Class A? Thats what i dont get. In the solution, you create and pass it in the main() function. But that would be pain in my case.You create Objects of A and B in main, but in my case they are created in other classes which where created in other classes and so on.. – Chris Nelson Feb 02 '18 at 14:50
  • 1
    To answer I need to know your A B and D classes and their relation to each other, at least. If D creates A, who creates B? You have to find a component that "knows" about both B and D. That component will create C, pass it to B and D, and D will pass it to A. – vahancho Feb 02 '18 at 14:56
  • The design is like to 2 branches, going down from main(). And at the very end of each branch - there are my 2 classes who need to use the timer. -> So i think the only solution is to create the Timer as shared ptr in the main() function and pass it through many classes in both branches. You help alot. Thank you. – Chris Nelson Feb 02 '18 at 15:05