Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ class object pointers and accessing member functions

I'm bit new to C++ and try to work things with Qt and came across this confusing thing:

The concepts on various tutorials state something like:

Class *obj;

*obj - will display the value of object stored at the referenced memory
obj - will be address of memory to which it is pointing

so, I would do something like

*obj=new Class();

but if I want to access a function, I have to do obj->function1(); instead of *obj->function1();

-- not sure why, since with normal objects [ normalObj.function1();] would work, since that's the value directly.

So, for pointer objects why do we use memory reference to access the function, or is it that in case of normal objects also, its always references

P.S: Can someone guide me to a good tutorial of usage of pointers in C++, so that my queries like these can be directly addressed in it.

like image 667
rock_win Avatar asked Jul 07 '12 07:07

rock_win


People also ask

How do you access member functions using pointers?

To access a member function by pointer, we have to declare a pointer to the object and initialize it (by creating the memory at runtime, yes! We can use new keyboard for this). The second step, use arrow operator -> to access the member function using the pointer to the object.

How do you call a member function using an object?

Using a pointer-to-member-function to call a function Calling the member function on an object using a pointer-to-member-function result = (object. *pointer_name)(arguments); or calling with a pointer to the object result = (object_ptr->*pointer_name)(arguments);

Which operator a pointer object of a class uses to access its data members and member functions?

Which operator a pointer object of a class uses to access its data members and member functions? a) . Explanation: ->(arrow operator) is used by a pointer object to access members of its class.

Can we call member function using this pointer?

Function pointer to member function in C++ In C++ , function pointers when dealing with member functions of classes or structs, it is invoked using an object pointer or a this call. We can only call members of that class (or derivatives) using a pointer of that type as they are type safe.


2 Answers

The * symbol is used to define a pointer and to dereference a pointer. For example, if I wanted to create a pointer to an int, I could do:

int *ptr;

In this example, the * is being used to declare that this is a pointer to an int. Now, when you are not declaring a pointer and you use the * symbol with an already declared pointer, then you are dereferencing it. As you probably know, a pointer is simply an address. When you dereference a pointer, you are obtaining the value that is being pointed to by that address. For example:

int pointToMe = 5;
int *ptr = &pointToMe;
std::cout << *ptr;

This will print out 5. Also, if you are assigning a pointer to a new address and it's not in the declaration, you do not use the * symbol. So:

int pointToMe = 5;
int *ptr;
ptr = &pointToMe;

is how you would do it. You can also deference the pointer to assign a new value to the value being pointed to by the address. Such as:

int pointToMe = 5;
int *ptr = &pointToMe;
std::cout << *ptr; // Prints out 5
*ptr = 27;
std::cout << *ptr; // Prints out 27

Now, -> acts like the deference symbol. It will dereference the pointer and then use the member functions and variables as if you had used . with a non-pointer object. Even with an object that is not a pointer you can use the -> by first getting the address:

CObj object;
(&object)->MemberFunction();

That's just a brief overview of pointers, hope it helps.

like image 140
Yokatta24 Avatar answered Oct 01 '22 03:10

Yokatta24


You can use the "normal" . to access the objects members, but you have to dereference the pointer first.

Due to operator precedence, this will look like (*obj).member. For those who think this is too much to write, obj->member is a shorter alternative.

If you have an object c of type Class, *c.ptr means dereferencing a pointer ptr that is a member of Class. That is one reason for (*obj).member, which means something else.

like image 24
Bo Persson Avatar answered Oct 01 '22 03:10

Bo Persson