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;
};

Dani AI

Generated

Initializing pointer members in a constructor’s member-initializer list is fine; they’re just values. The bigger questions are ownership and lifetime, exactly as pointed out.

Mixing base-class and member initializers in one list is normal. Just remember the actual construction order is fixed by the language: bases first, then data members in the order they’re declared in the class, regardless of the order you write them in the list. Keeping the list in declaration order avoids surprises and warnings. Also, passing arguments to a base constructor can only be done via the member-initializer list. (en.cppreference.com)

Example: even though the list is shuffled, BaseType is constructed first, then first, then second.

struct BaseType { explicit BaseType(int) {} };

struct Widget : BaseType {
  Widget() : second(2), BaseType(42), first(1) {}
  int first;
  int second;
};

On ownership:

  • Owning: store a std::unique_ptr data member and move it into place from the constructor; if you’re creating the pointee there, build it directly in the initializer list with std::make_unique for RAII and exception safety. (en.cppreference.com)
  • Non-owning: keep a raw pointer or a reference, make it const if you don’t mutate through it, and give the member a sensible default (nullptr) with an in-class default member initializer so the object starts in a known state. (en.cppreference.com)
struct Owner {
  explicit Owner(std::unique_ptr<Resource> r) : res(std::move(r)) {}
private:
  std::unique_ptr<Resource> res;
};

struct NonOwner {
  explicit NonOwner(Service& s) : svc(&s) {}
  Service const* svc = nullptr; // default member initializer
};

Recommended Answers

All 2 Replies

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.