Object instantiation

M

MA

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
 
W

WW

MA said:
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.
 
T

tom_usenet

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
 
A

Attila Feher

WW said:
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).
 
J

jeffc

MA said:
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.
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top