CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Threaded View

  1. #1
    Join Date
    Dec 2007
    Posts
    13

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured