How to manage objects in a fixed array? - c++

This if for my homework.
I have a class called Student that takes 3 parameters (id, name, class) and I want to store each student in an array called Roster (which can only have 7 students).
The user will provides input to add or remove students. Thus, I have to manage the array by creating or deleting students. So if the user specify the student ID, I have to remove him for the array.
I tried to use a fixed array, but I'm struggling to make it works. Is there a better way to implement this?
I must not use a vector or any STL container.
student.h
#ifndef STUDENT_H
#define STUDENT_H
#include <iostream>
#include <string>
static const int SIZE = 7;
class Student {
private:
int student_id;
std::string name;
std::string classification;
public:
Student(int, std::string, std::string); // constructor; initialize the list to be empty
~Student();
void print();
};
#endif
student.cpp
#include <iostream>
#include <string>
#include "student.h"
#define PROMPT "class> "
using namespace std;
Student::Student(int a, string b, string c){
student_id = a;
name = b;
classification = c;
}
Student::~Student(){
//delete Student
}
void Student::print(){
cout<<"Enrolled:"<<endl;
cout<<student_id<<"-"<<name<<"-"<<classification<<endl;
}
main.cpp
#include <iostream>
#include <string>
//#include <sstream>
#include "student.h"
#define PROMPT "class> "
using namespace std;
//**** Implement Error Handling ****\\
enum errorType {
UNKNOWN_ERROR,
INPUT_ERROR,
HANDLER,
NUM_ERRORS
};
// error messages
string errorMessage[NUM_ERRORS] = {
"Unknown Error\n",
"Input Error\n",
};
// error handler
void handleError(errorType err) {
if(err > 0 && err < NUM_ERRORS)
cout<< "Error: "<< errorMessage[err];
else cout<< "Error: "<< errorMessage[UNKNOWN_ERROR];
}
//**** END Error Handling ****\\
void enroll(Student newStudent){
cout<<"test";
Student roster[SIZE];
for(int i=0;i<SIZE;i++){
newStudent->roster[i];
}
}
void handleInput() {
int id; string n, c;
cin>>id>>n>>c;
Student newStudent(id,n,c);
newStudent.print();
enroll(newStudent);
//cout<<"hello3"<<endl;
return;
}
int main() {
//Student newStudent; /* <-- why doesn't this work?!*/
string input = "";
bool finished = false;
cout<<PROMPT; // prompt the user
while(!finished) {
if(input!="") cout<<PROMPT;
cin>>input;
if(input=="enroll") {
cout<<PROMPT<<"Enroll student:"<<endl;
handleInput();
}
else if(input=="drop") {
cout<<PROMPT<<"Enter ID:"<<endl;
}
else if(input=="roster") {
cout<<"This will print formatted list of students"<<endl;
}
else if(input=="quit") {
finished=true;
}
else handleError(errorType(1));
}
}

