Passing optional auto_ptr to function

This is a discussion on Passing optional auto_ptr to function within the C++ Programming forums, part of the General Programming Boards category; Does anyone know if it is possible to pass an optional auto_ptr to a function? I want to pass in ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    38

    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

    Code:
    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??
    Thanks
    Mark

  2. #2
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    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
    Posts
    7,318
    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:
    Code:
    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, 02: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, 08:24 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21