Pointers to Objects

Dec 27, 2012 at 7:21pm
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 Dec 27, 2012 at 7:21pm
Dec 27, 2012 at 7:59pm
The code you posted results in undefined behavior. No benefit there!
Dec 27, 2012 at 8:23pm
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.