Pointers to Objects

What is the benefit of using a pointer to a class? Speed? Flexibility?\
ex:
1
2
3
4
5
6
7
8
9
//Parser.h
class Parser
{
     public:
        void load_file()
        {
             //load the file
        }
};


1
2
3
4
5
6
7
8
9
10
11
12
//main.cpp
#include "Parser.h"
int main()
{
     Parser file;
     file.load_object();

     //vs
    
     Parser *PtoFile;
     PtoFile->load_object();
}
Last edited on
The code you posted results in undefined behavior. No benefit there!
Some idioms (like PIMPL) and design patterns (Singleton, for example) rely heavily on pointers to implementation or to a given instance of a class. Also, it helps you in separating the interface from the implementation (in case you have to work with many classes).
Topic archived. No new replies allowed.