Object instantiation

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

  1. MA

    MA Guest

    Hello,

    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
    to).

    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> >&)':
    C:/Dev-Cpp/include/c++/bits/ios_base.h:421:
    `std::ios_base::ios_base(const
    std::ios_base&)' is private
    main.cpp:92: within this context

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

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

    Execution terminated
    --------------------------------------------------------------
    There probably are more information that is relevant, but at the moment
    I don't know what. I will gladly add this information if asked to.

    Best regards, Magnus
     
    MA, Sep 21, 2003
    #1
    1. Advertising

  2. MA

    WW Guest

    MA wrote:
    > Hello,
    >
    > 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
    > to).
    >
    > According to my C++ book theese instantiations are equivalent:
    > MyClass mc(a, b);
    > MyClass mc = MyClass(a, b);


    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 aka Attila
     
    WW, Sep 21, 2003
    #2
    1. Advertising

  3. MA

    tom_usenet Guest

    On Sun, 21 Sep 2003 18:11:48 GMT, "MA" <>
    wrote:

    >Hello,
    >
    >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
    >to).
    >
    >According to my C++ book theese instantiations are equivalent:
    >MyClass mc(a, b);


    Direct initialization

    >MyClass mc = MyClass(a, b);


    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
     
    tom_usenet, Sep 22, 2003
    #3
  4. MA

    Attila Feher Guest

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

    >
    > 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.


    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 aka WW
     
    Attila Feher, Sep 22, 2003
    #4
  5. MA

    jeffc Guest

    "MA" <> wrote in message
    news:ERlbb.29517$...
    > Hello,
    >
    > 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
    > to).
    >
    > According to my C++ book theese instantiations are equivalent:
    > MyClass mc(a, b);
    > MyClass mc = MyClass(a, b);


    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
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Greg

    Instantiation of object fails

    Greg, Oct 21, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    313
    Dmitriy Lapshin [C# / .NET MVP]
    Oct 22, 2003
  2. Gandu
    Replies:
    5
    Views:
    8,475
    Lee Fesperman
    Jan 28, 2004
  3. ben
    Replies:
    6
    Views:
    508
    Ryan Stewart
    Apr 4, 2004
  4. Mario
    Replies:
    16
    Views:
    688
    Eric Sosman
    Jul 22, 2005
  5. Replies:
    1
    Views:
    590
    Salt_Peter
    Dec 25, 2006
Loading...

Share This Page