m4rkus
m4rkus

Reputation: 91

C++ access members and functions

I'm fairly familiar with Java and C# and want to broaden my horizon with C++.

But I am having trouble to understand when to use the -> or the . operator when accessing a member or a method of an objcect.

I'm sure that I'm not the first one to ask this question. There are a lot of questions out there that tackle the same problem, but I just can't relate the provided answers to my special case.

I have a very simple class that is called Stock:

class Stock
{
public:
    int32_t value;
    string name;

    Stock(void);
    ~Stock(void);

    void doStuff();
};

I know that if the object is allocated on the heap I have to use -> and . when it resides in stack space.

But if I wanted to print the length of the name string of a Stock object I would need to do something like this:

Stock* bmw = new Stock;
bmw->name = "BMw";
cout << bmw->name.length() << endl;

The name of the stock is accessed via the "->" operator, the length of the name via the "." operator. In my view, both of these objects (the stock and the string) reside in heap space, so i would only need the -> operator, right?

What am I missing here? How is it with strings? I thougt strings are always pointers... Could you please explain the concepts that are working here a bit?

Thanks in advance for your replies.

Markus

Upvotes: 0

Views: 145

Answers (5)

Ajay
Ajay

Reputation: 18411

Welcome to the native world, ruled by C++ !

In C++, you need to use -> only when expression on left evaluates to a pointer of a class, struct or a union, otherwise use .. A function may also return a pointer to a object, and there you need to use -> operator:

Stock* GetStockByIndex(int);
GetStockByIndex(0)->value;

Usage of -> is not bound to new/heap, and usage of .is not bound to stack allocated object. It is just the context (expression on left).

In near future, you will also see that:

Stock s;
s->calculate();

How is that possible, you might wonder. It is because C++ allows -> to be overloaded!

Welcome!

Upvotes: 0

Ahmad Ab
Ahmad Ab

Reputation: 11

Just know that you use the arrow operator (->) when u point to an object. in your case, the name member is not a pointer to std::string, so you can't use the arrow operator, instead you use the dot (.).

you could do something like this :

std::string* name;
Stock* sptr = new Stock;
sptr->name = new std::string("blah blah blah");
std::cout << sptr->name->length << std::endl;

Upvotes: 1

D Stanley
D Stanley

Reputation: 152511

-> is used when you have a pointer, . is used when you have an instance.

In your example, bmw is a pointer to a Stock instance, so you reference it's members by using the -> operator. the name field is a string instance, so you use the . operator.

You could also write it as

(*bmw).name = "BMw";

Since *bmw returns the instance whose address is stored in the varialbe bmw

It's as simple as that - don't confuse yourself by bringing in the stack and heap. Those are just memory allocation implementation details.

Upvotes: 3

alireza sadeghpour
alireza sadeghpour

Reputation: 689

"use "->" for object that allocated in heap" is not accurate. Simply when you want access to members of an object via pointer,you must use"->". For other situation use ".".

Upvotes: 0

ravi
ravi

Reputation: 10733

bmw->name.length()

bmw is a pointer to your Stock object created on heap. So do access its member (like in your case name ) you have to use ->. After "bmw->name" would give you the string object. And that string object is not allocated using new means it's not on heap. So, do access it's member ( like length() in this case ) you have to use '.' .

Upvotes: 0

Related Questions