Since it is a homework, I'd like to point out some mistakes you did because it is important to understand what you are doing in the first place.
You must not program by coincidence, but by trying to understand exactly what's going on. By doing that you will become better and better and the answers should fall in place.
What you've done
So, from what you are describing, the array is fixed. Thus it is a good idea to use a constant as you did (SIZE).
However, as we can see below you a declaring an array of size SIZE in the function. By doing that, your array is like a temporary variable, because its scope is inside the function. Each time you call this function, the array will be declared again and then deleted at the exit. So it should be declared outside.
void enroll(Student newStudent)
{
cout<<"test";
Student roster[SIZE]; // Here 'roster' will be available only inside the function.
for(int i=0;i<SIZE;i++)
{
newStudent->roster[i]; // Here there is few mistakes see my explanation below*
}
}
If we look at this part:
newStudent->roster[i];
First of all, the arrow '->' is used with pointers. The dot '.' is used with objects. In both case, it does the same thing, access to public members of Student.
Since you passed
void enroll(Student newStudent)
you should use '.' instead.
newStudent.SomeOfYourMembers;
If the parameter was a pointer to a Student
void enroll(Student *newStudent)
Then, you'd have to use the arrow '->' like you did.
Back to the original statement:
newStudent->roster[i];
This means, you want to access to 'roster' array at position 'i' inside your Student object (newStudent). As you can see in your code, roster is not declared inside Student (and should not be since you want an array of Students), so that won't work.
Guidelines
As I mentionned, your array should be outside the function, so at a higher scope.
Then, if you need an array of student, basically, 'roster[i]' will give you access to the student 'i'. Thus, if you want to print the student, you would do something like that:
roster[i].print();
This would be valid because 'print()' is defined as public.
In order to store a student inside the array, you can do something like:
roster[i] = new Student(0 /* id*/, "name", "classification");
But don't forget, each time you use new, you have to balance it with a delete. And if you are creating the student like this in a loop, you will have to clean them the same way:
for(int i = 0; i < SIZE; ++i)
{
delete roster[i];
}
Good luck!
Don't hesitate if there is there anything that I could clarify. I hope this helps!
Edit: In reply to your first comment.
Concerning the roster array
No, it is not mandatory to create a class roster you could declare roster in the main.cpp.
The key concept is that by defining
Student roster[SIZE];
the array will contains objects of type Student.
What roster[i].print() means is that you are printing one of the Student of that array, in fact the one at position 'i'.
Concerning the print() function
What is powerfull with Object Oriented language, each object will have the same print() function. So, you do not need to convert the array to string.
However, if you want a string to be printed out (or returned) you can write the code inside the print() function that will do this job.
The advantage of this, is that if further on you need to change your array in some ways, your print() function will always work.
Concerning the Delete
When you are doing something like this on an array that contains objects:
delete roster[i];
It will delete the object at the position 'i'. Thus, the destructor of that Student 'i' will be called. If your object Student would contains other object, you would have to delete them in the destructor.
Further notices
Since ID is an input that you are storing into a string, you will have to convert the ID to the same type of the student_id, which is a int. Then you can always write a loop for each student and check their ID to delete the proper one.
Concerning the container, a fixed array might not be the best to achieve this job. You might want to look the LinkedList concept.

It doesn't make much sense for enroll to be a member function, so
I'd wrap the roster into a class to get automatic clean up of my
pointers.
#include <cstddef>
struct Student {};
class Roster
{
private:
static const size_t size = 7;
// non-copyable
Roster(const Roster&);
Roster& operator=(const Roster&);
public:
Roster() {
for(unsigned i = 0; i < size; ++i) {
roster_[i] = NULL;
}
}
~Roster() {
for(unsigned i = 0; i < size; ++i) {
delete roster_[i];
}
}
// enroll by copy
bool enroll(const Student& s) {
for(unsigned i = 0; i < size; ++i) {
if(roster_[i] == NULL) {
roster_[i] = new Student(s);
return true;
}
}
// out of space
return false;
}
// enroll by taking ownership
bool enroll(Student* s) {
for(unsigned i = 0; i < size; ++i) {
if(roster_[i] == NULL) {
roster_[i] = s;
return true;
}
}
// out of space
return false;
}
private:
// data
Student* roster_[size];
};
int main()
{
Roster r;
Student s;
r.enroll(s);
Student* sp = new Student();
r.enroll(sp);
return 0;
}

What about this?
Student * roster[2];
roster[0] = new Student(5,"first","2A");
roster[1] = new Student(2,"Second","5B");
Ps:
Enrol and Size shouldn't be members of the student class.
Print should ideally be externalized and a ToString function should be added instead.
You should use the inline constructor initialization instead:
Student(int a,string b,string c):id(a),name(b),class(c){}

You've used the keyword class as a variable name of type string. You shouldn't do that. Does it even compile like that?
enroll should have two arguments: void enroll( Student enrollee, Student Roster[]). You should probably change the name of Roster to roster because it's not a class and typically class names are capitalized.
If your array will only ever have 7 students then you could use some sentinel value to mark that the current student as an invalid student. Perhaps the id will be -1 to mark this. It means basically that you need some way to keep track of which spots in the array you can still use. If you don't do this then declaring an array of Students will get you an array of students with garbage member variables. You wouldn't be able to tell which students are real ones and which are just place holders for when someone new enrolls in the class. I would create a default constructor of Student and initialize its member variables like this:
id=-1;
name="";
name_of_class="";
I changed the name of your string class to avoid confusion.
After all that, enroll would look something like this:
void Student::enroll( Student enrolee, Student roster[]){
//search through roster to check for the first student with an
//id of -1
//if there are no students with id of -1, produce an error message
//that the class is full
//overwrite the student with id of -1 with the id, name, and
//name_of_class of enrollee
}
Although I'm not sure what exactly string class is there for. Does it store what class the Student is in? Is it their year in school like Freshman, Sophomore?
If you're suppose to use dynamic allocation of roster, though, it's a different story, but you said it will only ever have seven students.

