Using a pointer to point to an object

Hello everyone, this is my first post, and I would like to know how I can use a pointer to point to an object itself so that I can call a function from the object, such as *pointer.do_blah(), and then be able to re-use that information.

I've tried experimenting with it a bit, but I usually wind up getting an error like this stating:
request for member `get_id' in `obj_stack[d]', which is of non-class type `int*[2]'

If it would help at all, I provided the code for what I'm working on at the bottom, which I am hoping to make a generic game objects set with (ie the one from Game Maker rofl; Game Maker in C++? Perhaps the GMC will stop whining then :P jk).

At any rate, thanks for reading this, and if this is in the wrong section, please let me know (or if you're a mod, could you kindly move it to the appropriate section? Thanks!). Well, now I'm off again to attempt fixing it.

Here's the code:

#include <iostream>

using namespace std;

int *obj_stack[5];

class obj{
int x, y, id;
public:
void set_pos(int a, int b){
x = a;
y = b;
}
void set_id(int a){
id = a;
obj_stack[id] = &this;
}
int get_x(){
return x;
}
int get_y(){
return y;
}
int get_id(){
return id;
}
};

int main(){
obj c;
int d, e;
cout <<"Assign 0-4 for ID.\n";
cin >> d;
cout << "\n\n";
c.set_id(d);
e = *obj_stack[d].get_id();
cout << e;
cout << "\nPress Enter to exit.";
cin.get();
}
Your obj_stack is an array of 5 pointers, as you want. However those pointers point to int not to obj

Furthermore, you never actually make them point to anything. IE, you don't have any obj objects.

Are you sure you want pointers here? Why not just have an array of objects?
Hi, thanks for the reply. I hadn't realized that it was pointing to int, and not obj, that was a pretty silly mistake, but I was trying to have obj_stack store pointers to each object, and I thought I had accomplished that with set_id(), specifically with this:

obj_stack[id] = &this;

Perhaps I was mistaken about the 'this' keyword, pointers have a habit of confusing me :( , but keyword 'this' is a pointer to the object itself, no? I know I had once before tried making a chain of pointers, but I didn't have much success then, so maybe this is a more advanced outcome which is similar to what I had then.

Anyway, I apologize for the rant, but yes, I would like to use pointers, unless an array of objects would be easier, in your opinion. I think I might do that eventually, just to get rid of any unnecessary pointer sections, but I would like to try a solution to this. Well, now I am going to attempt a solution based on what you said, and again, thanks for responding!

Edit:

I'm sorry, I just realized I hadn't answered your last question (its rather late where I am and I'm getting tired, probably why I screwed this up, lol), but I hadn't wanted to use an array of objects because I thought that it might take a performance hit as I wanted to be able to use dynamic memory, and in order to expand the array, I would have to back it up to another temporary array, delete the old one, and then re-create it and rewrite it (at least that's how I understand it anyway, please correct me if I'm wrong or suggest a better method).
Last edited on
okay.. back up.

You need to understand a few things.

1) An "object" is an instance of a class. If you have a class:

1
2
3
4
class Car
{
  // stuff here
};


This allows you to create Car objects. This doesn't mean you HAVE Car objects, though, you still have to create them.

 
Car foo;


Here... 'foo' becomes a Car object, because you created it. That is... 'foo' actually exists as a Car.

Conversely:
 
Car* bar;


'bar' is not an object. It's a pointer. 'bar' does not actually exist as a Car. Therefore you don't have any car with that code.

What pointers do... is they tell you where to find an existing Car. So:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Car foo;  // foo is actually a Car
Car* bar;  // bar is not a Car, it's a pointer

bar = &foo;  // with this... bar points to foo

// let's drive that car
bar->Drive();  // since 'bar' points to 'foo'
              // this is the same as doing:  foo.Drive();
              // IE:  they're both referring to the same Car:  foo

// here's where you have a problem:
Car* broken;  // a pointer, not pointing to anything
broken->Drive();  // BAD

/*
  because 'broken' does not actually point to a Car.. what exactly are you trying to Drive?
  This is an uninitialized / bad pointer.  These bugs are a big problem beause they compile without
  any problem, but they cause all sorts of runtime errors/crashes.
*/


Nowhere in your code are you actually creating objects. Therefore your pointers have nothing to point to. this does not create an object.

The "this" pointer gives you a pointer to the object on which the function was called. For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Car foo;
foo.Drive();

/*
  Because we're driving 'foo', in the Drive function..
  this == &foo
  That is... 'this' points to the object being acted on.

  You're doing the following
*/
Car* bad;
bad->Drive();

/*
  in this situation:   this == bad
  but again, 'bad' is a bad pointer because it doesn't point to anything.
  and because 'bad' is a bad pointer, that means 'this' is a bad pointer.
 */


Does that make sense? Hopefully that'll shed some light on the subject.

I really don't think you need pointers here.
Last edited on
But I did declare an instance of class obj,
obj c;
is the first line of main(). I'm sorry if that wasn't very obvious, my coding can be a bit hard on the eyes at times, and its a tiny line, but I did declare an instance of it :). But onto the 'this' part; should I perhaps be calling c.set_id() with 'this' as the argument (obviously without the quotes)? I'll try this out.

Edit:

Eh, sorry about that, but I did forget to mention that I was intending this particular project as more of a base for understanding how to make an object indexing system with dynamic memory. Thanks for the assistance, though, Disch. It is a pretty simple project on its own:).
Last edited on
omg, I'm sorry. You're totally right. For some reason I completely missed that! XD

My apologies. It is pretty late, after all </lame excuse>

Ah well.. nevermind then. ^^
Ah, lol, that's alright, I almost certainly would have done the same thing! :) But I did figure out what the problem was! In addition to the pointer not being of the same time (lol) I was calling functions like pointer.funct(), I guess maybe it needs to be ->? At any rate, between that and a couple of other little things I did, I got it fixed, and don't get me wrong, I do appreciate the assistance (and even if it is a lame excuse, I think its still valid [time of day]).

Here is the (more or less) working code:

#include <iostream>

using namespace std;

class obj{
int x, y, id;
public:
void set_pos(int a, int b){
x = a;
y = b;
}
void set_id(int a){
id = a;
}
int get_x(){
return x;
}
int get_y(){
return y;
}
int get_id(){
return id;
}
};

obj *obj_stack[5];

int main(){
obj c;

int d, e;
cout <<"Assign 0-4 for ID.\n";
cin >> d;
cout << "\n\n";
c.set_id(d);
obj_stack[d] = &c;
cout<<"\nNo crash?";

cout<<"\nStill none?";
cout << obj_stack[d]->get_id();
cout << "\nPress Enter to exit.";
cin.get();
cin.get();
}
Topic archived. No new replies allowed.