What is the best method to access a member of a class?

1

What is the best method to access a member of a class in terms of speed and organization?

class cMinhaClasse
{
public:
    void Funcao(int r);
};

int main()
{
    // Método 1 - Acessando classe por ponteiro
    cMinhaClasse *mClasse = new cMinhaClasse();

    if(!mClasse)
    {
        MessageBoxA(NULL, "Erro ao criar o ponteiro para classe     MinhaClasse", "Fatal erro!", NULL); 
    }
    else
    {
        mClasse->Funcao(50);
    }

    // Método 2 - Acessando diretamente
    cMinhaClasse::Funcao(50);

    // Método 3 - Acessando por objeto
    cMinhaClasse MinhaClasse;
    MinhaClasse.Funcao(50);
}
    
asked by anonymous 22.09.2017 / 18:55

2 answers

2

Each one does something different so the comparison gets complicated. The best is the one that caters to you in the specific need of that moment.

  

cMyClass * mClasse = new cManyClass ();

Here you are creating an object in the heap and assigning a pointer to this object in the variable. Track time can be long and you generally have to manage your destruction if the class no longer does. You can use a smart pointer to do this, but it was not used in this case. If you do not release this memory there will be a leak. It should only be used if you need it, which does not seem to be the case here, but this example is useless, so it does not count.

  

cMinhaClass :: Function (50);

Here's calling a static function, it does not create an object, if it does not depend on any object it can be a good one. Creating an object to do something that did not need it does not make sense. It seems to be the case, so I would say that in this restricted example is the best option. But if you change a little and in real cases it will be very different, this option may not be viable.

  

cMyClick MyClass;

Here you are creating the direct object in stack and memory management is automatic, but the lifetime is only as long as it is within the function where the object was created. Again you are creating an unnecessary object in what has been described.

Compilers do not execute anything, they render a text and a general execution code.

The running time for these things is in the nanoseconds, not the millimeters.

Of course, creating an object is much more time-consuming, but if you have to, you can not escape it. If you do not need it does not make any sense to create. As in the link above, in the stack it is faster than in the heap , but nothing beats creating anything.

What really gives performance is to make correct algorithms, to understand the whole operation of the code in every detail. Knowing a detail and not knowing others, all implications, as the characteristics interact with each other, will make it difficult to achieve performance. In fact the search for it in this way is not a good way.

Strange syntax is not a matter of taste and nothing interferes with organization. For example, for my taste this code is poorly written and strange. But there are those who do not.

It matters little if it's namespace or class in loose functions.

    
22.09.2017 / 19:17
0
class Foobar
{
    public:

        Foobar( void ) {};
        virtual ~Foobar( void ) {};

        void xpto( int n ) {};
        static void xyz( int n ) {};
};


int main()
{
    Foobar::xyz(50);  // OK: Acessando método estático da classe, não é necessario uma instancia.

    Foobar::xpto(50); // ERRO: Método não estático, é necessário uma instância.

    Foobar * p = new Foobar();   // Instancia do objeto no "heap" - Mais lento e mais complexo gerenciar
    p->xpto(50);                 
    delete p;                    // Instancias de objetos no "heap" precisam ser desalocadas


    Foobar f;        // RECOMENDADO: Instancia do objeto no "stack" - Mais rapido e mais facil gerenciar
    f.xpto(50);
}
    
22.09.2017 / 21:48