The Enchanted Cave 2
Delve into a strange cave with a seemingly endless supply of treasure, strategically choos
4.34 / 5.00 31,296 ViewsGhostbusters B.I.P.
COMPLETE edition of the interactive "choose next panel" comic
4.07 / 5.00 10,082 ViewsOk, so i have an array, but the thing is i want it to hold multiple types of data
like
element 1 of the array is an instance of the Ball class
element 2 is an instance of Magnet
element 3 is an instance of Cup
how can i do this?
You should be able to just make an array of generic objects and add those all to it.
At 1/23/06 04:36 PM, Afro_Ninja wrote: You should be able to just make an array of generic objects and add those all to it.
huh?
At 1/23/06 08:11 PM, Glaiel_Gamer wrote:At 1/23/06 04:36 PM, Afro_Ninja wrote: You should be able to just make an array of generic objects and add those all to it.huh?
The three things you made are objects, correct? Now this is speaking from my Java experience but if you make an array of type Object (as opposed to string or int) it should be able to hold three different objects. Maybe I'm completely wrong though..
all I know is that through polymorphism an array of superclass objects should be able to contain any subclass that inherits from it. Everything inherits from System.Object or similar.
Well, in C++ there is no Object class, but you can probably define your own Object class. Then from there it MIGHT work. If not, you can always cast to the base class.
At 1/23/06 08:31 PM, Ravens_Grin wrote: If not, you can always cast to the base class.
no cause there are some differences between the classes
Glaiel, in C++ an array must hold ALL the same data type. You can't have string in one, int in another double in another. Every element must be of the same type that was declared when you declared the array.
Check on:
vector
deque
list
Mutli-dimension arrays.
I'm studying some C++ now, and I haven't gotten into adding multiple data types yet, but those things might be good places to start looking into how to do it.
Perpetually looking for time to return to the arts.
Unfortunately, C++ does not have an Object class or a base class all other classes are derived from, otherwise, this would be simpler.
Anyway, try this, a structure defined how you like it.
typedef struct _tag {
-- Classone &obj1;
-- Classtwo &obj2;
-- Classthree &obj3;
}mystruct;
Then all you would have to do is:
mystruct Stuff = {Object1,Object2,Object3};
However, if you would like to optimize your program a little, you can do this: prefix all those obj* in the structure declaration with a & to make it a reference. The thing is that this optimizes the program because three entirely new objects don't have to be built, which takes time. You're just passing a reference to them.
However, references CAN be volatile, so to be sure you don't mutate the actual objects, do this declaration instead:
const mystruct Stuff = {Object1,Object2,Object3};
Const makes sure nothing gets fucked with in the context of your program, so you don't accidentally mess with the real objects.
Note I am not responsible for any injury and/or death that could be caused from this information. I had another idea, but I haven't really tested it (I haven't tested this either, but I'm more sure about this,) if I figure it out I'll try to explain it with my horrible vernacular later.
omg.
Playstation Network tag: muffin-noodle
the empty set
At 1/23/06 11:40 PM, SrgntJack wrote: typedef struct _tag {
-- Classone &obj1;
-- Classtwo &obj2;
-- Classthree &obj3;
}mystruct;
Son of a bitch. Anyway - that is what it would look like if you were doing the reference thing. If you don't care about references or whatever, just declare the structure as this:
typedef struct _tag {
-- Classone obj1;
-- Classtwo obj2;
-- Classthree obj3;
}mystruct;
And then do:
mystruct Stuff = {...};
omg.
Playstation Network tag: muffin-noodle
the empty set
i kinda need an array for what i'm doing, is there a way to with pointers?
At 1/23/06 08:40 PM, Glaiel_Gamer wrote:At 1/23/06 08:31 PM, Ravens_Grin wrote: If not, you can always cast to the base class.no cause there are some differences between the classes
Which is why you modify the constructors, that would most likely work
At 1/24/06 12:24 PM, Glaiel_Gamer wrote: i kinda need an array for what i'm doing, is there a way to with pointers?
If we knew what you were doing it might help. Why do you need to store these classes in an array?
just do an array of generic objects and then cast
Inheritance will probably be able to solve your problem.
Since C++ is a type-specific language, it is impossible to make an array of various data types directly. However, you can create a base class and use pointers to achieve a non-homogeneous array.
Say, for instance, that I had three classes A, B, and C. I would create a base class called Base and inherit those three classes from it:
class Base
{
...
};
class A : public Base
{
...
};
class B : public Base
{
...
};
class C : public Base
{
...
}
Then, you would make an array of Base pointers:
Base *Array[100] = {0};
And whenever you add an object, set the pointer:
A ClassA;
Array[0] = &ClassA;
However, this method does create some problems. If you want to use the list outside of the function it was created in, you will have to allocate the classes dynamically using the keyword new:
Array[0] = new A;
The second problem is accessing the data inside of the inherited classes. It's one thing to have a list of Base pointers representing different classes, but its another thing to access the data in those classes.
To remedy this, you can add all the interfaces (functions) needed for classes A, B, and C inside of the Base class. Then, you would overload the functions in the inherited classes.
For example, if you had two variables X and Y inside of class A, and a variable Z inside of class B, your Base class could look like this:
class Base
{
public:
virtual int GetX(){;}
virtual int GetY(){;}
virtual int GetZ(){;}
};
And A and B would look like this:
class A
{
protected:
int X, Y;
public:
int GetX(){return X;}
int GetY(){return Y;}
};
class B
{
protected:
int Z;
public:
int GetZ(){return Z;}
}
You may also want to include a type variable in the base class so you know what type of inherited class you are dealing with.
Of course, there are probably other ways to accomplish what you are trying to do. If you redeisgn your program, you may not even need to have a non-homogeneous array. However, I find that this strategy is fairly easy to implement. However, the code can get messy quickly if you are dealing with a number of complex classes.
I hope this helped.
At 1/24/06 05:34 PM, FPSinsanity wrote: I hope this helped.
It sure did! THANKS ALOT!
ok now i have yet another problem. I got the objects to work, and they behave right, but i had to disable the image loading cause it kept turning up "unhandled exemptions"
it's really odd.....
How are these images loaded? There are a variety of things that can cause an unhandled exception, and more information would definitely help. In the meantime, here are a few possibilities:
1. Make sure that the image name and file path are correct. The file path is relative to the project folder when developing from an IDE, and relative to the executable when running outside of an IDE. Also, make sure that the file extension of the file to load from is complete and correct.
2. Be sure to use the new command if the data is used outisde of the function in which it was created. If you don't, the data will be de-allocated and you will get an invalid pointer.
2. Check the specifics of the load function you are using. Are all of the parameters correct? Are there any special flags you need to set for the type of image you are trying to load?