Related

One warning and main() returned garbage value

Multiple students can associate with a single Department and single student can
associate with multiple Departments, but there is no ownership between the objects
and both have their own lifecycle. Both can create and delete independently.
WAP in C++ to model the relationships.
I have implemented this code as follows
#include<iostream>
#include<cstring>
using namespace std;
class Student
{
char* name_p;
public:
Student(char *sName)
{
cout<<"Student constructor called\n";
name_p=new char(sizeof(strlen(sName)));
name_p=sName;
}
~Student()
{
cout<<"Student destructor called\n";
delete name_p;
};
char* sName()
{
return name_p;
}
};
class Department
{
char* name_p;
public:
Department(char *dName)
{
cout<<"Department destructor called\n";
name_p=new char(sizeof(strlen(dName)));
name_p=dName;
}
~Department()
{
cout<<"Department destructor called\n";
delete name_p;
}
char* dName()
{
return name_p;
}
};
class Course
{
Student* std_p;
Department* dept_p;
char* courseName_p;
static unsigned int index;
static Course *courseList_p[4];
public:
Course(char* crseName,Student* student,Department* dept)
{
cout<<"Course constructor called\n";
std_p=student;
dept_p=dept;
if(index<4)
{
courseName_p=new char(sizeof(strlen(crseName)));
courseName_p=crseName;
courseList_p[index]=this;
++index;
}
else
{
cout<<"Cannot accomodate any more Course\n";
}
};
~Course()
{
cout<<"Course destructor called\n";
delete courseName_p;
};
static char* findStudent(char *crseName, char* deptName)
{
for(int i=0; i<index; i++)
{
if ( (courseList_p[i]->getCourseName() == crseName) &&
(courseList_p[i]->getDeptName() == deptName) )
{
return(courseList_p[i]->getStdName());
}
}
}
char* getStdName()
{
return std_p->sName();
};
char* getDeptName()
{
return dept_p->dName();
};
char* getCourseName()
{
return courseName_p;
};
};
unsigned int Course::index =0;
Course* Course::courseList_p[4]={0,0,0,0};
int main()
{
int i;
cout<<"\nExample of Association class\n";
cout<<"-----------------------------------\n\n";
cout<<"We have got 4 students\n";
Student *studentNames[4] = {new Student("Meera"), new Student("Moina"), new Student("Teena"), new Student("Mridula")} ;
cout<<"\n";
cout<<"We have got 2 Departments\n";
Department *departNames[2] = {new Department("Mathematics"), new Department("ComputerSceince")} ;
cout<<"\n";
cout<<"Here class Course Associates Student and Department, with a Course name\n";
Course course1("DataStructure",studentNames[0], departNames[1]);
Course course2("Maths",studentNames[3], departNames[0]);
Course course3("Geometry",studentNames[2], departNames[0]);
Course course4("CA",studentNames[1], departNames[1]);
cout<<"\n";
cout<<"Finding a Student using Course and Department\n";
cout<<"Student who has taken Maths Course in Mathematics Department is:"<<Course::findStudent("Maths", "Mathematics")<<endl;
cout<<"\n";
cout<<"Deletion of objects\n\n";
for(i=0;i<4;i++)
{
delete studentNames[i];
}
cout<<"\n";
for(i=0;i<2;i++)
{
delete departNames[i];
}
cout<<"\n";
return 0;
}
The code is showing warnings in main function i.e. ISO C++ forbids converting a string constant to char* and also the main() function is not returning 0 but a garbage value. Please help me in rectify these errors and warnings.
Also I don't want to use this pointer in class course, can I implement the code without using this pointer.
There are a couple of problems with your code. Mostly they are related to the use of char* instead of string. I'm amazed to see that some teacher still are found of this practice.
c-string allocation problems
The first problem causes memory corruption. This could explain that your main() returns garbage instead of 0. For example:
name_p=new char(sizeof(strlen(dName))); // OUCH!!!
In the statement you allocate it wrong. If you want to allocate an array, you must use new[...]. Moreover, keep in mind that a c-string allways needs 1 char more for the trailing null. So it should be:
name_p=new char[strlen(dName)+1];
would already improve the situation.
Last but not the least, if you allocate an array you must delete an array with delete[]
c-string copying issue
Then, you can copy C++ string with an assignment operator just as in:
name_p=sName; // OUCH
However, for char*, this just overwrites the pointer that new returned and you are not sure that the c-string it points to will still be valid when it's used.
If you want to assign a c-string, you need to go for:
strcpy (name_p, sName); // C-style
copy (sName, sName+strlen(sName)+2, name_p); // Safer C++ alternative
c-string comparison issue
Suppose you have two pointers to c string, say pa and pb. Comparing the pointers looks at the memory address, not at the content. So yu could have two identical strings but the comparison could nevertheless fail. THis kind of comparison would work perfectly well for c++ string, but for cstring, you need to to
strcmp(pa, pb)==0 // instead of pa == pb
c-strings in classes and structures
Whenever you have some pointers that you allocate in a class, you need to think of the rule of 3. Meaning that you'd need to add a copy constructor and an assignment operator. Fortunately, at first sight it seems that you don't need them here. Of couse, if you'd use c++ string you wouldn't have to worry about such details.
c-string conversion issues?
ANother issue in your code is the use of string literals with constructors. For example:
new Student("Meera").... // c-string literals (constant)
Student(char*...) // constructor using a c-string pointer (non constant)
You can easily get rid of this complaint, by making the constructor's pointer argument as constant:
Student(const char *sName)...
The same applies to function arguments, such as in find.
Other problems
The static member findStudent() works well, but only if a result is found. If nothing is found, it's UB, since there is no return statement (put a return nullptr; at the end of this function?)
Moreover, the wya you use this function, directly printing its result is dangerous, because dereferencing an invalid pointer will lead to UB.
Here the corrected code. It compiles and seem to run. I didn't analyse for more issues.

