0x_Anakin
0x_Anakin

Reputation: 3269

Accessing member function with pointer vs object

Hello I started learning c++ and I'm following a video tutorial series.

For the sake of simplicity consider the following code:

#include <iostream>
using namespace std;

class Test{
public:
    Test();
    void printCrap();
private:
protected:
};

Test::Test(){

};

void Test::printCrap(){
    cout << "I don't get this yet..." << endl;
};

int main(){

    Test testOne;
    Test *testTwo = &testOne;

    testOne.printCrap();
    testTwo->printCrap();

    return 0;
};

It's similar to the tutorial's code with the only exception that I merged the code into 1 cpp for the sake of this question.

What the tutorial doesn't explain is beside the obvious what are is the actual difference / benefits and where is this applicable.Perhaps a scenario where -> vs . would matter?

Upvotes: 0

Views: 913

Answers (7)

eatSleepCode
eatSleepCode

Reputation: 4637

The . (dot) operator and the -> (arrow) operator are used to reference individual members of classes, structures, and unions.

The dot operator is applied to the actual object. The arrow operator is used with a pointer to an object.

Test testOne;
Test* testTwo = &testOne; 

here testTwo is a pointer variable of type Test pointing at memory location of testOne, to access the members of Test class with testTwo it needs to dereference which is done with the help of -> arrow operator.

Upvotes: 0

yi.tang.uni
yi.tang.uni

Reputation: 103

imaging you have another class, Test2, which is inherited from your original Test. Test2 has is own version of printCrap() function. We have two ways calling:

//as member function
Test.printCrap();
Test2.printCrap();
//as pointer 
ptr = &Test;
ptr->printCrap();
ptr = &Test2;
ptr->printCrap(); 

The above code illustrate the usage only, doesn't tell which one is better.

Upvotes: 0

Boris Ivanov
Boris Ivanov

Reputation: 4254

Well member of class can be accessed by applying . operator to an object of class testTwo or by applying the -> to a pointer of class testTwo. Arrow -> came from C time and abit redundant.

Also inside class you don't need any operators. Just call printCrap. And if you looking for in static member function you use :: f.e. testOne::staticPrint.

Upvotes: 0

Lukasas
Lukasas

Reputation: 145

Its just a memory work. When you allocate it

Test testOne;

you just create an object in memory with you operate, when you point at it

Text *testTwo = &testOne;

you point at same location whenre testOne is. You just created another name for the same part of memory. And when you need to pass this thing to a function, you don't want to create whole new object, but you just want to point at it like

function workWithObject(Test *testit)

So the testit points to same part of memory as testTwo and it is textOne.

Upvotes: 1

John Dibling
John Dibling

Reputation: 101456

Simply, use . when the variable you're calling through is either an actual object or a reference. Use -> when the variable is a pointer-to-object.

In general, prefer to use either actual objects or reference-to-objects. Pointers are used on as as-needed basis. Some cases where you often need to use pointers are:

  1. In using polymorphism
  2. When you need the object to outlive the scope in which it was created

Upvotes: 3

mathematician1975
mathematician1975

Reputation: 21351

It is not a matter of benefits or differences. If you have a pointer you need to dereference it to access members. The -> operator is equivalent to

testTwo->print();
(*testTwo).print();  // same thing.

If you have an instance (or reference) you use

testTwo.print();

This is the language syntax - its not about preferences

Upvotes: 1

Medinoc
Medinoc

Reputation: 6608

testTwo->printCrap() is just syntactic sugar for (*testTwo).printCrap(), nothing more, nothing less.

Upvotes: 3

Related Questions