What is the best method to access a class member?

Asked

Viewed 248 times

2

What is the best method to access a class member for 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);
}
  • This is not c++?

  • Yes is c++, posted in the wrong area?

  • Ms-access is not c++. I will set the tags

  • Sorry, I actually put access (to indicate access to classes) I do not know why it ended up leaving as Ms-access

  • Set speed and organization. Explain what you want to know.

  • What I meant when I wrote speed is more or less like this: when I access a function it usually takes a while for the compiler to execute the function, within a class I believe it takes a few more milliseconds to execute it, what is the best-performing class access method? What I tried to say with organization is for example: To use a class directly with :: is very strange because for those who have not created the code the first feeling that has is that this is a namespace not a class.

Show 1 more comment

2 answers

2


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

cMinhaClasse *mClasse = new cMinhaClasse();

Here is creating an object in heap and assigning a pointer to this object in the variable. Track time can be long and usually you 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 leakage. It should only be used if needed, which doesn’t seem to be the case here, but this example is useless anyway, so it doesn’t count.

cMinhaClasse::Function(50);

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

cMinhaClasse Minhaclasse;

Here is creating the direct object in stack and memory management is automatic, but life time is only while inside the function where the object was created. Again you are creating an object needlessly in what has been described.

Compilers do not execute anything (with exception in C++, but this does not need to be considered for understanding here), they process a text and general execution code.

The running time of these things is in the nanosecond house and not milliseconds.

Of course creating an object is much more time consuming, but if you need it there is no escape. If you don’t need it makes no sense to create. As in link above describes, in stack is faster than in heap, but nothing beats creating nothing.

What gives even performance is to make correct algorithms, understand the entire functioning of the code in every detail. Knowing one detail and not knowing others, all the implications, how the characteristics interact with each other, will make it difficult to get performance. In fact the search for him in this way is not a good way.

Strange syntax or not is a matter of taste and nothing interferes with organization. For example, for my taste this code is poorly written and strange, but some people do not think.

It matters little if it is namespace or class in single functions.

  • Thanks for the reply, about the part "For example, for my taste this code is poorly written and strange. But there are those who do not." could give me a small example of how to you would be a clean code?

  • For example you use this: https://answall.com/q/139321/101

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);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.