1

Usually I use std::unique_ptr to convey ownership and pass in raw pointers as parameters when no ownership is implied. However, I almost never consider a nullptr to be acceptable as an argument, so it would be nice to use references to avoid this, but I'm not sure most people would expect that the object the parameter references needs to exist for the lifetime of the struct A without looking at the source code.

struct A {
    A(B& b)
    :  bPtr(&b) {;}

    void method() {
        // use bPtr
    }

    B* bPtr;
};

The questions are:

1) Is the only option here to stick with pointers and always check for nullptr?

3) Does the fact that A needs bPtr to be valid mean it should take a shared_ptr? If so I feel like I would use shared pointers practically everywhere.

2) Are there conventions that can help convey lifetime requirements for parameters to users besides comments and documentation?

  • recommended reading: Why is asking a question on “best practice” a bad thing? – gnat Feb 20 '14 at 6:26
  • Is there a stack exchange where I can ask for input from other more experienced programmers without causing alarm? Sometimes people working completely on their own need advice, and I generally find the users of StackExchange to be more knowledgable than on c++ forums. I'd be more than happy to migrate my question somewhere else. – user487100 Feb 20 '14 at 7:14
  • 2
    @user487100: Your question is fully on topic here. It is just that the phrase 'best practice' triggered a reaction. – Bart van Ingen Schenau Feb 20 '14 at 7:32
1

From a C++ language standpoint, your code is fine.

As a C++ user, it depends very much on the semantics of struct A if I would expect it to maintain a reference/pointer to B, make a copy of it or to use the constructor parameter only in the constructor.

My general expectations are (all unless indicated otherwise):

  • If a parameter is passed by (smart) pointer, then the class will likely keep a reference to it
  • If a parameter is passed by (smart) pointer and there is no setter for it, then the class will likely not deal well with null pointers. This can be reinforced by a comment.
  • If a parameter is passed by const reference, then it is safe to pass a temporary object. The receiving object will either make a copy or not use the parameter beyond the constructor.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.