Initializer lists

Hello,

I am trying to initialize the base class. I would like to use an intializer list, but the class is not imediatly descended from the base class (there is another between.

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

cCharacter(int Health, int AttackRating, int DefenseRating, int Initiative, int MeleeDamage, string Name):
		myHealth(Health),
		myAttackRating(AttackRating),
		myDefenseRating(DefenseRating),
		myInitiative(Initiative),
		myMeleeDamage(MeleeDamage),
		myName(Name),
		myWeapon(0),

		myBody(0),
		myQuickness(0),
		myStrength(0),
		myCharisma(0),
		myIntelligence(0),
		myWillpower(0),
		myEssence(0),
		myMagic(0),
		myLuck(30)
		{
			myPosition.x = 0;
			myPosition.y = 0;
		};

class cEnemy : public cCharacter
{
public:

	cEnemy():
	myChanceOfMinions(0)
	{};
	virtual ~cEnemy(){};

	int  GetMyChanceOfMinions(){return myChanceOfMinions;};

	void DrawImage()
	{
		if(myImage.Buffer)
			cout.write(myImage.Buffer, myImage.Length);
	};

	int myChanceOfMinions;
	ASCIIART myImage;
};


class cSkeleton : public cEnemy
{
public:
	cSkeleton()
	{
		SetName("Skeleton");
		SetAttackRating(6);
		SetDefenseRating(4);
		cSword* s = new cSword;
		GiveWeapon(s);
		SetHealth(40);
		LoadASCIIArt("skeleton.txt", &myImage);
		SetIntiative(2);
	}
	~cSkeleton(){};

private:

};


I want to initialize the base class cCharacter at the cSkeleton level.

Would I need to use an initialize list here? And if so, how do i implement them?

I know how to do this with one level, but not at two or more.

> I want to initialize the base class cCharacter at the cSkeleton level.
> I know how to do this with one level, but not at two or more.

To explicitly initialize the indirect base class object, it must be derived from virtually. The two options are:
a. pass cCharacter constructor arguments through cEnemy::cEnemy
b. Make cCharacter a virtual base class and initialize it directly from cSkeleton

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
struct cCharacter
{
    explicit cCharacter( int a = 1, int b = 2 ) : aa(a), bb(b) {}
    virtual ~cCharacter() {}

    const int aa, bb ;

    // ...
};

namespace option_one
{
    struct cEnemy : cCharacter
    {
        cEnemy( int a, int b, int c ) : cCharacter(a,b), cc(c) {}

        const int cc ;

        // ...
    };

    struct cSkeleton : cEnemy
    {
        cSkeleton( int a, int b, int c, int d ) : cEnemy(a,b,c), dd(d) {}

        const int dd ;

        // ...
    };
}

namespace option_two
{
    struct cEnemy : virtual cCharacter
    {
        cEnemy( int c ) : cc(c) {}

        const int cc ;

        // ...
    };

    struct cSkeleton : virtual cCharacter, cEnemy
    {
        cSkeleton( int a, int b, int c, int d ) : cCharacter(a,b), cEnemy(c), dd(d) {}

        const int dd ;

        // ...
    };
}
I didn't know that you could create a virtual class.

I could do this:

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

using namespace std;

class Base
{
public:

	Base(int v):
	  size(v)
	{
		cout<<"Base Constructor"<<endl;
		cout<<"Size "<<size<<endl;
	};
	~Base()
	{
		cout<<"Base Destructor"<<endl;
	};

	int size;
};

class Middle : public Base
{
public:
	Middle(int v):
	  Base(v)
	{
		cout<<"Middle Constructor"<<endl;
	};
	~Middle()
	{
		cout<<"Middle Destructor"<<endl;
	};
};

class Top : public Middle
{
public:
	Top(int v):
	  Middle(v)
	{
		cout<<"Top Constructor"<<endl;
	};
	~Top()
	{
		cout<<"Top Destructor"<<endl;
	};
};

int main()
{
	Top* t = new Top(6);
	delete t;
	system("PAUSE");
	return 0;
};



Base Constructor
Size 6
Middle Constructor
Top Constructor
Top Destructor
Middle Destructor
Base Destructor
Press any key to continue . . .


What would the benefit be to creating a virtual class?
Last edited on
If i do this:

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

using namespace std;

class Base
{
public:

	Base(int v):
	  size(v)
	{
		cout<<"Base Constructor"<<endl;
		cout<<"Size "<<size<<endl;
	};
	~Base()
	{
		cout<<"Base Destructor"<<endl;
	};

	void PrintSize(){cout<<"my size is "<<size<<endl;};

	int size;
};

class Middle : public Base
{
public:
	Middle(int w):
	  width(w)
	{
		cout<<"Middle Constructor"<<endl;
	};
	~Middle()
	{
		cout<<"Middle Destructor"<<endl;
	};
	void PrintWidth(){cout<<"My Width "<<width<<endl;};
	int width;
};

class Top : public virtual Middle, virtual Base
{
public:
	Top(int v, int w):
	  Middle(w),
	  Base(v)
	{
		cout<<"Top Constructor"<<endl;
	};
	~Top()
	{
		cout<<"Top Destructor"<<endl;
	};
};

int main()
{
	Top* t = new Top(6, 8);
	t->PrintSize();
	t->PrintWidth();
	delete t;
	system("PAUSE");
	return 0;
};


I don't think i am doing this correctly.
Last edited on
> I could do this:

Yes. That would be option_one.


> I don't think i am doing this correctly.

No, you are not doing it correctly. It wouldn't even compile for more than one reason.


> What would the benefit be to creating a virtual class?

Virtual base classes are useful (immensely useful) when:

a. In a multiple inheritance scenario, the virtual base class is indirectly inherited through more than one direct base class.

b. In addition to combining behaviour of the direct base classes (for example, std::iostream combines the behaviour of std::istream and std::ostream), we also want to share the state of the virtual base class (for example both, std::istream and std::ostream inherit std::ios virtually).

See: http://en.wikipedia.org/wiki/Virtual_inheritance
Last edited on
Topic archived. No new replies allowed.