I have not found anything that addresses these questions. Any help is appreciated. If I have a class that has pointers to objects of a user-defined class as members, is there any problem with initializing those pointers with the initialization list of the constructor? For example, where A, B, and C are all user-defined classes:

class A{
    public:
        //Constructor
        A(B *b, C *c) : member1(b), member2(c){}

    private:
        B *member1;
        C *member2;
};

Also, is there any problem with initializing members of a class and passing parameters to the base class constructor in the same initialization list? For example:

class Base{
    public:
        //Constructor
        Base(foo f) : baseMember(f){}

    private:
        foo baseMember;
};


class Derived: public Base{
    public:
    //Constructor
    Derived(foo f, bar b) : Base(f), derivedMember(b){}

    private:
        bar derivedMember;
};

is there any problem with initializing those pointers with the initialization list of the constructor?

No. It's perfectly fine. But you need to know what to do with the pointers once your object contains them (is your object responsible for deleting it? (hint: it cannot).. can you guarantee that the objects pointed to will always exist during the life-time of your object? (hint: you can't).. etc.). So, there are problems associated with using raw-pointers, but nothing related to initializing them in an initialization list (in fact, that is recommended). For more info on the ownership of objects and the dangers of raw pointers, consider reading my tutorial on the subject.

is there any problem with initializing members of a class and passing parameters to the base class constructor in the same initialization list?

No. This is exactly what initialization lists are great for. In fact, you can't pass parameters to a base class constructor in any other way. And I don't understand what you mean by "in the same initialization list?". There is just one initialization list in a constructor, and it is called a "list" for a good reason, you can do many initializations of base-classes and data members (formally called "sub-objects"), as long as none appear twice in the list (of course!).

Thanks, Mike. By saying "in the same initialization list" I meant to emphasize that I was mixing the initialization of the base class and data members of the derived class in the initialization list. All of the examples I have seen dealt with initializing one or the other but not both at the same time, and I could find no examples of initializing pointers in the initialization list. For the project I am working on at the moment, there is no problem with using raw pointers, but I will definitely give your tutorial a read. I have seen mention of smart pointers before in other places, and I find it odd that they are not mentioned in my C++ text books. Thanks again.

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.