Can some give a detail explanation on pointers to object | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can some give a detail explanation on pointers to object

7th Apr 2021, 1:38 PM
Rangu Sanjay
Rangu Sanjay - avatar
6 Answers
+ 2
Keep going thru your C++ course, you have to complete more than 6% to get to pointers
7th Apr 2021, 1:54 PM
Michal Doruch
+ 2
Martin Taylor i guess it is, if you are for example making some RPG game, and player is able to change his form, then you can just point "player" class pointer to preffered form. I'm not sure if it makes any sense, but i would probably try it that way
8th Apr 2021, 8:18 PM
Michal Doruch
+ 2
Martin Taylor, yeah classes and struct are very similar. An instance of a struct and an instance of a class are also very similar. You could say they're both objects. A reference and a pointer are very similar too. Aside from a reference never being null, I can't think of much of a meaningful difference. Is a null reference the distinction you're making? You think a pointer to NULL offers no actual benefit?
8th Apr 2021, 10:09 PM
Josh Greig
Josh Greig - avatar
0
It seems that pointer is not the point here...
7th Apr 2021, 2:54 PM
Tomas Konecny
0
Here is c++ source for a program with pointers to objects. It has a pointer to an int and a pointer to an instance of a class which is also known as an object. #include <iostream> using namespace std; class Point { public: int z=0; }; int main() { int x = 5; int * y = &x; // y is a pointer. // y has the address of x. // In other words, y is a pointer to x. cout << (*y) << endl; Point p; // instance of a class. p.z=3; Point * objectPointerToP = &p; cout << "z = " << (objectPointerToP->z); return 0; }
7th Apr 2021, 9:23 PM
Josh Greig
Josh Greig - avatar
0
Martin Taylor yes, you could use 'player' class, but if player can change it's form, it is easier to have "current form" object, and "optional form" object, and point to another form than changing all it's properties at runtime. Let's think of Druid class. So you have that abstract base class 'player', which can take multiple shapes. Next, you can create classes, for example 'druid', and 'bear' which are child classes of "player". Now you can declare and create these two objects (druid and bear), create pointer to player, and point to preffered object like this: druid d1(); bear b1(); player* p1 = nullptr; p1 = &d1; p1 = &b1; Does it makes sense? Xd
9th Apr 2021, 5:16 PM
Michal Doruch