Search code examples
c++iostream

Selection of istream from cin or file with ternary operator


I would like to have a member variable of a class which stores the input stream depending on a string entered by the user. eg.

#include <string.h>
#include <iostream>
#include <fstream>

class myInput {
public:
    myInput(std::string file_name);
    myInput() = delete;

protected:
    std::istream& in_data;
};
 
myInput::myInput(std::string file_name)
    : in_data((file_name == "-") ? std::cin : std::ifstream(file_name)){}

void main() {
    myInput("file.txt");
}

However I am receiving the following errors

error: use of deleted function ‘std::basic_istream<_CharT, _Traits>::basic_istream(const std::basic_istream<_CharT, _Traits>&) [with _CharT = char; _Traits = std::char_traits]’
error: ‘std::basic_istream<_CharT, _Traits>::basic_istream(const std::basic_istream<_CharT, _Traits>&) [with _CharT = char; _Traits = std::char_traits]’ is protected within this context

I have also tried creating the ifstream object separately and assigning that to in_data, but encounter similar problems. Why is this?


Solution

  • std::istream does not allow you to do something like in_data = std::cin or in_data = std::ifstream("file.txt") as the assignment operator is deleted.

    An alternate solution is to use a pointer to std::istream.

    class myInput {
    public:
        myInput(std::string file_name);
        virtual ~myInput() { if (bIsHeapAllocated) delete in_data; } // delete the heap allocation if done.
        myInput() = delete;
    
    protected:
        std::istream* in_data = nullptr;
        bool bIsHeapAllocated = false;    // You can make this a private member if you don't want your inherited classes knowing this.
    };
    
    myInput::myInput(std::string file_name)
    {
        if (file_name == "-")
            in_data = &std::cin;
        else
            in_data = new std::ifstream(file_name), bIsHeapAllocated = true;    // Allocate the ifstream in heap
    }