C++ Derived Class Pointer

Hi,

How can I get a base class pointer?

Let's say the class is
1
2
3
4
5
6
Existence:Existence(Context *context)
{
 public:
 protected:
   Existence * ExistencePtr;
}


I would like to set a pointer of the class like in a running functions

1
2
3
4
int ExistenceMain(void)
{
        ExistencePtr = &Existence;
}


Vivienne
Last edited on
Don't quite understand what do you mean by getting the base class pointer.

If you are talking about the inheritance, the pointer of a derived class instance is the base class pointer itself.
Not much of your example makes sense. In line 1 of the first segment, I'm not sure what you are declaring. It seems to be a class, but it takes an argument, so it looks like a constructor (but you are missing one of the ':' in the scope operator). But then you declare public and private sections, so it looks like a class (except you forgot the 'class' keyword).

Since you say it is a class, I'll assume you meant to declare a class, and the end of line 1 was a mistake.

In your second segment you declare a function that is not a member function trying to set a data member of a class. But then you set the data member to the address of the class. You can only take the address of an object, not a class. Were you perhaps trying to use the "this" pointer?

If you clean up your example to indicate what you are trying to do, maybe we can answer your question better.
Last edited on
liuyang wrote:
If you are talking about the inheritance, the pointer of a derived class instance is the base class pointer itself.


Hi,

I think that statement is misleading - maybe you wrote it too quickly? Maybe you meant to say that a pointer to Derived is a valid pointer to Base? I am guessing you know all this already - so the stuff that follows is for the benefit of the OP. Also you might be quite right in seeing what the OP is trying to do, which is pretty good work considering the poorly expressed question from the OP.

@Vivienne
If one declares a function to take a pointer to a Base class as an parameter, then the function is called with an argument that is a pointer to an instance of a Derived class object, then all that happens is the compiler checks to see if Derived really is derived from the Base class. There is no conversion of the pointer to a Base pointer, the pointer is still a pointer to Derived.

One often wouldn't want it to be a pointer to Base anyway, for 2 reasons: The Base is often abstract - so that won't work; It's the pointer to Derived that one wants, so that one can call the Derived class function.


The beauty of this is that it can save one from writing lots of functions, just have class which has a virtual function which takes pointers to Base, then redefine that function in a derived class where necessary to achieve some specialisation.

For example one could have a Base class named Animal and it would have a pure virtual function named Speak();

Then there could be derived classes named Dog, Cow, Duck - each of which redefines the Speak() function to std::cout the sound that each respective animal makes.

Now create a std::vector of pointers to Animal. Then push_back pointers to the different types of Derived animals. Then cycle through the vector, calling the Speak function for each item in the vector :

item->Speak();

So the advantages here are:

We have one vector of Animals, not a different one for each different type of Animal. This allows us to add more types later on.

We have one Speak() function, not DogSpeak(), CowSpeak(), DuckSpeak(). Again this allows us to add more types later on.

So is something like the example above what you are trying to do?
I want the Singleton class to get the SharedPtr (applicationPtr) in the Existence class. In Urho3D api I'm using, I have a Game Handler. So, depending on a event it creates ExistenceStateLogin for example which is a derived class of Singleton. I'm avoiding all the classes being created basically messing up the memory.

I'm calling the other class by ExistenceClient * = new ExistenceStateLogin<>(); for example.


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
#ifndef EXISTENCECLIENT_H
#define EXISTENCECLIENT_H

using namespace Urho3D;
using namespace std;

class ExistenceClient : public ExistenceApp
{
    OBJECT(ExistenceClient);

public:
    /// Mention friend classes
    friend class ExistenceClientStateSingleton;
    friend class ExistenceClientStateMainScreen;
    friend class ExistenceClientStatePlayer;
    friend class ExistenceClientStateAccount;
    friend class ExistenceClientStateLogin;
    friend class ExistenceClientStateProgress;

    /// Construct.
    ExistenceClient(Context* context);
    virtual ~ExistenceClient();

    /// Setup after engine initialization and before running the main loop.
    virtual void Start();

    /// Return XML patch instructions for screen joystick layout for a specific sample app, if any.https://github.com/urho3d/Urho3D/tree/master/Source/Samples
    virtual String GetScreenJoystickPatchString() const
    {
        return
            "<patch>"
            "    <add sel=\"/element/element[./attribute[@name='Name' and @value='Hat0']]\">"
            "        <attribute name=\"Is Visible\" value=\"false\" />"
            "    </add>"
            "</patch>";
    }

    void Init(Context * context);

    /// Temporary online
    bool IsClientConnected(void);
    bool ClientConnect(void);
    bool SetServerSettings(void);

    /// Get subsubsystems
    Renderer * GetRenderSubsystems(void) const;
    UI * GetUISubsystems(void) const;
    Graphics * GetGraphicsSubsystems(void) const;
    ResourceCache * GetResourceCacheSubsystems(void) const;

    Window * GetSharedWindow(void) const;

    /// Debug test string
    int GetTestString(void)
    {
        return testvalue;
    }

    /// Base class get
    SharedPtr <ExistenceClient> GetApplicationPTR(void) const
    {
        return applicationPtr;
    }


    SharedPtr<ExistenceClient> applicationPtr;


protected:

    /// Scene Shared Pointer
    SharedPtr<Scene> scene_;
    SharedPtr<Scene> scenePlayerUI_;
    SharedPtr<Scene> sceneLoadingGameModeTransition_;



private:


};


/// Login State
class ExistenceClientStateSingleton : public LogicComponent
{
    OBJECT(ExistenceClientStateSingleton);
public:
    SharedPtr<ExistenceClient> baseclass;
    ExistenceClientStateSingleton(Context * context);
    virtual ~ExistenceClientStateSingleton();
    virtual void Enter();
    virtual void Exit();
    virtual void OnUpdate(Urho3D::StringHash eventType, Urho3D::VariantMap& eventData );
private:
    void Singleton(void);
protected:


};

#endif


Code

ExistenceClientStateSingleton::ExistenceClientStateSingleton(Context * context)
    :LogicComponent(context)
{

    /// ApplicationPtr is a  Urho3D::SharedPtr<ExistenceClient> in the Existence Class(Public)
    /// baseclass is a Urho3D::SharedPtr<ExistenceClient>

    /// singleton construct
    cout << "Debug: Singleton Constructor" << endl;


}
Last edited on
Topic archived. No new replies allowed.