View Full Version : Help needed with c++ asap! (pls)
Misao
15-10-2003, 10:30 PM
If you know c++ please please please help.
I am getting very confused with the syntax, and need to understand something so i can do my assignment.
I have pasted the code below.
I dont understand this bit in a.cpp:
B** A::getResult()
with the B** is that a pointer? or an array? or what is it, and how should i use it?
Please message me on icq # 111001860
Thanks
test.cpp (wrapper):
#include "A.h"
int main()
{
A a;
B ** b;
for(int i = 0; i < B_MAX; i++)
{
b = a.getResult();
}
for(int i = 0; i < B_MAX; i++)
{
b[i] -> print();
}
return 1;
}
A.h
#ifndef A_H
#define A_H
#include "B.h"
const int B_MAX = 10;
class A
{
public:
A();
~A();
B** getResult();
private:
B * b;
};
#endif
[b] a.cpp
#include "A.h"
#include "B.h"
A::A()
{
}
A::~A()
{
for(int i = 0; i < B_MAX; i++)
{
delete b[i];
}
}
B** A::getResult()
{
for(int i = 0; i < B_MAX ; i++)
{
b[i] = new B(i);
}
return b;
}
b.h
#ifndef B_H
#define B_H
class B
{
public:
B(int aB);
void print();
private:
int b;
};
#endif
b.cpp
#include "B.h"
#include <iostream.h>
B::B(int aB)
{
b = aB;
}
void B::print()
{
cout << b << endl;
}
skozombie
15-10-2003, 10:53 PM
** is a pointer to a pointer.
Normally this is used when you've got arrays of pointers, so you might have an array of objects.
Think of your PROPER int main prototype:
int main(int argc, char** argv);
argv is an array of arrays (a string is a character array after all).
Hope this helps.
Misao
15-10-2003, 10:54 PM
cool , that's exactly what i needed to know :)
Misao
15-10-2003, 11:34 PM
OK, i am still getting confused
I will have an array of my own class, lets call it foo
now, with my array of foo - lets call it fooArray - i need the values in it updated by another function
so what i want to do is pass a pointer to my array to the function.
i then want the function to access the objects that this pointer refers to.
how do i do that? i am getting mega confused. I know how to pas a reeference to an int, or a string or soemthing , but how i do ideclare in the start of the function that i am pasing a pointer to an array?
thanks :)
geggle
16-10-2003, 12:25 AM
Ok, so check if I have this right here.
You have:
B **b = ....;
which is an array of pointers to instances of class B.
You want to pass this array to a function, which is going to modify the instances of class B in this array, but is not going to change the size of the array.
Is this correct?
Such a function could be declared as:
void modifyArray( B **theArray ) ...
If (say) you wanted modifyArray to replace the existing item 2 with a new instance of class B, you would implement it like this:
void modifyArray( B **theArray )
{
// Dispose original item 2
delete theArray[2];
// Assign new item 2
theArray[2] = new B(256);
}
Misao
16-10-2003, 12:49 AM
OK, i think i get it, do i have to deference or do anything weird?
Also, i think that makes sense, i tell the function it is receiving a pointer to a pointer, but i dont need to put the [] to say it is an array?
is there a more efficent way i can do the assignment?
will the new things disappear when i exit the function?
Basically our assignment is to implement an OO version of the game of life, which is a cell automation thing
I have to do what you have posted 8 times for each cell in a grid. so a grid of 10x10 = 100 cells, which means that i have to create and delete 100 x 8 objects (pretty hefty). What I am using the function for is because each cell has to know what to do. It needs to know what the state of it's neighbours are to implement the growth rules, and our lecturer has said we are not allowed to store an array of references to the neighbours within the cell (the cell cant know about its environment, but we can tell it .. umm something like that).
So basically, for each cell to be updated, i need to get the cell coordinates, pass the reference and the array of pointers to a function.
Then this function will update the array. I can then access the array to see if the cell needs to change at all according to it's rules.
the algorithm for this would look like:
for (i=0; i<width; i++)
for (j=0; j<height; j++) // these 2 lines will loop through and access every cell
(loop 8 times)
delete neighbour[x];
new neighbour[x];
this would have to happen every "cycle" of the program
Asmodeus
16-10-2003, 05:15 AM
cant store the cells neighbors in in cell info.. why not make another class that stores the calls neighbor references then fo rthe controller section to reference, but not the cells themselves?
just a stupid idea.
vBulletin® v3.7.2, Copyright ©2000-2008, Jelsoft Enterprises Ltd.