Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Initialisation of Static Array in constructor 1

Status
Not open for further replies.

RichardF

Programmer
Oct 9, 2000
239
GB
Hi,

Straight to the code :

///
class cBook {
public:
cBook(int NumPages, string Title, string Author) :
iNumPages(NumPages),
cTitle(Title),
cAuthor(Author)
{
};

private:
int iNumPages;
string cTitle, cAuthor;
};

class cBookShelf
{
public:
cBookShelf();

cBook &GetBook(int Index) { return Books[Index]; }

private:
static cBook Books[20];
};

cBook cBookShelf::Books[20];
///

Im not sure if this can be done : Is there a way to initialise the array of books in the cBookShelf constructor ?

i did try :

cBookShelf::cBookShelf() :
Books[0](200,"Microsoft ADO","Microsoft"),
Books[1](500,"MSDOS Manual","Microsoft")

{
}

and i got all sorts of errors. Any ideas ?

Regards,
Rich.


 
You can't initialize arrays in the constructor initalization list, you have to do it "manually" in the code.

Code:
cBookShelf::cBookShelf()
{
   Books[0] = cBook(200,"Microsoft ADO","Microsoft"),
   Books[1] = cBook(500,"MSDOS Manual","Microsoft")
}

Due to this, the element (in this case cBook) must also have a default constructor.



/Per
Nerdy signatures are as lame as the inconsistent stardates of STTNG.
 
Oh, and should the Books member really be static?

I don't think so, in case it should, then it should not be initialized at all in the constructor (since it is then related to the class rather than the instance), it should instead be initialized directly in the implementation file.

/Per
Nerdy signatures are as lame as the inconsistent stardates of STTNG.
 
Hi,

RE: the first reply -
i thought so.

RE: the second reply -
i hear what your saying - i actually intended there to be one instance of the class anyway. And this app will be the only client of the class

Regards,
Rich.
 
> i actually intended there to be one instance of the class anyway. And this app will be the only client of the class

That is the situation today, but tomorrow you would perhaps want to extend it to manage multiple book shelves... ;-)

/Per
Nerdy signatures are as lame as the inconsistent stardates of STTNG.
 
Well Actually,

the book classes are merely a textbook example of what i trying to achieve and is in no way connected to the real world problem.

Indeed for these classes there will probally be multiple book shelves and maybe libraries :) - but I know for sure that for the real world class there will never be multiple instances.

I guess i need the whole class static !

Regards,
Richard
 
>but I know for sure that for the real world class there will never be multiple instances.

May I then suggest you make it a singleton?

>I guess i need the whole class static !
Then it's not really a class anymore ;-) but rather just a namespace with some functions...


/Per
Nerdy signatures are as lame as the inconsistent stardates of STTNG.
 
>> May I then suggest you make it a singleton?
what is a singleton, and more importantly how would i implement in vc ?

>>>>I guess i need the whole class static !
>>Then it's not really a class anymore but rather just a namespace with some functions...
yes, your right, but its nice to have all in a class.


Rich.
 
>what is a singleton
An object-orianted way to:
1) Ensure that there is at most one instance of a certain class.
2) Ensure that all calls go to the same instance.
3) Give access to this single instance from whatever place of your code (i.e. it is the "global variable" of Object Orientation).

It can be implemented in various ways, here is one of the simpler ones:

Code:
// MyClass.hpp
class MyClass
{
  public:
    // The only way to access the MyClass is through the instance method.
    static MyClass* instance()
    {
       if (!sInstance)
         sInstance = new MyClass();
       return sInstance;
    }
    
    // Other public stuff
    void doSomehitng() { ... }
      .
      .
      
  private:
     // Private constructor. Ie only MyClass static can instantiate
     MyClass() { ... }

     static MyClass* sInstance;
};

//---------------------------------------------
// MyClass.cpp

// Initialize the static member
MyClass* MyClass::sInstance=NULL;
 .
 .
 .

//---------------------------------------------
// Usage
  .
  .
  MyClass::instace()->doSomething();
  .
  .

I usually don't recommend singletons since I'm allergic to global thingies (even if they are object oriented), but if it fits in your scheme...




/Per
Nerdy signatures are as lame as the inconsistent stardates of STTNG.
 

Cool. Have a star.

Regards,

Rich.

"Programmers are tools for converting caffeine into code - BSRF"
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top