Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9141625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:40:11+00:00 2026-06-17T09:40:11+00:00

I have a pretty standard class with some public member functions and private variables.

  • 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++;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T09:40:12+00:00Added an answer on June 17, 2026 at 9:40 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My problem: require_once '/includes/aws-sdk-1.5.2/sdk.class.php'; My environment: I have a pretty standard PHP site that
I have some js (show/hide pretty standard I guess): <script> function showTransactions(transactions){ $('.steps').hide() $('#'
this is just some pretty standard code I have tried. What I am trying
I have pretty standard Qmail toaster installation. I'm using the dot files to set
I have a pretty standard database sitting on SQL Server. To manage the data
I have a pretty standard Repository pattern going, where repositories are injected into my
I have a pretty standard .NET MVC 3 application that uses Ninject for dependency
I do not think this is a duplicate. I have done some reading but
I have a classic Apple reachability class which is not adapted to ARC. It
Hello guys i have a pretty standard div with css and i would like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.