Inheritance question

I dont really know what my problem is called so if you have a better subject name pleas tell me.

i think the best way to explain is to show you, the first is my c# example that works just fine, and my second is the c++ example that does not work.

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
using System;

class Program
{
    static void Main(string[] args)
    {
        Test1 test = new Test2();

        test.say1();
        ((Test2)test).say2();

        Console.ReadLine();
    }
}

public class Test1
{
    public void say1()
    {
        Console.WriteLine("MyClass");
    }
}

public class Test2 : Test1
{
    public void say2()
    {
        Console.WriteLine("MyClass2");
    }
}


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
#include <iostream>

using std::cout;
using std::endl;

class test1
{
public:
	virtual void say1()
	{
		cout << "say 1" << endl;
	}

private:

};

class test2 : public test1
{
public:
	void say2()
	{
		cout << "say 2" << endl;
	}

private:

};

int main()
{
	test1 t = test2();

	t.say1(); // i can access 
	((test2)t).say2(); // error: cannot convert from 'test1' to 'test2'

	return 0;
};


problem is found on line 35 of c++ example. i would like to know why this does not work and how one could fix this.
1
2
3
4
5
6
int main(){
   test2 obj;
   test1 *pointer = &obj;
   pointer->say1();
   dynamic_cast<test2*>(pointer)->say2();
}


When you do test1 t = test2(); you'll observe object slicing.
Also, if you have a virtual member function, then your destructor should be virtual too.
is this good practice? doing it like this?

when i was fooling around with xna i would create a general person class and npc, enemy's and even the character would derive from that class, i would then store it in one big array(list).

but it seems that in c++ it works a bit differently. could you suggest maybe a better way to accomplish this in a better way?
> is this good practice? doing it like this?
¿casting? no, I don't think so

> i would create a general person class and npc, enemy's and even the character would derive from that class,
> i would then store it in one big array(list).
if you pretend to threat them all as `general person' instead of going to the specific of each class, then it is fine.
But if you are going to test the type, then you are aware that that type exists, and you could have another collection for them.

> could you suggest maybe a better way to accomplish this in a better way?
I have no idea what you are trying to do
>I have no idea what you are trying to do

ok let me explain my current situation, for school i am required to make a game in the console window. i started off buy making menus.

i have a base menu class with some functions for changing the selected menu and some other stuff. now every menu draws its stuff differently so when i create a new menu class i derive from the base menu class that way i only need to change the draw function.

but now i have let say 7 menus and it's overhead in lines of code is pretty messy so i wanted to store the classes in a array so i could then iterate through to get the one i need, instead of 14 if statements. i c# it is no problem doing it the way i mentioned but as you sed in c++ its a different story.

so this is my question, how would you handle/ manage the classes to accomplish this.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class menu{
public:
   virtual void draw();
};
class classic_menu: public menu{
public:
   virtual void draw(){
      std:;cout << "Classic\n";
   }
};
//overload the virtual member function `draw()'
class keyboard_driven_menu : public menu{};
class simple_menu : public menu{};
class advanced_menu : public menu{};


std::vector< std::unique_ptr<menu> > Menus;
Menus.emplace_back( new classic_menu(/**/) );
Menus.emplace_back( new keyboard_driven_menu(/**/) );
Menus.emplace_back( new simple_menu(/**/) );
Menus.emplace_back( new advanced_menu(/**/) );

Menu[2]->draw();


If you only care about looks in the `draw()' function, another possibility would be
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class draw(){
public:
   virtual void render();
};
class simple_draw: public draw{};

class menu{
   std::unique_ptr<draw> d;
public:
   void draw(){ //note: no virtual
      d->render();
   }
};

std::vector<menu> Menus(5); //note: holding objects
Menus[3].set_draw_method( new simple_draw() );
Menus[3].draw();
Last edited on
Topic archived. No new replies allowed.