P
P
Pavel K2016-09-13 02:17:17
OOP
Pavel K, 2016-09-13 02:17:17

The most beautiful and correct way to pass parameters through another class?

Greetings!
I often come across the following, for example:
There is a class, for example CamsList, where, accordingly, work with the list of cameras is carried out (adding, changing, deleting, etc.)
there is another class, for example Cam - the camera object itself.
Suppose the camera itself needs a common class for all cameras to work OutputBuffer (Which nafig does not need CamsList).
Those. I can either pass the class by pointer through CamsList and then, when creating a new camera in it, indicate this pointer in its constructor, or use a singleton.
I don't like either. What other ways are there, how would you do it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
nirvimel, 2016-09-13
@PavelK

#include <iostream>

class Cam;

class OutputBuffer {
public:
    OutputBuffer() { std::cout << "OutputBuffer " << this << " initialized\n"; }

    void receive(Cam &cam) { std::cout << "OutputBuffer " << this << " receiving data from camera " << &cam << "\n"; }
};

class Cam {
    static OutputBuffer buffer;
public:
    Cam() { buffer.receive(*this); }
};

OutputBuffer Cam::buffer = OutputBuffer();

int main() {
    Cam cam1, cam2;
};

OutputBuffer 0x6013d1 initialized
OutputBuffer 0x6013d1 receiving data from camera 0x7fff7742970e
OutputBuffer 0x6013d1 receiving data from camera 0x7fff7742970f

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question