Accessing specific attributes of an array of a user defined type

I'm a beginner in C++ and I am trying to create a program which simulates a flight management system. I have created classes to simplify the process. Right now, I have a class named "Flight" which has other user-defined data types as its attributes. For example, it includes two "Date" objects, for arrival and departure dates of my flight object. I also have an array of 30 "Passenger" objects which represent the passengers on my flight. I'm experiencing difficulty making changes to this array though. I need to write a member function for my Flight class which can delete Passengers from the array given the ID (which is an attribute of Passenger).
I want to try to implement this function by passing it an integer "removeID". This value is then compared to the ID's of the passengers in the array. If it matches, the entry is "deleted" essentially freeing the space in the array.
My class Flight is defined as such in its own header file.
#include <string>
#include "Date.h"
#include "Time.h"
#include "Passenger.h"
#ifndef FLIGHT_H_
#define FLIGHT_H_
class Flight
{
public:
Flight(std::string, Date, Date, Time, Time);
float flightTime();
void addPassenger(Passenger);
void removePassenger(int);
bool listChecker(Passenger);
void flightListPrinter()const;
int seatsRemaining();
private:
std::string flightNumber;
Date arrivalDate;
Date departureDate;
Time arrivalTime;
Time departureTime;
std::string destination;
Passenger* passengerList[30];
int numPassengers;
};
#endif
This is the layout of my class Passenger:
#include <string>
#include "Date.h"
class Passenger
{
public:
//Part a)
// Constructor
Passenger(int = 1337, std::string = "Name", std::string = "Banana Island", std::string = "(514)514-5145", Date birth=(1,1,1999));
// Part b)
int getID();
std::string getName();
std::string getAddress();
std::string getTel();
Date getBday();
// Part c)
void setAddress(std::string);
void setTel(std::string);
void setBirthD(Date);
// Part d)
void printInfo()const;
private:
int ID;
std::string name;
std::string address;
std::string Tel;
Date birthD;
};
I'm trying to access the ID, an attribute of the Passenger object, which itself is and attribute of the Flight object. The passengers are all stored in an array called passengerList, it is 30 passengers long. In my main.cpp file, I've defined the remove passenger function as follows:
void Flight::removePassenger(int removeThisID) // Passing the ID to remove
{
for (int i = 0; i < 30; i++) // Check all the passengers in the array
{
// Unsure about the way to access the ID. PassengerList[i].ID == removeThisID ?
}
}
Unfortunately, you can't delete from an array, from what I understand of your project I can suggest you 2 things to overcome this problem
1 - You can store the array in some other data structure in every deletion, and reinitialize it without your target, which is a lot of work OR,
2 - You can use vectors from cpp's standart library (std) which will do the first part for you, automatically. You can check the vectors from http://www.cplusplus.com/reference/vector/vector/vector/
You can not remove from an array. Its fixed size.
One way would be to recreate the array everytime you change the size,
but that would be quite annoying.
Gladly there is the c++ standard library which you can use to solve
such problems.
It has a diverse set of containers that manage the storage space for their elements and provide member functions to access them.
Replace Passenger* passengerList[30] with std::vector<Passenger> passengerList
void Flight::removePassenger(int removeThisID) // Passing the ID to remove
{
for (unsigned int i = 0; i < passengerList.size(); i++)
{
// if (passengerList.at(i).getID() == removeThisID)
if (passengerList[i].getID() == removeThisID)
{
passengerList.erase(passengerList.begin()+i);
return;
}
}
}

