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.
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");
}
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:
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.
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.
@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.
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