0
Can some give a detail explanation on pointers to object
9 Answers
+ 2
Keep going thru your C++ course, you have to complete more than 6% to get to pointers
+ 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
+ 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?
0
It seems that pointer is not the point here...
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;
}
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