-
March 5th, 2008, 02:09 AM
#1
Can I initialize a private data member within the class constructor?
I have class 'ClassA' which has a constructor that takes an int, but no default constructor. Then, I have 'ClassB' which contains a private data member 'myArray[8]' of type 'ClassA'. If I initialize all 8 elements of 'myArray' within 'ClassB's constructor, is that good enough? Or must I initialize them in the private data member section? For example:
class ClassA {
public:
ClassA(int val) : myVal(val) {}
private:
int myVal;
};
class ClassB {
public:
ClassB() {
for(int i=0; i<8; i++)
myArray[i] = i; // will this satisfy ClassA's constructor?
}
private:
ClassA myArray[8]; // or must in initialize here?
};
Thanks in advance for your help and patience. I'm obviously still a beginner at this stuff.
Last edited by RobotJones; March 5th, 2008 at 02:13 AM.
Reason: hit <ENTER> too soon
-
March 5th, 2008, 02:19 AM
#2
Re: Can I initialize a private data member within the class constructor?
I do not think the code will compile: you need to provide a default constructor for ClassA.
By the way, kindly post your code within code tags.
-
March 5th, 2008, 02:52 AM
#3
Re: Can I initialize a private data member within the class constructor?
Originally Posted by RobotJones
I have class 'ClassA' which has a constructor that takes an int, but no default constructor. Then, I have 'ClassB' which contains a private data member 'myArray[8]' of type 'ClassA'. If I initialize all 8 elements of 'myArray' within 'ClassB's constructor, is that good enough? Or must I initialize them in the private data member section? For example:
class ClassA {
public:
ClassA(int val) : myVal(val) {}
private:
int myVal;
};
class ClassB {
public:
ClassB() {
for(int i=0; i<8; i++)
myArray[i] = i; // will this satisfy ClassA's constructor?
}
private:
ClassA myArray[8]; // or must in initialize here?
};
Thanks in advance for your help and patience. I'm obviously still a beginner at this stuff.
Coming to your question one by one.
Originally Posted by RobotJones
If I initialize all 8 elements of 'myArray' within 'ClassB's constructor, is that good enough?
You are not initializing but you are assigning. Confusing..?? I try to make it clear.
When you create objcet of your class ClassB constructor will be called allright..???
But before body of your constructor gets executed which is.
Code:
for(int i=0; i<8; i++)
myArray[i] = i; // will this satisfy ClassA's constructor?
All the members as well as base clas part of your class ClassB would be initialized. Hence in your constructor body what you are trying to do is assignment and not initialization...
Generally people use contructor's initialization list for that.
But since you have not used constructor initialization list all the members of your class will have default initialization allright?
So befor you enter the body your ClassA myArray[8]; should be already initialized.
But ClassA does not have default constructor then how elements held in your myArray undergo default initialization..It would not.
Hence your compilar will give you a compilation error. That there is no sutiable constructor..
Rightly mentioned by laserlight.
Regards
Prashant.
Dont forget to rate my post if you find it useful.
-
March 5th, 2008, 10:00 AM
#4
Re: Can I initialize a private data member within the class constructor?
Thank you both for your help, I believe I understand now. And in the future I'll be sure to put my code inside of code tags. Sorry about that.
So here's my follow-up question: If I were to initialize myArray in the initialization list rather than assign it within the body of the constructor, would that work?
If so, what is the correct way to initialize an array inside an initialization list? Similar to initializing an array anywhere else, but with parentheses rather than curly brackets, and without the assign operator (=)?
Code:
ClassB
{
public:
ClassB()
: myArray(1, 2, 3, 4, 5, 6, 7, 8)
{
}
private:
ClassA myArray[8];
};
Or would I have to initialize each element separately like so:
Code:
ClassB
{
public:
ClassB()
: myArray[0](1), myArray[1](2), myArray[2](3), //etc...
{
}
private:
ClassA myArray[8];
};
Thanks again for your help.
-
March 5th, 2008, 10:11 AM
#5
Re: Can I initialize a private data member within the class constructor?
If I were to initialize myArray in the initialization list rather than assign it within the body of the constructor, would that work?
The problem is that you can only initialize in the initialization list and assign to member variables in the body of the constructor.
If so, what is the correct way to initialize an array inside an initialization list?
Unfortunately, such initialization is (currently) not possible.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|