gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

C/C++ > Need help with initializing an array in a C++ constructor

#53629 - LOst? - Fri Sep 09, 2005 11:37 pm

Code:

class CTest
{
private:
   char TestArray [4];

public:
   CTest ();
}

CTest::CTest ()
:   TestArray (?????????)
{


}


I want to initialize all 4 array elements in TestArray to the number 3. How do I do this in propper C++ syntax?
_________________
Exceptions are fun

#53638 - sajiimori - Sat Sep 10, 2005 3:33 am

You can't do it in the initializer list, but that's okay in this case because chars don't have a default constructor, so there's no redundancy by initializing them in the body of the constructor.

#53640 - LOst? - Sat Sep 10, 2005 4:07 am

sajiimori wrote:
You can't do it in the initializer list, but that's okay in this case because chars don't have a default constructor, so there's no redundancy by initializing them in the body of the constructor.


But what if i have more variables to initialize in the constructor initializer list? How do I skip TestArray?
_________________
Exceptions are fun

#53641 - jumperwillow - Sat Sep 10, 2005 4:07 am

I don't know exactly what you mean by "proper," but this will get the job done.

Code:

class CTest
{
private:
   char TestArray [4];

public:
   CTest ();
}

CTest::CTest ()
{
   int i;
   for(i=0; i<4; i++)
      TestArray[i] = '3';
}

#53644 - LOst? - Sat Sep 10, 2005 4:27 am

This is the problem:
Code:

class CTest
{
private:
   int TestInteger;
   char TestArray [4];
   bool TestBoolean;
   char TestChar;

public:
   CTest ();
}

CTest::CTest ()
:   TestInteger (5),
    ERRORERRORERRORERRORERROR,
    TestBoolean (true),
    TestChar ('A')
{
}

_________________
Exceptions are fun

#53647 - jumperwillow - Sat Sep 10, 2005 5:24 am

LOst? wrote:
This is the problem:
Code:

class CTest
{
private:
   int TestInteger;
   char TestArray [4];
   bool TestBoolean;
   char TestChar;

public:
   CTest ();
}

CTest::CTest ()
:   TestInteger (5),
    ERRORERRORERRORERRORERROR,
    TestBoolean (true),
    TestChar ('A')
{
}


What exactly are you trying to accomplish here?

The Syntax you are using vaguely resembles C++ Inheritance, but there's no actual inheritance going on here...

#53650 - LOst? - Sat Sep 10, 2005 7:06 am

jumperwillow wrote:
LOst? wrote:
This is the problem:
Code:

class CTest
{
private:
   int TestInteger;
   char TestArray [4];
   bool TestBoolean;
   char TestChar;

public:
   CTest ();
}

CTest::CTest ()
:   TestInteger (5),
    ERRORERRORERRORERRORERROR,
    TestBoolean (true),
    TestChar ('A')
{
}


What exactly are you trying to accomplish here?

The Syntax you are using vaguely resembles C++ Inheritance, but there's no actual inheritance going on here...


I am just wondering what I should put instead of ERRORERRORERRORERRORERROR to not generate any compiler errors or warnings.
_________________
Exceptions are fun

#53651 - jumperwillow - Sat Sep 10, 2005 7:33 am

LOst? wrote:
jumperwillow wrote:
LOst? wrote:
This is the problem:
Code:

class CTest
{
private:
   int TestInteger;
   char TestArray [4];
   bool TestBoolean;
   char TestChar;

public:
   CTest ();
}

CTest::CTest ()
:   TestInteger (5),
    ERRORERRORERRORERRORERROR,
    TestBoolean (true),
    TestChar ('A')
{
}


What exactly are you trying to accomplish here?

The Syntax you are using vaguely resembles C++ Inheritance, but there's no actual inheritance going on here...


I am just wondering what I should put instead of ERRORERRORERRORERRORERROR to not generate any compiler errors or warnings.


Why are you initializing your class in the function header though? You are better off just initializing your variables in the actual constructor...
Code:

class CTest
{
private:
   int TestInteger;
   char TestArray [4];
   bool TestBoolean;
   char TestChar;

public:
   CTest ();
}

CTest::CTest ()
{
    int i;
    TestInteger=5;
    for(i=0; i<4; i++)
        TestArray[i]='3';
    TestBoolean=true;
    TestChar='A';
{
}

_________________
-h

#53705 - sajiimori - Sat Sep 10, 2005 8:32 pm

Jumperwillow, it's called an initializer list, and it's often critical for efficient object construction.

Lost, just skip the array and initialize the other variables, then fill the array in the constructor body. The compiler won't complain.

#53721 - LOst? - Sun Sep 11, 2005 4:08 am

sajiimori wrote:
Jumperwillow, it's called an initializer list, and it's often critical for efficient object construction.

Lost, just skip the array and initialize the other variables, then fill the array in the constructor body. The compiler won't complain.


Thank you for that! I will.
_________________
Exceptions are fun