0

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?

2
  • The only difference is that testTwo may contain an invalid address (in the general case, not in your example) Commented Dec 30, 2013 at 12:53
  • 1
    This example is an answer. Commented Dec 30, 2013 at 12:54

7 Answers 7

3

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
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for mentioning 1 , 2.
References are fine for polymorphism as well.
3

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

1 Comment

Assuming that testTwo is a pointer, not a class (with defined operator * and/or ->).
1

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

Comments

1

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.

1 Comment

Hmm I see that clarified some things for me, thanx.
0

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.

Comments

0

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.