DamageCreature is being initialized as a Cards* DamageCreature, I want to initialize it as another pointer. If you guys need my header files added please say so, they are too large.
1 2 3 4 5
bool Creature::attackCreature(Player* opponent, int Creature2Attack)
{
auto DamageCreature = opponent->GetCardFromGround(Creature2Attack);
//local variable Cards* DamageCreature
}
It gives me the error that it can't be set to another pointer.
1 2 3 4
Creature* DamageCreature;
DamageCreature = opponent->GetCardFromGround(Creature2Attack);
//a value of type "Cards*" can't be assigned to an entity of type
//"Creature*"
pointers are strongly typed; you cannot just say
int x;
double * dp = &x; //no can do.
you can force the issue with casting, but all bets are off on how you access the data without going out of bounds in memory. The most common use of that is to cast to 1 byte types to handle the data byte-wise, which is sometimes useful for things like compression or writing to a binary file.
consider this as well:
struct one
{
char header[10];
int type;
double d;
};
struct two
{
char header[10];
int type;
string s;
};
if you cast a pointer of one to two or two to one, you can access the first two items correctly because they are identical, and the third items and beyond are not safe to access.
if you have a situation like that, just cast the pointer, but you need to be sure you know what you are doing. This kind of coding is more C-ish and it is very rare in C++ to need to resort to this kind of thing.
anyway, you can force it with a cast. What happens after that is up to you!
@jonnin I understand but in my code the "Cards" class is the base class for all other classes, so what I don't understand is why the pointer is being initialized as "Cards*" without inheriting anything ?
Can you explain? or Do I have the inheritance thing all wrong lol
You can 'go up' from derived towards the base (upcast) but can't 'come down' away from the base (downcast) without using a cast.
If Cards is the base class and from the first post DamageCreature is Cards* and Creature is derived from Cards then you can't assign a type Cards* to Creature*. As keskiverto says above, you need to use a dynamic_cast.