Skip to main content
gamedev.net
🔒 Locked

class creating problem

Started by DorRaba 6 replies 650+ views
Original Post
DorRaba
DorRaba
hi, I''ve created a class named Person, that have the properties of Name, Address, Phone, My proplem is not in the class, the class works, but I Don''t know how to Declare an Array of the Class, I tryed this: Person* PersonObj[] = new Person*[5]; Person[0]->Name=S"Dor"; I get a run-time error, not a compiler error, does anyone know what is the problem?
DDDDor RRRRaba
coldacid
coldacid

Person* lamerz = new Person[5];
lamerz[0]->Name = "froghat";

quorn
quorn
you're assigning a point rather than copying a string. either use c's strncpy or use std::string which has an assignment operator.

Also encapsulate.

[edited by - quorn on April 30, 2004 11:44:36 AM]
DorRaba
DorRaba
Another question isHow in Visual C++ .NET, I create a new Database, write and Read from it, just with code?
DDDDor RRRRaba
Zahlman
Zahlman
quote:
Original post by DorRaba
hi, I''ve created a class named Person, that have the properties of Name, Address, Phone, My proplem is not in the class, the class works, but I Don''t know how to Declare an Array of the Class, I tryed this:

Person* PersonObj[] = new Person*[5];
Person[0]->Name=S"Dor";

I get a run-time error, not a compiler error, does anyone know what is the problem?


(1) you need to array-subscript the array "PersonObj", not the typename "Person". That shouldn''t compile; I assume you typed it incorrectly in your post.
(2) You allocated an array of pointers; those pointers don''t actually point to anything yet. You have to create the object that they are to point to:

Person* PersonObj[] = new Person*[5];
PersonObj[0] = new Person();
PersonObj[0]->Name = S"Dor";

I have no idea about the VC++.NET "Database" object; go look up its documentation.

AP: I don''t really know anything about .NET, but I assume that instantiating managed objects directly is not possible because it would mess with the garbage collection built into the managed stuff. So you''ll have to make "ListViewItem** NewItem = new ListViewItem *[5];", i.e. create an array of pointers, and work with it as in the previous example with Persons.
DorRaba
DorRaba
I have two more little questions:

1. how to I convert Ascii code in numbers to a string or a char object so I can use the character it represents.

2. Does the next short program(not very smart but just for this sample) affect the memory because I don't delete the object before I create as new one?

Person * per = new Person;
.
.
per = new person;
.
.
per = new person;
.
.
//should I delete "per" before I create a new Person object on it?
Does it make a memory leak???


[edited by - DorRaba on May 1, 2004 6:24:24 AM]
DDDDor RRRRaba
VolkerG
VolkerG
If Person is a manged class, the answer is "no, it won''t cause a memory leak". If it is unmanaged you should delete the object.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.