Not the pointer needed

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
}
I want to initialize it as another pointer.

Then... set it to be equal to that other pointer, I guess. What is it that's confusing you?
Last edited on
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*" 
Last edited on
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
"Cards" class is the base class for all other classes

You say that Character inherits Cards. That every Character object IS-A Cards object.

However, every Cards is not a Character. (Probably.)

You can do:
1
2
3
4
Cards* c = GetCard...
if ( Character* p = dynamic_cast<Character*>(c) ) {
  // You can use p here, if c points to a Character object
}
Last edited on
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.

Consider:

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

class base1 {
	virtual void fun() {}
};

class derv1 : public base1 {
	void fun() override {}
};

int main()
{
	base1 b1;
	derv1 d1;

	base1* pb1 {&b1};
	base1* pb2 {&d1};

	derv1* pd2 {&d1};

	//derv1* pd1 {&b1}; Does not compile
	derv1* pd1 {dynamic_cast<derv1*>(&b1)};

	if (pd1 != nullptr)
		std::cout << "Dynamic cast failed\n";
}


This will compile and run without issue.

Perhaps (not tried):

 
auto DamageCreature {dynamic_cast<Character*>(opponent->GetCardFromGround(Creature2Attack))}; 


Last edited on
Topic archived. No new replies allowed.