I'm probably going to confuse myself while writing this, sorry in advance:
Is there a way I can access a location in a dynamic array(increment an array pointer) of pointers using the sizeof() the object that is in the array?
For example:
I have an dynamic array of type base class Student populated with derived class objects(Graduate, Undergraduate).
Due to this I can't just step through my array in the normal fashion to display information because the actual objects Graduate and Undergraduate are different sizes than Student. Each array step will move sizeof(Student) when the actual objects are larger.
Depending on the type of student I am doing(this is for Graduate):
Student *student = new Graduate(undergrad, fName, lName, major, idNum, arr2, cSize,
degreeType, thesis);
arr[i] = student;
Where arr was declared: Student *arr = new Student[size];
Using my array I had created this in a for loop:
if (!students[i].getGradStatus()){
handleGraduate(&students[i], i);
step = step + sizeof(Graduate);
}
else if (students[i].getGradStatus()){
handleUndergraduate(&students[i], i);
step = step + sizeof(Undergraduate);
}
I was trying to come up with a way to change the step size. I don't think this will work with a for loop but a while loop may be different. Pretty much I'm trying to go something similar to a file seekg() but manually on an array.
And as I've noticed everyone likes to question the use of dynamic arrays over vectors so let me just say I cannot use vectors on this project(No STL is allowed :( ). And I have to use Polymorphism, thus why I have an array pointer of type Student holding derived class objects.
You can't store different sized objects in an array like that. When you try to copy a derived type like Graduate over a base type like Student you get what's known as slicing because of the differences in object size (parts can get chopped off).
To do this you need to store Student* (pointers to Students.)
class Student
{
std::string name;
public:
Student(const std::string& name): name(name) {}
virtual ~Student() {} // virtual destructor
virtual void handle() = 0; // implementation must override this
std::string get_name() const { return name; }
};
class Graduate
: public Student
{
public:
Graduate(const std::string& name): Student(name) {}
virtual void handle() { std::cout << "Handling Graduate" << '\n'; }
};
class UnderGraduate
: public Student
{
public:
UnderGraduate(const std::string& name): Student(name) {}
virtual void handle() { std::cout << "Handling UnderGraduate" << '\n'; }
};
int main()
{
Student** students = new Student*[3];
students[0] = new Graduate("Wendy");
students[1] = new UnderGraduate("Bob");
students[2] = new Graduate("Rachel");
for(int i = 0; i < 3; ++i)
{
std::cout << students[i]->get_name() << '\n';
students[i]->handle(); // polymorphic function (virtual)
}
delete[] students; // clean up or (better) use a smart pointer
}
Output:
Wendy
Handling Graduate
Bob
Handling UnderGraduate
Rachel
Handling Graduate
Change your array to be an array of Student pointers instead of Student objects.
Student **arr = new Student*[size];
When you do that your objects will live on the heap and the pointer size is always the same, so the issue you are having will go away.
Since you can write that I assume that it's possible to instantiate a Student (non-derived). That's bad, since you're going to store derived objects in the array your students will be sliced (poor students!). To counter that you're doing weird things with the size of the instance types. Just don't.
Make an array of student pointers instead, Student **arr. Pointers have fixed size and they will point to any of the derived types of student so you don't have to worry about their sizes.
Student **student = new Student*[size];
...
student[i] = new Graduate(...);
This should work.
Related
I try to make simple program that will display students names from array of objects called "Student", I know that it can be easily done using vectors but I would like to do that using dynamic array. My code:
class Student {
public:
string name;
Student(string name){
this->name = name;
}
};
void DisplayStudentsNames(Student array[],int length) {
for(int i=0;i<length;i++){
cout << array[i].name << endl;
}
}
int main(int argc, char** argv) {
Student ** array = new Student *[3];
array[0] = new Student("Michael");
array[1] = new Student("Tom");
array[2] = new Student("Timmy");
DisplayStudentsNames(*array,3);
return 0;
}
I am not sure why it does not work, it does compile but program just "stops responding" after displaying first student name. I wonder what is the issue, I have read here that if Class does not have any zero argument constructor you cannot create an array of dynamic objects dynamically, I wonder why is that? I'm just assigining new objects to pointers (in C# that's the normal way creating a class).
Your issue is here
DisplayStudentsNames(*array,3);
What you are actually passing to DisplayStudentNames() here is the first element is the array array. The issue is that array[0] is not an array, it is a pointer to a single object. Therefore when you try and iterate it you get undefined behaviour, an exception would be the best case scienorio here (as you saw with your infinite loop).
You can fix this by changing
DisplayStudentsNames(*array,3);
to
DisplayStudentsNames(array,3);
and DisplayStudentsNames to take Student**
Also this wont work:
cout << array[i].name << endl;
Each element in array is a pointer to a Student object and in C++ accessing an object through a pointer requires using the -> operator (so array[i]->name instead).
Side note:
Student(string name){
this->name = name;
}
This is bad C++, use initaliser lists for setting members on creation instead, as it allows compiler optimisations and is easier to read
I need to add multiple students to the course class but it keeps overwriting the last entry.
I tried to set the address to the object and also using the bracket operator but this causes a memory leak. The problem is with the AddStudent function
class Student {
public:
Student() { name = "unknown"; };
Student(string n) { name = n; };
void Print() { cout << name << endl; };
string GetName() { return name; };
private:
string name;
};
class Course {
public:
Course(int i) {
id=i;
nstudents=0;
capacity=0;
};
void AddStudent(Student s) {
students=&s;
nstudents++;
};
private:
int capacity;
int nstudents;
Student* students;
int id;
};
It only lets me add one student.
"students=&s;" you are taking the address of a copy-by-value variable which becomes dangling after you leave the function. You don't store the students. You have to put a container in it something like std::vector and copy/move your students in it.
So here with a std::vector with copy and move.
class Course {
public:
Course(int i) {
id=i;
};
void AddStudent(const Student& s) {
students.push_back(s);
};
void AddStudent(Student&& s) {
students.push_back(std::move(s));
};
private:
std::vector<Student> students;
int id;
};
You need to allocate memory for your Students array.In your constructor class add : students = new Student[SomeInitialSize] .Also in your add method you need to check if the size of your students is enough to store another student s,and if there is not you need to allocate more memory.
When you add your first student, students is set to point to s inside the AddStudent function. The problem is since s is local to the function, it is destroyed when the function returns. So students is now a dangling pointer pointing to nowhere since the memory it points to is no longer used to store the Student object. In order to keep the memory after the AddStudent function returns, you would have to dynamically allocate it with new, but there are other problems with that.
Let's say you dynamically allocate the memory for the new student. When we add a second student, some more memory will be allocated to store that. That new memory could end up being in a totally different place. Your AddStudent function will set the students pointer to point to the new Student object, but now we've forgotten where the existing student is stored.
So how do we fix this? We could allocate an array of Student objects, leaving extra space for new students. This way, all the students are stored in a contiguous piece of memory, and students will always point to the first object. In our AddStudent function, we would put the new student after the last student we currently have by doing something like students[nstudents] = s, before incrementing nstudents.
The problem with this is that if we exceed the capacity of the array, we would have to allocate a new larger array and copy everything over since we can't expand an existing block of allocated memory. Or you could just make the array fixed sized. But there's a better solution: std::vector.
std::vector is a standard library container that manages the memory for you. You can store your students there and easily add one using push_back. You can learn how to use vectors by asking your instructor, reading a good book, or finding a tutorial online (do note that there are many bad ones out there).
I have two classes, PersonnelLists and Employee. I create an instance of PersonnelLists in my main, like so:
int main() {
PersonnelLists example; //Make a personnel list
...
}
PersonnelLists uses a constructor with member initialisation of a list of employees, the number of employees, and the size of the array:
PersonnelLists::PersonnelLists(): List(new Employee[SIZE]), numEmployees(0), arraySize(SIZE){
}
This results in some null empty employees being created (I think?):
Employee::Employee(): employeeNumber(0), name(NULL), department(NULL) {
}
It is at this line that I get an invalid null pointer error.
I am new with C++, fresh off the boat from Java programming. I'm still a novice with pointers, so I'm not quite sure what I'm doing wrong here.
UPDATE:
As requested, here is the class definition of Employee:
#include <iostream>
class Employee {
public:
Employee(); //constructor
Employee(std::string name, std::string deparment);
void Print() const; //Print this employee's details
void setEmployeeNo(int employeeNum);
private:
int employeeNumber;
std::string name;
std::string department;
};
In Java, new Employee[SIZE] creates an array of null references.
In C++, new Employee[SIZE] creates an array of default-constructed instances of Employee. Your default constructor tries to set name and department to NULL. Attempting to initialize a std::string to NULL would give the error you describe.
There's no "null" string in C++, but you could default-construct name and department, which would set them to empty strings:
Employee::Employee(): employeeNumber(0), name(), department() {
Finally, if List can contain a variable number of elements, I would recommend that you use std::vector<Employee> (which is similar to ArrayList<Employee> in Java).
If name and department are std::strings (or a similar string type), then initializing them with NULL (a null character pointer) is invalid.
If I guessed right, you should default-initialize them instead, as:
Employee::Employee(): employeeNumber(0), name(), department() {
}
But we really can't tell without seeing the class definition of Employee.
As others have pointed out, you should use a std::vector instead of an array. That allows you to only
have valid Employee objects in your "list".
I don't know what the actual definitions of your classes are, so it's kind of hard to identify your problem.
But an option in modern C++ of doing that is to use a std::vector<Employee> data member inside PersonnelList class. std::vector can grow dynamically at runtime, using its push_back() method, e.g.
#include <vector> // for std::vector
class Employee
{
....
};
class PersonnelList
{
public:
PersonnelList()
{
// Nothing to do - vector is initialized empty
}
// Get current employee count
size_t Count() const
{
return m_employees.size();
}
// Add a new employee to the personnel
void AddEmployee(const Employee& newEmployee)
{
m_employees.push_back(newEmployee);
}
private:
std::vector<Employee> m_employees;
};
No need to use raw pointers or something similar: robust RAII STL container classes make your code simpler.
Have a homework assignment in which I'm supposed to create a vector of pointers to objects
Later on down the load, I'll be using inheritance/polymorphism to extend the class to include fees for two-day delivery, next day air, etc. However, that is not my concern right now. The final goal of the current program is to just print out every object's content in the vector (name & address) and find it's shipping cost (weight*cost).
My Trouble is not with the logic, I'm just confused on few points related to objects/pointers/vectors in general. But first my code. I basically cut out everything that does not mater right now, int main, will have user input, but right now I hard-coded two examples.
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Package {
public:
Package(); //default constructor
Package(string d_name, string d_add, string d_zip, string d_city, string d_state, double c, double w);
double calculateCost(double, double);
~Package();
private:
string dest_name;
string dest_address;
string dest_zip;
string dest_city;
string dest_state;
double weight;
double cost;
};
Package::Package()
{
cout<<"Constucting Package Object with default values: "<<endl;
string dest_name="";
string dest_address="";
string dest_zip="";
string dest_city="";
string dest_state="";
double weight=0;
double cost=0;
}
Package::Package(string d_name, string d_add, string d_zip, string d_city, string d_state, string r_name, string r_add, string r_zip, string r_city, string r_state, double w, double c){
cout<<"Constucting Package Object with user defined values: "<<endl;
string dest_name=d_name;
string dest_address=d_add;
string dest_zip=d_zip;
string dest_city=d_city;
string dest_state=d_state;
double weight=w;
double cost=c;
}
Package::~Package()
{
cout<<"Deconstructing Package Object!"<<endl;
delete Package;
}
double Package::calculateCost(double x, double y){
return x+y;
}
int main(){
double cost=0;
vector<Package*> shipment;
cout<<"Enter Shipping Cost: "<<endl;
cin>>cost;
shipment.push_back(new Package("tom r","123 thunder road", "90210", "Red Bank", "NJ", cost, 10.5));
shipment.push_back(new Package ("Harry Potter","10 Madison Avenue", "55555", "New York", "NY", cost, 32.3));
return 0;
}
So my questions are:
I'm told I have to use a vector
of Object Pointers, not Objects.
Why? My assignment calls for it
specifically, but I'm also told it
won't work otherwise.
Where should I be creating this
vector?
Should it be part of my Package
Class? How do I go about adding
objects into it then?
Do I need a copy constructor? Why?
What's the proper way to deconstruct
my vector of object pointers?
Any help would be appreciated. I've searched for a lot of related articles on here and I realize that my program will have memory leaks. Using one of the specialized ptrs from boost:: will not be available for me to use. Right now, I'm more concerned with getting the foundation of my program built. That way I can actually get down to the functionality I need to create.
Thanks.
A vector of pointers can be reused for storing objects of sub-classes:
class Person
{
public:
virtual const std::string& to_string () = 0;
virtual ~Person () { }
};
class Student : public Person
{
const std::string& to_string ()
{
// return name + grade
}
};
class Employee : public Person
{
const std::string& to_string ()
{
// return name + salary
}
};
std::vector<Person*> persons;
person.push_back (new Student (name, grade));
person.push_back (new Employee (name, salary));
person[0]->to_string (); // name + grade
person[1]->to_string (); // name + salary
Ideally the vector should be wrapped up in a class. This makes memory management easier. It also facilitates changing the support data structure (here an std::vector) without breaking existing client code:
class PersonList
{
public:
Person* AddStudent (const std::string& name, int grade)
{
Person* p = new Student (name, grade);
persons.push_back (p);
return p;
}
Person* AddEmployee (const std::string& name, double salary)
{
Person* p = new Employee (name, salary);
persons.push_back (p);
return p;
}
~PersonList ()
{
size_t sz = persons.size ();
for (size_t i = 0; i < sz; ++i)
delete persons[i];
}
private
std::vector<Person*> persons;
};
So we can re-write our code as:
{
PersonList persons;
Person* student = persons.AddStudent (name, grade);
Person* employee = persons.AddEmployee (name, salary);
student.to_string ();
employee.to_string ();
} // The memory allocated for the Person objects will be deleted when
// `persons` go out of scope here.
Getting familiar with the Rule of Three will help you decide when to add a copy constructor to a class. Also read about const correctness.
Question 1:
You mentioned inheritance. Since inherited objects often need more bytes of storage, they don't fit into the place of a base object. If you try to put them in, you get a base object instead. This is called object slicing.
Question 2:
Design first, before you write code. There are a bunch of possible solutions.
For a start you can keep it in main(), but later you will be forced to make a class like PackageContainer for holding your objects.
Question 3 + 4:
You need a copy constructor, an assignment operator= and a destructor, when a class object owns dynamically allocated objects (the Rule of the Big Three). So a PackageContainer will probably need them.
You create objects dynamically using new Object(..). You are responsible for destroying them and for giving their memory back to the system immediately before your vector of pointers is destroyed:
for (size_t i = 0; i < shipment.size(); ++i)
{
delete shipment[i];
}
Since working with naked pointers to dynamically allocated objects is not safe, consider using
std::vector<tr1::shared_ptr<Package> > shipment;
instead or
std::vector<std::shared_ptr<Package> > shipment;
if your compiler understands C++0x. The shared_ptr handles freeing memory for you: It implements the Rule of the Big Three for one object pointer. It should be used in production quality code.
But try to get it right with naked pointers also. I think that's what your homework assignment is about.
I'm told I have to use a vector of Object Pointers, not Objects. Why? My assignment calls for it specifically, but I'm also told it won't work otherwise.
Usually, one would avoid using vector of objects to avoid the problem of Object Slicing. To make polymorphism work You have to use some kind of pointers. I am not sure of how the classes in your assignment are aligned but probably you might have Inheritance there somewhere and hence if vector is storing objects of Base class and you insert objects of Derived class in it then it would cause the derived class members to slice off.
The Best solution will be to use a smart pointer instead of a Raw pointer. The STL has an auto_ptr, but that cannot be used in a standard container.Boost smart pointers would be a best solution but as you already said you can't use Boost So in your case you can use your compiler's implementation of smart pointers, which comes in TR1 namespace,remember though that there is some disagreement on the namespace for TR1 functions (Visual C++ puts them in std::, while GCC puts them in std::tr1::).
Where should I be creating this vector? Should it be part of my Package Class? How do I go about adding objects into it then?
Your example code already has an example of adding a pointer to Package class in a vector. In a nutshell you will dynamically allocate pointers to Package and then add them to the vector.
Do I need a copy constructor? Why?
The copy constructor generated by the compiler does member-wise copying. Sometimes that is not sufficient. For example:
class MyClass {
public:
MyClass( const char* str );
~MyClass();
private:
char* str;
};
MyClass::MyClass( const char* str2 )
{
str = new char[srtlen( str2 ) + 1 ];
strcpy( str, str2 );
}
Class::~Class()
{
delete[] str;
}
In this case member-wise copying of str member will not duplicate the buffer (only the pointer will be copied(shallow copy)), so the first to be destroyed copy sharing the buffer will call delete[] successfully and the second will run into Undefined Behavior. You need deep copying copy constructor (and assignment operator as well) in such a scenario.
When to use a custom copy constructor is best defined by the Rule Of Three:
Whenever you are writing either one of Destructor, Copy Constructor or Copy Assignment Operator, you probably need to write the other two.
What's the proper way to deconstruct my vector of object pointers?
You will have to explicitly call delete on each contained pointer to delete the content it is pointing to.
vector::erase
Removes from the vector container and calls its destructor but If the contained object is a pointer it doesnt take ownership of destroying it.
Check out this answer here to know how to corrctly delete a vector of pointer to objects.
I am looking to be pointed in the right direction.
I have 1 class Event
class Event{
private:
vector<string> Question;
char Mode;// 1 = Ascending 2 = Descending 3 = None
string EventName;
public:
Event(string Name){
EventName = Name;
SetQuestionSize();
SetQuestion();
Mode = 3;
}
void SetName(string NewName){
EventName = NewName;
}
void SetQuestionSize(){
Question.resize(15);
}
int ReturnQuestionSize(){
return Question.size();
}
void SetQuestion(){
Question[0]="Enter ";
Question[1]="1 ";
Question[2]="to ";
Question[3]="sort ";
Question[4]="in ";
Question[5]="ascending ";
Question[6]="order, ";
Question[7]="2 ";
Question[8]="for ";
Question[9]="Descending, ";
Question[10]="or ";
Question[11]="3 ";
Question[12]="to ";
Question[13]="ignore ";
Question[14]=EventName;
}
string ReturnQuestion(int Index){
return Question[Index];
}
/*vector<string> ReturnQuestion(){
return Question;
}*/
void SetMode(char NewMode){
if (NewMode == '0' || NewMode == '1' || NewMode == '2')
Mode = NewMode;
}
char ReturnMode(){
return Mode;
}
string ReturnName(){
return EventName;
}
};
This is will be a member of a second object, which will use Event's functions to store data in Event's members.
The problem I'm having is declaring an array of Event objects in my second object. When researching I came across ways to use an array of pointers to the first object, and some operator '->' that I'm guessing is related to virtual functions.
class WhatTheyWant{
Event *events[2];
public:
WhatTheyWant(){
events[0]= new Event("Miss");
events[1]= new Event("Dodge");
}
};
I'm very ignorant about pointers, and I know I will have to learn them eventually, but are they the best way to go or is there a better.
Since your Event class doesn't have a default constructor, you need to explicitly construct each object with its name, so the way you're doing it currently is the only way to do it.
If you add a default constructor to Event, you can do it in at least two other ways:
If you will always have a (small) fixed number of objects, you can just declare an array of constant size:
Event events[2];
Doing this will automatically construct the objects when WhatTheyWant is created, so you just need to set the names afterwards:
WhatTheyWant() {
events[0].SetName("Miss");
events[1].SetName("Dodge");
}
If you want to have a variable number of events, you can declare a single pointer and dynamically allocate an array of objects:
Event *events;
And you could probably give the number as a parameter to the constructor:
WhatTheyWant(int numEvents) {
events = new Event[numEvents];
for (int i = 0; i < numEvents; i++)
events[i]->SetName("...");
}
Also, not directly related to your question, but your Mode variable would be better modeled using an enumeration instead of a char. Using an enum makes it clearer as to what the variable really means, rather than using values like 0, 1 and 2. For example:
public:
enum ModeType { Ascending, Descending, None };
private:
ModeType Mode;
public:
Event() {
...
Mode = Ascending;
}
void SetMode(ModeType NewMode) {
Mode = NewMode;
}
ModeType ReturnMode() {
return Mode;
}
You can use either array of objects or array of pointers.
Array of objects go like below.
class WhatTheyWant{
Event events[2];
public:
WhatTheyWant()
{
events[0] = Event("Miss");
events[1] = Event("Dodge");
}
};
Note: You need to add default constructor to your event class to compile the above approach.
With the above approach, you do not need to take care of freeing Event objects. Whenever WhatTheyWant object gets destroyed, event objects get destroyed.
Array of pointers approach goes like you mentioned.
But you need to take care of freeing the memory allocated(Unless you use auto_ptr or some c++0x equivalent).
Deletion should happen in destructor like below.
class WhatTheyWant{
Event *events[2];
public:
WhatTheyWant(){
events[0]= new Event("Miss");
events[1]= new Event("Dodge");
}
~WhatTheyWant()
{
delete events[0];
delete events[1];
}
};
In C++, pointers are just like arrays
in your WhatTheyWant class, you define the private member:
Event *events[2];
This is an array of arrays (2D array) with variable length (of arrays) and 2 element in each array.
and the operator '->' is used when you want to access a (member of some kind of object) and that is called an object pointer (a pointer which points to an object) but when you define a normal object variable you use '.' operator.
If you've got the courage and knowledge to use them they are very useful but in general they're dangerous and that's why the new languages tend to go to the managed way.