How can I pass arguments to a class by creating an object in C++?

I am working on my first separate class file. I have been given a driver program which I am not supposed to change and I am to create a class file that runs off the driver.
#include <iostream>
#include <iomanip>
using namespace std;
#include "Question.h"
int main()
{
string q2Answers [] = {"China","India","Mexico","Australia"};
Question q2("Which country is home to the Kangaroo?",q2Answers,'D');
q2.display();
cout << endl;
}
The driver, simplified above, appears to be passing arguments to the class via arguments. My class header file is built in the following fashion.
#ifndef QUESTION_H
#define QUESTION_H
#include <string>
using namespace std;
class Question
{
public:
void setStem(string newStem);
void setAnswers(string newAnswers[]);
void setKey(char newKey);
void display();
string getStem();
string getAnswer(int index);
char getKey();
private:
string stem;
string answers[4];
char key;
};
#endif // QUESTION_H
How can I execute the functions in my class using arguments passed into an object? I am confused as to how the line,
Question q2("Which country is home to the Kangaroo?",q2Answers,'D');
has any way of pushing those arguments into the functions. Any insight into this would be very appreciated.
If I understood you correctly, you're asking for how to make a constructor(See OldProgrammer's link in the comment to your question):
You can either make it right in the header file, like so:
Question(const std::string& theQuestion,
const std::string theOptions[], const char& correctAnswer)
{
this->stem = theQuestion;
for(int i=0; i<4; i++){
this->answers[i] = theAnswers[i];
}
this->key = correctAnswer;
}
~Question(){}
//This is called the "Destructor", it is a function called when the object is destroyed
(You can imagine the const std::string&-part as string or const char&-part as just char if you don't know what they mean, since it's not very important right now.)
Or you can make it in the separate .cpp-file like so:
Question::Question(const std::string& theQuestion,
const std::string& theOptions[], const char& correctAnswer)
{
this->stem = theQuestion;
for(int i=0; i<4; i++){
this->answers[i] = theAnswers[i];
}
this->key = correctAnswer;
}
Question::~Question(){}
You might be asking why we use destructors; it's because sometimes there are things we need to do before the object is removed.
Like for instance if you want to save certain info or make a change, or more commonly to free dynamic memory you allocated when you made the object. Otherwise you get memory leaks, which are bad.
Then you can construct/create an object as such:
Question q2("Which country is home to the Kangaroo?",q2Answers,'D');
You can also "overload" the constructor, i.e. you can make other versions of it. For example if you imagine that the constructor's only parameter was the question:
Question(std::string q){
this->stem = q;
}
Question(char c[]){
this->stem = c;
}
Now you can pass either a string or an array of characters to the object. But if you only have one, you can't do the other too, so if we only have the first constructor, we can't pass an array of characters to do the exact same thing. And you can make as many of these as you like, but it doesn't necessarily mean that it's better just because it has a ton of constructors.

I have a class object as a member of another class, how do I initialize as a safe empty state?

