How to move methods from ancestor to descendant

I work with one project which uses abstract methods. And I wanted to try something. I wanted to move the abstract methods from ancestor to descendant as it seemed to me redundant to use abstract methods.

First I give a schema:

Ancestor class is called ECBase (means Effect/Condtitions).
Effects and Conditions have very similar format and there are classes Effect and Condition which can read or write effects to file.
Classes Effects and Conditions inherits publicly from ECBase,

In the ECBase there were these shared methods originally:
1
2
3
4
virtual std::string getName() const = 0;
virtual int getPlayer() const = 0;
virtual void setPlayer(int player) = 0;
virtual void tobuffer(Buffer &b) const = 0;

Now I removed them and created these methods in the Effect and Condition classes:
1
2
3
4
std::string getName();
int getPlayer() const;
void setPlayer(int player);
void tobuffer(Buffer &b);


When I try to debug, I have error in Trigedit class which works either with Class type instance or Effect instance (which is same block if data prepared before it will be written to file).

In the original Trigger there was instance ECBase& source with member tobuffer which first, erases buffer and then it will save the data to buffer.

This is how the source was defined:
1
2
3
4
ECBase& source =
	(data->type == CONDITION) ?
	static_cast<ECBase&>(t->conds[data->index]) :
	t->effects[data->index];


Now I get to the problem. Becase the ECBase no longer contains these members, I cannot refer to ECBase. I try to remake it so, that I could call either Effect's member of Condition's member.

I tried this:
1
2
3
4
5
6
if (data->type == CONDITION)
			// Condition& source = reinterpret_cast<Condition&>(t->conds[data->index]);
			Condition& source = t->conds[data->index];
if (data->type == EFFECT)
			// Effect& source = reinterpret_cast<Effect&>(t->effects[data->index]);
			Effect& source = t->effects[data->index];


But none of this tries works. Now I have error:

error C2065: 'source' : undeclared identifier
error C2228: left of '.tobuffer' must have class/struct/union

on this line:
source.tobuffer(nullbuff);

This is source of ECBase:
http://paste.ofcode.org/32azWX4uUfEggbH4PmYVvPi

Have you some idea how could I solve the problem? To call the methods when they were moved from the Ancestor class to descendant class...
Last edited on
Looks like you are declaring the source reference inside of the if statement so if you check it outside it is no longer in scope. You may have to move the tobuffer() call into the if statement after the assignment.

But I'd like to ask: why are you replacing the dynamic dispatch method with basically a manual version of the same thing? Now you need to check a class tag every time you want to use an ECBase object reference and see which kind you really have.
To rewrite your code a little without technically changing it:
1
2
3
4
5
6
7
8
9
10
11
if (data->type == CONDITION)
{
			// Condition& source = reinterpret_cast<Condition&>(t->conds[data->index]);
			Condition& source = t->conds[data->index];
}

if (data->type == EFFECT)
{
			// Effect& source = reinterpret_cast<Effect&>(t->effects[data->index]);
			Effect& source = t->effects[data->index];
}


The scope of source is the if block. Outside the curly braces, source does not exist.

The further you go down this road, the clearer it will become why the abstract class was used in the first place.
Topic archived. No new replies allowed.