Object pointer

Hello, everyone!
When creating an object of a class, how do I create a pointer to that object so that multiple other classes can access the same object (in this case: the other classes are inheriting from the class that contains the object). Code and scenario can be supplied upon request if this is needed (I would do now, but I don't know if it is or is not possible without this).
Thanks.
Last edited on
1
2
3
4
5
// Create an object
someObjectType anInstance;

// Create pointer to the object I just made
someObjectType* pToAnInstance = &anInstance;
That doesn't sound like a situation where a pointer is warranted.

If the base class contains an object, it is guaranteed to exist for the entire lifetime of the derived class, therefore the derived class doesn't need to check if the pointed object exists, therefore, it's sufficient to use a reference.
I request the code and scenario, please.
You may not actually need direct access.

For what I read, it seems that you don't know the difference between an object and a class.
A reference is what I meant. Sorry for my lack of specification, though - I am fairly new to this (and quite tired), so I occasionally make mistakes in naming what I want to do. Yes, I do know the difference between an object and a class (again, bad wording).

Scenario:
I am making a (console based) RPG. I wish to have a reference in the base class to the map object because if there is just a declaration of the map object, every derived class will have it's own map.

Code:
The code can be found at the following link (there are several other errors in the code (I only learned that developers plan code after I had started and had to restructure the game half way through, as well as underestimating the project's complexity, so I had to make some changes (that are yet to be finished (once I can deal with this and the problem just to be mentioned))), namely being due to the LivingObject class not recognizing that GameObject is a class. I think that it is due to a circular dependency, but I may be wrong. If anyone else sees what the problem is, could you please tell me? I can deal with the rest of the problems.):
http://www.4shared.com/zip/7kmznpvE/Text_Based_RPG_1.html


Thanks everyone (sorry if this is a bit vague, I can try to explain better if this makes too little sense to be of any use).
closed account (4z0M4iN6)
Moschops showed this example:

1
2
3
4
5
// Create an object
someObjectType anInstance;

// Create pointer to the object I just made
someObjectType* pToAnInstance = &anInstance;



You also could do it this way:


1
2
3
4
5
6
7
8
9
10
11
// global space -------------

MyClass * pMyObject;


// functions -----------------------

void MyObjectInitialization(void)
{
     pMyObject = new MyClass;
}


And don't forget to call somewhere at the end: delete pMyObject

And you also could have an array, a vector, a list or what else ever of such pointers.
I tried to implement this inside the class (initializing the pointer to the object in the constructor, so it was done correctly) and all of the previous error messages disappeared (to be replaced with a really long one that I couldn't decipher (extremely long and complex)). I then moved the map object (and the relevant pointer to the object to the global namespace) and got 6 errors, stating: "error: request for member 'xxxxxxx' in 'MpoP', which is of non-class type 'MapFunctions*'" ('xxxxxxx' being a function). Could anybody please correct this for me?

Updated code is available here:
http://www.4shared.com/zip/8zH8w2Pq/Text_Based_RPG_2.html
And why would anybody on the earth download some zipped files.

That why this forum has code tags to show the code, isn'it?
codekiddy, didn't you post your own .rar file for someone to download in the C++ forum? I'm not sure what the difference is between someone else posting a hosted file and you posting a hosted file, but try not to rag on someone for doing the exact same thing you did...
Hi Kazekan,
Yes you're completly right but that's different, the thing which I uploaded was something that can't be represented using code tags, so the only way was to upload it.
Oops, forgot that (stupid me)! The code is here:
GameObject.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef GAMEOBJECT_H
#define GAMEOBJECT_H

#include "MapFunctions.h"

using namespace std;

MapFunctions Mpo;
MapFunctions *MpoP = &Mpo;

class GameObject
{
public:
    GameObject();
    void DirectionCheck(int tlocx, int tlocy);
    ~GameObject();
};

#endif // GAMEOBJECT_H 


MapFunctions.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#ifndef MAPFUNCTIONS_H_INCLUDED
#define MAPFUNCTIONS_H_INCLUDED

#include <iostream>
#include <cstdlib>
#include <new>
#include <string>
#include <sstream>
#include <fstream>
#include "Enemy.h"

using namespace std;

class MapFunctions
{
public:
    MapFunctions();
    void MapRead(); // Get a map from a file.
    int ReadMap(int xvalue, int yvalue); // Read one part of the map.
    void WriteMap(int xvalue, int yvalue, int writevalue); // Write a new value to one part of the map.
    void MapDelete();
    int getLevel();
    int getRows();
    int getCols();
    void setLevel();
    ~MapFunctions();
protected:
    int level;
private:
    int ** Map;
    int rows, cols, StartTextSize;
    string StartText;
};

#endif // MAPFUNCTIONS_H_INCLUDED 


GameObject.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include "GameObject.h"

GameObject::GameObject()
{
    //ctor
}

//Works out the directions in which the player may travel.
void GameObject::DirectionCheck(int tlocx, int tlocy)
{

    bool DirectionSpecifier[4];
    int DirectionSpecifierCount = 0;

    if(tlocy<Mpo.getRows())
    {
                if(Mpo.ReadMap(tlocx,tlocy+1)!=0)
        {
            DirectionSpecifier[North]=1;
            DirectionSpecifierCount++;
        }
    }
    else
    {
        DirectionSpecifier[North]=0;
    }
    if(tlocx<Mpo.getCols())
    {
        if(Mpo.ReadMap(tlocx+1,tlocy)!=0)
        {
            DirectionSpecifier[East]=1;
            DirectionSpecifierCount++;
        }
    }
    else
    {
        DirectionSpecifier[East]=0;
    }
    if(tlocy>0)
    {
        if(Mpo.ReadMap(tlocx,tlocy-1)!=0)
        {
            DirectionSpecifier[South]=1;
            DirectionSpecifierCount++;
        }
    }
    else
    {
        DirectionSpecifier[South]=0;
    }
    if(tlocx>0)
    {
        if(Mpo.ReadMap(tlocx-1,tlocy)!=0)
        {
            DirectionSpecifier[West]=1;
            DirectionSpecifierCount++;
        }
    }
    else
    {
        DirectionSpecifier[West]=0;
    }
    for(int i=0; i<4; i++)
    {
        if(DirectionSpecifier[i]!=0 && DirectionSpecifierCount>2)
        {
            switch(i)
            {
            case 0:
                cout << "north, ";
                break;
            case 1:
                cout << "east, ";
                break;
            case 2:
                cout << "south, ";
                break;
            case 3:
                cout << "west, ";
                break;
            }
            DirectionSpecifierCount--;
        }
        else if(DirectionSpecifier[i]!=0 && DirectionSpecifierCount>1)
        {
            switch(i)
            {
            case 0:
                cout << "north and ";
                break;
            case 1:
                cout << "east and ";
                break;
            case 2:
                cout << "south and ";
                break;
            case 3:
                cout << "west and ";
                break;
            }
            DirectionSpecifierCount--;
        }
        else if(DirectionSpecifierCount=1 && DirectionSpecifier[i]!=0)
        {
            switch(i)
            {
            case 0:
                cout << "north.";
                break;
            case 1:
                cout << "east.";
                break;
            case 2:
                cout << "south.";
                break;
            case 3:
                cout << "west.";
                break;
            }
            DirectionSpecifierCount--;
        }
    }
}

GameObject::~GameObject()
{
    //dtor
}


I know that this program's structure is a bit confused: I only found out that programmers plan code after starting and I have nearly restructured the program to a plan, but I need to ensure that each relevant class can get to the same map object first before doing it.nI also underestimated the project's complexity when starting it.

Is this enough information? If not, I can post more code.
Topic archived. No new replies allowed.