Thread: Passing optional auto_ptr to function

  1. #1
    Registered User
    Join Date
    Nov 2005

    Passing optional auto_ptr to function

    Does anyone know if it is possible to pass an optional auto_ptr to a function? I want to pass in an auto_ptr to a file handle sometimes, but not others. My code looks like the following

    void generateTree(PhyloTree* tree, auto_ptr<ofstream>& file = NULL);
    Then later when the function is declared I want to check if it is NULL or not. But it is not working. Any suggestions??

  2. #2
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    How is it not working? I don't see anything wrong with what you've posted so far.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  3. #3
    Registered User
    Join Date
    Jan 2005
    You cannot assign NULL to a reference, even an auto_ptr reference. You can make it a const reference and assign a default constructed auto_ptr:
    void generateTree(PhyloTree* tree, const auto_ptr<ofstream>& file = auto_ptr<ofstream>());
    Although, there might be a better way. What do you do if it is null? If you write to cout, I would just take an ostream& and pass the ofstream or cout (or an ostringstream) as necessary.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. Passing A Function Into A Constructor
    By fourplay in forum C++ Programming
    Replies: 6
    Last Post: 03-15-2009, 06:06 AM
  4. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  5. Passing a byte and its bits to a function
    By rtarbell in forum C Programming
    Replies: 9
    Last Post: 12-04-2008, 09:24 AM