C++ class object pointers and accessing member functions -
i'm bit new c++ , try work things qt , came across confusing thing:
the concepts on various tutorials state like:
class *obj;
*obj
- display value of object stored @ referenced memoryobj
- address of memory pointing
so, like
*obj=new class();
but if want access function, have obj->function1();
instead of *obj->function1();
-- not sure why, since normal objects [ normalobj.function1();
] work, since that's value directly.
so, pointer objects why use memory reference access function, or in case of normal objects also, references
p.s: can guide me tutorial of usage of pointers in c++, queries these can directly addressed in it.
the *
symbol used define pointer , dereference pointer. example, if wanted create pointer int, do:
int *ptr;
in example, *
being used declare pointer int. now, when not declaring pointer , use *
symbol declared pointer, dereferencing it. know, pointer address. when dereference pointer, obtaining value being pointed address. example:
int pointtome = 5; int *ptr = &pointtome; std::cout << *ptr;
this print out 5. also, if assigning pointer new address , it's not in declaration, not use *
symbol. so:
int pointtome = 5; int *ptr; ptr = &pointtome;
is how it. can deference pointer assign new value value being pointed 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 deference symbol. dereference pointer , use member functions , variables if had used .
non-pointer object. object not pointer can use ->
first getting address:
cobj object; (&object)->memberfunction();
that's brief overview of pointers, hope helps.
Comments
Post a Comment