I'm somewhat new to C++, and this question has been nagging at me. Is it more efficient to use a Pointer, or an actual object?

Here's an example:

class SomeObject{
public:
    SomeObject();
    /* Other methods */
};

int main(int argc, char **arcv){
    SomeObject *ptrObj = new SomeObject();
    // Do something with ptrObj

    SomeObject normObj();
    // Do the same something with normObj
}

Is storing the object's pointer, and dereferencing it to access methods/field, faster than storing it as a normal object, and calling the method/accessing the fields normally?

It probably doesn't really matter one way or the other, the compiler will probably optimize them both the same. Whether or not to use a pointer should depend on other factors. But consider that pointers are a little more dangerous because programmers often fail to clean up when done with them, causing memory leaks.

Using pointers is much more flexible since it allows you to decide on the size at run-time. However, as mentioned previously, you need to handle it with care to properly release the memory after using it.

By the way, your line 11 actually declares a function named normObj() that returns an object of type SomeObject. To declare an object, there should be no parentheses as in:

SomeObject normObj;

But on the other hand the efficiency of these are completely different :

//with pointers
void DivideBigNumbers(BigNumberClass * num, BigNumberClass * num);
//without pointers, just object
void DivideBigNumbers(BigNumberClass  num, BigNumberClass  num);
//and there is reference
void DivideBigNumbers(BigNumberClass&  num, BigNumberClass&  num);
commented: I agree. It really depends on the situation. Passing parameters by reference/pointer is recommended to passing by value. +1

Agree to firstPerson's post. It really depends on the situation. Passing by reference/pointer is usually recommended than passing by value. Passing by value requires an extra overhead due to call to copy constructor compared to passing by reference/pointer.

To the compiler, passing by reference is really no different than passing by pointer. The compiler will treat them both the same (assuming you don't attempt to do something obscure like pointer arithmetic or pointer increment/decrement -- those can not be done with references.). But IMO passing by reference is preferable to passing by pointer because references are less prone to error. As far as I can tell by looking at the assembly code produced by Microsoft compiler, the compiler transforms the reference operator into a pointer then passes the object by pointer.

>Is storing the object's pointer, and dereferencing it to access
>methods/field, faster than storing it as a normal object, and
>calling the method/accessing the fields normally?

No. If anything, pointers are slower due to the extra step of chasing the pointer. Member access is the same either way, once you get down to the actual object. However, the speed difference is negligible.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.