gflores Posted August 18, 2004 Share Posted August 18, 2004 How does one make an object null? I think in Java, one could do anObject = null; but I could be wrong. Basically, I have an array of objects and I want to delete one of the objects. I think null is the way to do it, no? Secondly, I have this.. class EmpArys { public: EmpArys(); int findEmp(int pSocSecNbr); Employee getEmp(int pSocSecNbr); bool addEmp(Employee pEmp); bool delEmp(int pSocSecNbr); string toString(); string getPayList(Tax pTaxTable); private: Employee empList[100]; //i've temporarily put 100 there int empCount; const int maxNumOfEmps, empNotFound; }; I can't figure out how to instaniate empList to maxNumOfEmps and also to instantiate maxNumOfEmps to 100 and empNotFound to -1. I can't instantiate them in the constructor because they are constant and not in the class either. Major help needed. I tried this for instantiating the object in the constructors, which would work in Java (I think), but not in C++ Code: EmpArys::EmpArys() { empArys[maxNumOfEmps]; } Thanks, I greatly appreciate the help. Link to comment Share on other sites More sharing options...
0 kjordan2001 Posted August 18, 2004 Share Posted August 18, 2004 To delete an object: delete empList[x]; or you could set it to NULL: empList[x] = NULL; For the array, try: .h: Employee *empList; const int maxNumOfEmps = 100; const int empNotFound = -1; .cpp: empList = new Employee[maxNumOfEmps]; And make sure to include the .h. Link to comment Share on other sites More sharing options...
0 +virtorio MVC Posted August 18, 2004 MVC Share Posted August 18, 2004 You can't do this sort of thing directly, you instead need to maintain a list of pointers to your objects. You also just can't delete a object by removing all references to it, it must be manually deleted using the delete keyword. Hopefully this simple demo will give you a basic idea. I only had a few minutes to create it so it might have a few problems but there are lots of resources on the internet about this sort of thing. #include "stdafx.h" #include <iostream> class Student { private: char m_firstName[30]; char m_lastName[30]; int m_id; public: Student() { memset(m_firstName, 0, 30); memset(m_lastName, 0, 30); m_id = 0; } void setFirst(char* name) { strcpy(m_firstName, name); } void setLast(char* name) { strcpy(m_lastName, name); } void setId(int id) { m_id = id; } int getId() { return m_id; } char* getFirst() { return m_firstName; } char* getLast() { return m_lastName; } }; class Cource { private: Student* m_studentList[30]; public: Cource() { for (int i=0; i < 30; i++) m_studentList[i] = NULL; } void add(Student* pStudent) { for (int i=0; i < 30; i++) { if (m_studentList[i] == NULL) { pStudent->setId( getNextId() ); m_studentList[i] = pStudent; return; } } } void del(Student* pStudent) { for (int i=0; i < 30; i++) { if (m_studentList[i] == pStudent) { m_studentList[i] = NULL; delete pStudent; } } } Student* get(int index) { return m_studentList[index]; } int getNextId() { int id = 0; for (int i=0; i < 30; i++) { Student* student = get(i); if (student != NULL) { if (student->getId() >= id) id = student->getId() + 10; } } return id; } void free() { for (int i=0; i < 30; i++) { Student* pStudent = get(i); if (pStudent != NULL) del(pStudent); } } }; int _tmain(int argc, _TCHAR* argv[]) { Cource cr; Student* student = new Student(); student->setFirst("Willow"); student->setLast("Cantara"); cr.add(student); student = new Student(); student->setFirst("Lisa"); student->setLast("Gerard"); cr.add(student); student = new Student(); student->setFirst("Xavier"); student->setLast("Devenport"); cr.add(student); student = cr.get(1); cr.del(student); for (int i=0; i < 30; i++) { Student* pStudent = cr.get(i); if (pStudent != NULL) { std::cout << pStudent->getId() << " " << pStudent->getFirst() << ", " << pStudent->getLast() << "\n"; } } cr.free(); } Link to comment Share on other sites More sharing options...
0 bithub Posted August 18, 2004 Share Posted August 18, 2004 To understand deleting objects in C++, you have to understand the difference between creating an object on the stack, and creating an object on the heap. Here is an example: CObject stackObject; ?// This is an object created on the stack CObject *heapObject = new CObject; // Object created on the heap Now to delete stackObject, you do not have to do anything. This object's memory will be freed when the function it was created in ends. heapObject needs to be deleted manually though. In order to do this, simply run: delete heapObject; Basically if thenew> operator is used to create an object, you have to call delete on it in order to free the memory. Link to comment Share on other sites More sharing options...
Question
gflores
How does one make an object null? I think in Java, one could do
but I could be wrong. Basically, I have an array of objects and I want to delete one of the objects. I think null is the way to do it, no?
Secondly, I have this..
class EmpArys { public: EmpArys(); int findEmp(int pSocSecNbr); Employee getEmp(int pSocSecNbr); bool addEmp(Employee pEmp); bool delEmp(int pSocSecNbr); string toString(); string getPayList(Tax pTaxTable); private: Employee empList[100]; //i've temporarily put 100 there int empCount; const int maxNumOfEmps, empNotFound; };I can't figure out how to instaniate empList to maxNumOfEmps and also to instantiate maxNumOfEmps to 100 and empNotFound to -1.
I can't instantiate them in the constructor because they are constant and not in the class either. Major help needed.
I tried this for instantiating the object in the constructors, which would work in Java (I think), but not in C++
Code:
EmpArys::EmpArys()
{
empArys[maxNumOfEmps];
}
Thanks, I greatly appreciate the help.
Link to comment
Share on other sites
3 answers to this question
Recommended Posts