I have 2 classes, ISBN, Order. I have an ISBN object as a data member of my Order class and I am having issues with the Order constructor to place the ISBN object in a safe empty state.
My Order.h
#include <iostream>
using namespace std;
class ISBN;
class Order {
int ordered;
int delivered;
ISBN * book;
bool empty;
public:
Order();
Order(const ISBN & isbn);
};
My ISBN.h
#include <iostream>
using namespace std;
class ISBNPrefix;
class ISBN {
char isbnNum[13];
char area[6];
char publisher[8];
char title[7];
char checkDigit[1];
bool emptycheck;
bool registered;
public:
ISBN();
ISBN(const char * str, const ISBNPrefix& list);
}
In my Order constructor I tried this code:
Order::Order() {
ordered = 0;
delivered = 0;
empty = true;
*book->ISBN();
/*
(*book).isbnNum[0] = '\0';
book.area[0] = '\0';
book.publisher[0] = '\0';
book.title[0] = '\0';
book.checkDigit[0] = '\0';
book.emptycheck = true;
book.registered = false; */
}
And variations of it, but I get errors like: "type name is not allowed" "expression must have pointer type" etc...Anyone know what my issue is?
You almost certainly don't want a pointer here, just an ISBN object as a data member:
ISBN book;
This will automatically be initialised using its default constructor; you don't need to do anything. If you want to initialise it using the other constructor (with arguments), then you'll need to do that in the initialiser list:
Order::Order() : book(some_string, some_list)
{
// body of constructor
}
You are having problems because you have declared book as an ISBN*. Therefore your posted line *book->ISBN(); is trying to dereference a null and then call the blank constructor.
If you want to manually allocate book, then you should then use this pattern:
Order::Order() {
ordered = 0;
delivered = 0;
empty = true;
book = new ISBN();
}
Note this will require Order's destructor to call delete on its book member.
You can automatically allocate and delete book as an ISBN by making it a class member, and not a pointer. For that, use this declaration:
class Order {
ISBN book;
... // your other members
}
This will automatically allocate and automatically deallocate an ISBN object member whenever class Order is instatiated and destroyed respectively. No additional steps necessary.

How can I access a class's member function via an array of pointers?

I have a pretty standard class with some public member functions and private variables.
My problem originally stems from not being able to dynamically name object instances of my class so I created an array of pointers of the class type:
static CShape* shapeDB[dbSize];
I have some prompts to get info for the fields to be passed to the constructor (this seems to work):
shapeDB[CShape::openSlot] = new CShape(iParam1,sParam1,sParam2);
openSlot increments properly so if I were to create another CShape object, it would have the next pointer pointing to it. This next bit of code doesn't work and crashes consistently:
cout << shapeDB[2]->getName() << " has a surface area of: " << shapeDB[2]->getSA() << shapeDB[2]->getUnits() << endl;
The array of pointers is declared globally outside of main and the get() functions are public within the class returning strings or integers. I'm not sure what I'm doing wrong but something relating to the pointer set up I'm sure. I'm writing this code to try and learn more about classes/pointers and have gotten seriously stumped as I can't find anyone else trying to do this.
I'm also curious as to what the CShape new instances get named..? if there is any other way to dynamically create object instances and track the names so as to be able to access them for member functions, I'm all ears.
I've tried all sorts of permutations of pointer referencing/de-referencing but most are unable to compile. I can post larger chunks or all of the code if anyone thinks that will help.
class CShape {
int dim[maxFaces];
int faces;
string units;
string type;
string name;
bool initialized;
int slot;
public:
static int openSlot;
CShape();
CShape(int, string, string); // faces, units, name
~CShape();
void initialize(void);
// external assist functions
int getA(void) {
return 0;
}
int getSA(void) {
int tempSA = 0;
// initialize if not
if(initialized == false) {
initialize();
}
// if initialized, calculate SA
if(initialized == true) {
for(int i = 0; i < faces; i++)
{
tempSA += dim[i];
}
return(tempSA);
}
return 0;
}
string getUnits(void) {
return(units);
}
string getName(void) {
return(name);
}
// friend functions
friend int printDetails(string);
};
// constructor with values
CShape::CShape(int f, string u, string n) {
initialized = false;
faces = f;
units = u;
name = n;
slot = openSlot;
openSlot++;
}
My guess is you use the CShape constructor to increment CShape::openSlot?
You're probably changing the value before it's read, thus the pointer is stored in a different location.
Try replacing openSlot with a fixed value to rule out this CShape::option.
-- code was added --
I'm pretty sure this is the problem, the constructor is executed before the asignment, which means the lhs. will be evaluated after CShape::openSlot is incremented.