Interfaces

Hi,

i'm pretty new to c++ i usually code in c# but i wanted to learn more about c++ so i'm writing a small game. i want to make it able to render in directx and opengl, and if i wanted to do something like that i would do something like this in c#

C# this works just fine in c#
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
  sing System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TextCS
{
    interface IRender
    {
        void Init();
        void Input();
        void Render();
    }

    class DirectX : IRender
    {
        public void Init()
        {
        }

        public void Input()
        {
        }

        public void Render()
        {
        }

        public void Dispos1()
        {
        }
    }

    class OpenGL : IRender
    {
        public void Init()
        {
        }

        public void Input()
        {
        }

        public void Render()
        {
        }

        public void Dispos2()
        {
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            IRender Ren;

            if (true)
            {
                Ren = new DirectX();
            }
            else
            {
                Ren = new OpenGL();
            }

            Ren.Init();
            while(true)
            {
                Ren.Input();
                Ren.Render();
            }

            if (Ren.GetType() == typeof(DirectX))
            {
                ((DirectX)Ren).Dispos1(); //this is my question
            }
            else
            {
                ((OpenGL)Ren).Dispos2(); //this is my question
            }
        }
    }
}



now i have been testing in c++ and i cant quite get it right.
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
  #include <iostream>
 
using namespace std;
 
// Base class
class IRender 
{
public:
   // pure virtual function providing interface framework.
   virtual void Init() = 0;
   virtual void Input() = 0;
   virtual void Render() = 0;
protected:
};
 
// Derived classes
class DirectX: public IRender
{
public:
        void Init()
        {
        }

        void Input()
        {
        }

        void Render()
        {
        }

        void Dispos1()
        {
        }
};

class OpenGL: public IRender
{
public:
        void Init()
        {
        }

        void Input()
        {
        }

        void Render()
        {
        }

        void Dispos1()
        {
        }
};
 
int main(void)
{
    IRender* Ren;
 
	if (true)
	{
		Ren = new DirectX();
	}
	else
	{
		Ren = new OpenGL();
	}

    Ren->Init();
    while(true)
    {
        Ren->Input();
        Ren->Render();
    }

	// How would i figure out what type it is?
	 ((DirectX)Ren).Dispos1(); // how would i access Dispos1?

	 ((OpenGL)Ren).Dispos2(); // how would i access Dispos2?

   return 0;
}


Ho would one figure out what type it is and access Dispos1 and or Dispos2?
You could use either static_cast or dynamic_cast

Use a static_cast only if you are sure the IRender pointer is pointing to an instance of the target class; otherwise use dynamic_cast, which is allowed to fail (in the pointer case, the result of the cast is a null pointer if the class of the object pointed to is not the one that is wanted).

1
2
3
4
5
6
7
8
	OpenGL pOGL = dynamic_cast<OpenGL*>(Ren);
	if(pOGL != 0) {
		pOGL->Dispos2();
	} else {
		DirectX pDX = dynamic_cast<DirectX*>(Ren);
		if(pDX != 0)
			pDX->Dispos1();
	}


See Type Casting
http://www.cplusplus.com/doc/tutorial/typecasting/

But the names Dispos1 and Dispos2 sounds like they do the same kind of thing. So why not make Dispos a virtual function like the others?

Andy
Last edited on
This was a setup more to prove a point, how do i accomplish this, I might need to do some extra steps and this would be how i could accomplish this.

thanks for the info :}

Of course i might be doing this all wrong and if you have a better suggestion i am willing to hear it.
Last edited on
Topic archived. No new replies allowed.