pointer to instance of class?

I'm working on a chess program. The way the program works now is sort of inefficient. Its easier to explain with an example.

I have classes for squares and for pieces. So lets say the user gives an input that indicates that he wants to manipulate one of the values of the piece on square 2x2. His input reads 2232. It indexes through every instance of every piece class looking for a piece that has xCoord==2 and yCoord==2.

1
2
3
(qPawn[0,1,ect].xCoord==2 && qPawn[0,1,ect].yCoord==2) ||
(qRook[0,1,ect].xCoord==2 && qRook[0,1,ect].yCoord==2) ||
ect...

Its not terribly inefficient except that i have to do it many times to check inputs to see if they conform to the myriad of rules. also its not just with checking inputs, there are a lot of places in the code where it would be nice just to manipulate or check the values of what ever pieces was attached to a given square.

I had the idea that maybe i could create one pointer for each square and point it at the piece that was contained on that square than i could get by with going through this indexing process only once per game loop.

Here is an example of the sort of thing i had in mind though of course it isnt working, i wouldn't be here if it was.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

class CClass
{
public:
	void print() {
		std::cout << "I want this to print" << std::endl;
	}
}qClass;

int main() {
	void *pClass = &qClass;
	*pClass.print();
	system("pause");
}

thanks for taking a look friends!
Last edited on
Due to order of operations, you need to use (*pClass).print();, though I would recommend simply
pClass->print();.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

class CClass
{
public:
	void print() {
		std::cout << "I want this to print" << std::endl;
	}
}qClass;

int main() {
	void *pClass = &qClass;
	(*pClass).print();
	system("pause");
}


still gives errors =(
Ah, I completely missed the fact that you declared pClass as a void pointer instead of a pointer to CClass.
1
2
//void *pClass = &qClass;
CClass *pClass = &qClass;
Its working now. thanks so much!
hey L B if you are still around. This functionality isnt working for what i need it to do. How do i make it so that i can reassign the pointer to instances of different classes later on in the program? so something like:
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 CClass1
{
public:
	void print() {
		std::cout << "This should print first" << std::endl;
	}
} qClass1;

class CClass2
{
public:
	void print() {
		std::cout << "This should print second" << std::endl;
	}
} qClass2;

int main() {
	void *pClass;           //yea i know void is wrong but it would have looked           
	pClass = &qClass1;      //even dumber to write CClass1;
	(*pClass).print();
	pClass = &qClass2;
	(*pClass).print();
	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
Last edited on
bump
You'd need to make a common base class from which both those classes inherit and give it a print() function, then make pClass a pointer to that base class.
No need to bump the topic, I always check the topics I post in.

What you're looking for (and I had guessed you might be) is inheritance and polymorphism. These are the next step in object-oriented design - if you are ready you can read about them here:
http://www.cplusplus.com/doc/tutorial/inheritance/#inheritance
http://www.cplusplus.com/doc/tutorial/polymorphism/
Cast the pointer to appropriate type and dereference...

1
2
3
4
5
6
7
8
9
int main() {
	void *pClass;
	pClass = &qClass1;
	(*(CClass1 *)(pClass)).print();
	pClass = &qClass2;
	(*(CClass2 *)(pClass)).print();
	std::cin.ignore();
	return 0;
}


Or

1
2
3
4
5
6
7
8
9
int main() {
	void *pClass;
	pClass = &qClass1;
	((CClass1 *)(pClass))->print();
	pClass = &qClass2;
	((CClass2 *)(pClass))->print();
	std::cin.ignore();
	return 0;
}


The code below apparently might cause undefined behavior, although it compiles fine for me and runs perfectly. I will search around as to why it works

1
2
3
4
5
6
7
int main() {
	void *pClass(NULL);
	((CClass1 *)(pClass))->print();
	((CClass2 *)(pClass))->print();
	std::cin.ignore();
	return 0;
}


http://eternallyconfuzzled.com/tuts/languages/jsw_tut_pointers.aspx
Last edited on
thanks L B. it wasn't just for you, i was bumping in the hopes that anyone would read, and i think it worked. in any event i am very appreciative of your effort, i will check out those links.

im trying to answer two other questions for every one of mine that is answered =D.
Last edited on
@Smac89, I would like to ask that you don't recommend solutions with void * unless they are the only solution. It's very bad practice to use void * like that. It's also extremely dangerous to dereference a null pointer, even if the member function you are calling does not look at this - technically it is undefined behavior and may crash.
L B wrote:
It's very bad practice to use void * like that

Are you referring to the 3rd example or the first 2?
I am referring to all three examples - yes, even the first two. This is not what you should be using void pointers for ;)
Not meaning to sound rude but I understand why it shouldn't be used for the third one, but what is wrong with the first ones? And what are void pointers useful for if they can't be used in this manner?
The first two are essentially identical. The problem is that you are using the same void pointer to point to different types of data at different points in the program. Unfortunately, this means you also have to somehow track what kind of data is stored before accessing it, or you have enormous problems. This is multiple problems in one: having to track which kind of data is pointed to (defeating the purpose of using one pointer in the first place; you might as well use several), you have to cast to and from void pointer to access the data, and due to code duplication you leave room for errors.

These are the problems that inheritance and polymorphism are specifically designed to solve. I won't explain why as you should know.

So what, then, are they useful for, you ask? Well, they still retain quite an important use with C-like or C-compatible libraries and even modern C++ libraries: user data. The two most common examples I can think of are threads and callbacks. For threads, you need to give data to that thread somehow, but the library that handles the threads for you can't know in advance what kind of data you will use, so it uses a void pointer. The important thing here is that the void pointer will always be used for the same purpose with the same code, so there is no reason to check what type of data it points to - you already know. With callbacks, the same applies.
Yea, I've used it with posix threads before but I never knew that was the reason it was being used for. They can also be used for generic c functions; Example being the qsort function in stdlib

Thanks for clarifying L B
Topic archived. No new replies allowed.