0

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++;
}
10
  • 1
    Are you sure shapeDB[2] points at an allocated object? (i.e. has the new expression line occurred with CShape::openSlot being 2?) Commented Jan 16, 2013 at 21:09
  • What is a dynamically name object instances? Commented Jan 16, 2013 at 21:10
  • from what I can tell, yes. the constructor is what increments openSlot during create so the next time an object instance is created, it is all set to have a spare pointer pointing to it. ill post some more of the code Commented Jan 16, 2013 at 21:10
  • Where exactly is static CShape* shapeDB[dbSize] declared? Could it be that you declare separate shapeDB array in each translation unit in your program? I.e. you initialize one of them, while the rest remain filled with nulls. Commented Jan 16, 2013 at 21:12
  • 1
    But are you sure 2 is the right index? Commented Jan 16, 2013 at 21:12

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Even worse: it's unspecified whether the value of openSlot used for indexing is the value from before or after the constructor is called.
i think your on the right track here. commented out constructor incrementation of openSlot++ and its able to continue.
confirmed - re-impelemted the other functions and it works properly now with openSlot++ after the creation & not in the constructor.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.