vendredi 17 septembre 2021

What's more efficient, creating objects within a class or passing objects into class? [closed]

This example would have me create all motor objects within the global scope and pass them in.

#include <Motor.h>

Motor motor1;
Motor motor2;
Motor motor3;

class Axis
{
private:
    Motor _motor;
public:
    Axis(Motor motor)
    {
        _motor = motor;
        _motor.doStuff();
    }
};

Axis axis1(motor1);
Axis axis2(motor2);
Axis axis3(motor3);

or

This example would organize my motor objects into their respective axis objects.

#include <Motor.h>

class Axis
{
private:
    Motor _motor;
public:
    Axis()
    {
        Motor _motor;
        _motor.doStuff();
    }
};

Axis axis1;
Axis axis2;
Axis axis3;

I'm curious if there is performance differences. Also any notes about code maintainability are appreciated.

Aucun commentaire:

Enregistrer un commentaire