473,383 Members | 1,918 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Object instantiation

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
--------------------------------------------------------------
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
Jul 19 '05 #1
4 12556
WW
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
Jul 19 '05 #2
On Sun, 21 Sep 2003 18:11:48 GMT, "MA" <as***********@nowhere.nospam>
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
Jul 19 '05 #3
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
Jul 19 '05 #4

"MA" <as***********@nowhere.nospam> wrote in message
news:ER*******************@newsc.telia.net...
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.
Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Greg Freeman | last post by:
I'm missing something obvious with C++ instantiation of .NET objects. (accessing managed code from unmannaged code) Anyone suggest good examples in C++ (not C#)? I'm trying to create an...
7
by: Dave L | last post by:
I am inquiring about the feasability of being able to determine when an object of a certain type is instantiated. I have a base type that I want to be able to determine when each instance is...
7
by: johny smith | last post by:
Based on my experience there is two ways to instantiate an object. Method 1: Car* car1 = new Car();
10
by: cody | last post by:
would it be possible to allow an extended syntax for Object instantiation so that instead of Hashtable<string,int> table = new Hashtable<string,int>(10, 10f); I can write: ...
1
by: Agoston Bejo | last post by:
Hi! This is the smallest example I was able to create to demonstratet the problem. It seems that a certain kind of object instantiation gets parsed as a function definition (or whatever) by the...
7
by: John A Grandy | last post by:
I'm trying to get a decent idea of the relative performance of three types of implementations of data-access classes in ASP.NET 2.0. I believe this boils down to a more basic question regarding...
8
by: v4vijayakumar | last post by:
Is there any way to defer instantiation of " t1" to the constructor of "test1"? because, "t1" can only meaningfully initialized in the "test1" constructor. #cat test.cpp #include <iostream>...
4
by: Søren M. Olesen | last post by:
Hi Is it (any way) possible using reflection to set an indicator (property, variable, attribute,..). on an object before it actually instantiated, so that this indicator can be used in the...
6
by: Ritesh Raj Sarraf | last post by:
Hi, I've been very confused about why this doesn't work. I mean I don't see any reason why this has been made not to work. class Log: def __init__(self, verbose, lock = None): if verbose...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.