Object instantiation

Discussion in 'C++' started by MA, Sep 21, 2003.

  1. MA

    MA Guest


    I've a question that I really don't know how to do a search for on the
    internet. The project I'm working on now is one of my first
    C++-projects, earlier I've been using C and Java (and VB when forced

    According to my C++ book theese instantiations are equivalent:
    MyClass mc(a, b);
    MyClass mc = MyClass(a, b);

    In my project I have a class named FileReader which I'm trying to
    instantiate. Using this method it works fine:
    char *filename = "c:/temp/testfile.txt";
    FileReader fr(filename, 10);

    ....but if I use this method it doesn't:
    char *filename = "c:/temp/testfile.txt";
    FileReader fr = FileReader(filename, 10);

    The error messages from the compiler are the following:
    g++.exe -D__DEBUG__ -c main.cpp -o
    ain.o -I"C:/Dev-Cpp/include/c++" -I"C:/Dev-Cpp/include/c++/mingw32" -I
    "C:/Dev-Cpp/include/c++/backward" -I"C:/Dev-Cpp/include" -g3

    C:/Dev-Cpp/include/c++/bits/ios_base.h: In copy constructor
    `std::basic_ios<char, std::char_traits<char> >::basic_ios(const
    std::basic_ios<char, std::char_traits<char> >&)':
    std::ios_base&)' is private
    main.cpp:92: within this context

    C:/Dev-Cpp/include/c++/streambuf: In copy constructor
    std::char_traits<char> >::basic_filebuf(const
    std::char_traits<char> >&)':
    C:/Dev-Cpp/include/c++/streambuf:486: `std::basic_streambuf<_CharT,
    _Traits>::basic_streambuf(const std::basic_streambuf<_CharT,
    [with _CharT = char, _Traits = std::char_traits<char>]' is private
    main.cpp:92: within this context

    make.exe: *** [main.o] Error 1

    Execution terminated
    MA, Sep 21, 2003
    1. Advertisements

  2. MA

    WW Guest

    They are not. The latter form might create an unnamed, temporary MyClass
    instance and then use the copy constructor to initialize the one named mc.
    This is what happening in your case.
    WW, Sep 21, 2003
    1. Advertisements

  3. MA

    tom_usenet Guest

    Direct initialization
    Copy initialization.

    The two are slightly different. Although they will generally generate
    the same code, the second requires an accessible copy constructor.
    Streams aren't copyable, for a start.

    tom_usenet, Sep 22, 2003
  4. MA

    Attila Feher Guest

    Correction, tom_usenet is - of course - right and I was not exact enough.
    It does not really matter if the final code will or will not create that
    temporary. In any case the object must be copyable (must have an accessible
    copy constructor at that scope).
    Attila Feher, Sep 22, 2003
  5. MA

    jeffc Guest

    No, I wouldn't say that. In the first case, there is only one object
    involved. In the second, you are creating one object and then copying it
    into a second. This case is different from a simpler case where a literal
    is involved.

    int i (1);
    int i = 1;
    Those 2 are equivalent. I don't know why you're getting an error - it
    depends on the implementation of that class. Use the one that works :)
    Hopefully, there is some documentation on using the class that you can read.
    jeffc, Sep 22, 2003
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.