This might be (in fact I can probably say it is) a really dumb question but it's been doing my head in since I've been looking at it. First off, I've been looking at C++ for a short time and it's confusing to me more than what Java ever was. I have an assignment that involves C++ and one of the requirements from a .h file reads;

    /**
     * Constructor that will build an initial screen to hold the
     * specified array of registers.
     */
    Screen(Register* registers, int numRegisters);

Now this is the dumb bit; I don't understand what it's asking to be done? The register .h file is basically a struct holding data such as strings, int's, etc. Could someone put this into layman's terms for me and point me in the right direction of what I would need to do? This is from main.cpp with said constructor in use;

int numRegisters = 5;
Register *registers = Setup::buildRegisters(numRegisters);

// Create the screen and add/set the registers

Screen screen(registers, numRegisters);

Apologies for wasting anyones time who reads this.

It's hard to say without knowing the purpose of the Screen class. What is a "screen" in this context? The call to the constructor suggests one of two things:

  1. The Screen object will take ownership of a pointer to the registers created by the caller:

     Screen::Screen(Register* registers, int numRegisters)
         : _registers(registers), _numRegisters(numRegisters)
     { }
    
  2. The Screen object will make a copy of the given "array" (many possible variations):

     Screen::Screen(Register* registers, int numRegisters)
     {
         _registers = new Register[numRegisters];
    
         for (int i = 0; i < numRegisters; i++) {
             _registers[i] = registers[i];
         }
     }
    

The Screen class will basically be used to create statistics based on the struct in the Register.h file. The problem description is;

"Within a system, details of registrations are held within the shown Register structure. A significant number of registers occur each day. At the end of each day, a large array holding details of the registers is passed to a screening program that will provide some useful statistics."

And the Screen.cpp class has a number of functions such as averages of certain things, etc. Such as;

    Screen.h
    /**
     * Functions that permit the registers stored within the screening class to
     * be modified. setRegisters will completely replace any existing
     * registers with those specified. addRegistess will add 
     * additional registers to those already specified.
     */
    void setRegisters(Register* registers, int numRegisters);
    void addRegistes(Register* registers, int numRegisters);

    main.cpp
    screen.setRegisters(someRegisters, numRegisters);   
    screen.addRegistes(moreRegisters, numRegisters);

But looking at what you've done I was kind of thinking along the right lines only I was doing registers = new Register[numRegisters]; like in Java. I still haven't got my head around pointers which I think is the problem.

I still haven't got my head around pointers which I think is the problem.

Every object reference you used in Java...was a pointer. The only differences are Java references don't use explicit dereferencing operators, and Java references are restricted in terms of things like pointer arithmetic. That may help you conceptually.

Screen::Screen(Register* registers, int numRegisters)
{
    Register* _registers(registers);
    int _numRegisters(numRegisters);
}

Just to double check, that's what you meant by your example right? So it's in a way like Java it would be similar to this.numRegisters = numRegisters on a constructor?

Just to double check, that's what you meant by your example right?

No, I used _registers and _numRegisters as names for data members in the class:

class Screen {
    Register* _registers;
    int _numRegisters;
public:
    Screen(Register* registers, int numRegisters);
};
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.