Archived

This topic is now archived and is closed to further replies.

Esap1

Class Question

Recommended Posts

Can I do this? class Object_class { int hi; }; int LoadFile(char *file,Object_class *Object) { //Is this ok? Object = new Object(10); //Those are brackets } void Whatever() { Object_class *Object; LoadFile("hi.txt",Object); } ---------------------------------------------------- Can I do that, it doesnt seem to work. Thanks for the help.

Share this post


Link to post
Share on other sites
Ok First, use the SOURCE brackets, like this:
    
class Object_class {
int hi;
};

int LoadFile(char *file,Object_class *Object)
{
//Is this ok?

Object = new Object(10); //Those are brackets

//It should be this :

//Object = new Object_class[10];

// I think this is why you have an error.

}

void Whatever()
{
Object_class *Object;
LoadFile("hi.txt",Object);
}



Try this and if you have an error again, tell us what is exactly the error it gives to you.

Programming is:
A.The art of debugging a blank sheet of paper (or an empty file).
B.A pastime similar to banging one's head against a wall, but with fewer opportunities for reward.
C.The most fun you can have with your clothes on (although clothes are not mandatory).

Share this post


Link to post
Share on other sites
quote:
Original post by Esap1

Can I do this?
class Object_class {
int hi;
};
int LoadFile(char *file,Object_class *Object)
{
//Is this ok?
Object = new Object(10); //Those are brackets
}

void Whatever()
{
Object_class *Object;
LoadFile("hi.txt",Object);
}


----------------------------------------------------
Can I do that, it doesnt seem to work. Thanks for the help.




Should get it working with this (if I understand your problem right..):
        
int LoadFile(char *file,Object_class **Object)
{
*Object = new Object_class[10];
}

void Whatever()
{
Object_class *Object;
LoadFile("hi.txt",&Object);
}




Edited by - _2Late_ on June 15, 2000 12:00:15 AM

Share this post


Link to post
Share on other sites

Its not a compiling error, its just it doesnt work. What Im trying to do is I have a MAP class that holds a number of Object Classes. What I want to do is have my LoadASE() function be able to alocate the right number of Object classes in the Map class and then load the data into the object classes.

class Map_class {
Object_class *Object;//An array of Objects
};

int LoadASE(char *file, Object_class *Object)
{
//Find out how many object are in the file
FindObjects();
Object = new Object_class[numObjects];
}

Share this post


Link to post
Share on other sites

Im quite sure on this, i cant get to a mashine with vc6++ right now to test it.


void Whatever()
{
Object_class *whateverObject;
LoadFile("hi.txt",whateverObject);
}

int LoadFile(char *file,Object_class *loadfileObject)
{
//Is this ok?
loadfileObject = new Object(10); //Those are brackets
}


you are sending what whateverObject pointers points to, to the loadFile call. But when you move the loadfileObject pointer (with new), you DONT move the whateverObject pointer, it still points to the memory area it did from the start.

So here are three posibilities
1: do the thing with double pointers.
2: return the pointer
[source]
Object_class * LoadFile(char *file)
{
Object_class * loadfileObject;
loadfileObject = new Object(10); //Those are brackets
....
...
..
return loadfileObject;
}

void Whatever()
{
Object_class *whateverObject;
whateverObject = LoadFile("hi.txt");
}

return NULL for error.

3:
send a pointer to the Map_class object


class Map_class {
Object_class *Object;//An array of Objects
};

int LoadASE(char *file, Map_class *Map)
{
//Find out how many object are in the file
Map->Object = new Object_class[numObjects];
}

void Whatever()
{
Map_class Map;
LoadFile("hi.txt", &Map);
}

Edited by - claus hansen ries on June 16, 2000 3:13:18 AM


Edited by - Claus Hansen Ries on June 16, 2000 3:15:01 AM

Share this post


Link to post
Share on other sites
quote:
Original post by Esap1
Its not a compiling error, its just it doesnt work. What Im trying to do is I have a MAP class that holds a number of Object Classes. What I want to do is have my LoadASE() function be able to alocate the right number of Object classes in the Map class and then load the data into the object classes.

class Map_class {
Object_class *Object;//An array of Objects
};

int LoadASE(char *file, Object_class *Object)
{
//Find out how many object are in the file
FindObjects();
Object = new Object_class[numObjects];
}




Your problem has to do with a common misunderstanding of how parameters are handled. I understand it but have a difficult time explaining it effectively, so maybe some sample code will illustrate:

    
void by_value(int x) {
x = 1;
}

void by_pointer(int* px) {
*px = 2;
}

void by_new_pointer(int* px) {
px = new int;
*px = 3;
}

void by_new_double_pointer(int** ppx) {
*ppx = new int;
**ppx = 4;
}

void by_reference(int& rx) { // C++ only

x = 5;
}

void by_pointer_reference(int*& rpx) { // C++ only

rpx = new int;
*rpx = 6;
}

int main()
{
int a = 0, *b = &a;
printf("%d %d\n", a, *b); //output: 0 0


by_value(a);
printf("%d %d\n", a, *b); //output: 0 0


by_pointer(b)
printf("%d %d\n", a, *b); //output: 2 2


by_new_pointer(b);
printf("%d %d\n", a, *b); //output: 2 2


by_new_double_pointer(&b);
printf("%d %d\n", a, *b); //output: 2 4


delete b; //ok, b not pointing at a
b = &a;

by_reference(a);
printf("%d %d\n", a, *b); //output: 5 5


by_pointer_reference(b);
printf("%d %d\n", a, *b); //output: 5 6


delete b; //ok, b not pointing at a
return 0;
}


Also note that the call to by_new_pointer() has leaked memory which cannot be recovered.



---- --- -- -
Blue programmer needs food badly. Blue programmer is about to die!

Share this post


Link to post
Share on other sites
you could just return the Object_Class pointer from LoadFile()

eg:

Object_class* LoadFile(const char* pszFilename)
{
Object_class* pArray = new Object_Class[10];

return pArray;
}

this way, NULL is returned if the newing failed

then you say:

void Whatever()
{
Object_class* p = LoadFile("some.map");

if(p == NULL)
cout << "HANDLE THE ERROR!";
}

Share this post


Link to post
Share on other sites
What about doing this.

class Map_class {
Object_class *Object;
void LoadFile(char *file);
};




void Map_class::LoadFile(char *file)
{
Object = new Object_class[numObject];
//Load in the stuff
}


Is there a way I can get it to work when the function is in Map_class, It seems a more logical place to put it.

Edited by - Esap1 on June 17, 2000 10:50:58 PM

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
no, you cant.
when u use new, you need a variable type, not a perticular variable

it should be:

int LoadFile(char *file,Object_class *Object)
{
//Is this ok?
Object = new Object_class[10]; //Those are brackets
}



- Infested Furby

infestedfurby.cjb.net







Can I do this?

class Object_class {
int hi;
};

int LoadFile(char *file,Object_class *Object)
{
//Is this ok?
Object = new Object(10); //Those are brackets
}

void Whatever()
{
Object_class *Object;
LoadFile("hi.txt",Object);
}


----------------------------------------------------
Can I do that, it doesnt seem to work. Thanks for the help.



Share this post


Link to post
Share on other sites
Um, what? Map_class is a class, Im trying to make an array of Object_class''s, of that *Object pointer. Can I do it that way?

Share this post


Link to post
Share on other sites
I've been away for a little while.


OK, when you use pointers, they are really just another type of variable, and when you do this:


void function(int* pointer)
{
// do something here
}

void main()
{
int* pointer;

function(pointer);
}



function() just makes a copy of the pointer variable and uses that. So to solve your problem, you don't need to change anything except this one line:


int LoadFile(char *file,Object_class *&Object)



Which basically just tells the compiler not to make a copy of the pointer. Simple, huh? (In reality, it does the double-pointer thing in the compiled code, but you use it like a single pointer.)


OK, now with your objects, you need a constructor to assign a value to a data member. As your code is now, you can't access the member from outside the class member functions, because that's the default with classes. If you do want to access it from outside, use public:. I've modified the code with all of those changes, and here is the finished product:


class Object_class {
public:
int hi;
Object_class();
};

Object_class::Object_class()
{
// this function is called whenever an Object_class is created

hi = 0; // default value of hi
}

int LoadFile(char *file, Object_class *&Object)
{
Object = new Object(10); // should be brackets
}

void Whatever()
{
Object_class *Object;
LoadFile("hi.txt", Object);
}



And yes, an array variable is really just a pointer to the first element anyway. You can do this:


void main()
{
char* mystring = new char[10];
}



And you can do the same thing with an Object_class pointer and an Object_class array.


Here's the Map_class code again:


class Map_class {
Object_class *Object;
void LoadFile(char *file);
};

void Map_class::LoadFile(char *file)
{
Object = new Object_class[numObject];
//Load in the stuff
}



BTW, I was reading up on the sizeof() operator, and it says that it returns the size of an array, so there's really no point in storing the size in your map class. I think that this should work:


void main()
{
Object_class* p = new Object_class[10];

int numObjects = sizeof(p); // should be 10
}



That code shouldn't cause anymore performance slowdown than just storing the size of the array yourself, because sizeof() just looks up the variables that C++ is already using to track the size of the array.





- null_pointer
Sabre Multimedia


(I wish the source tags would work properly!)


Edited by - null_pointer on June 18, 2000 7:24:04 AM

Share this post


Link to post
Share on other sites