473,394 Members | 2,168 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,394 software developers and data experts.

Visual Studio C++ .NET 2003 Compiler optimization (Bug ?)

using namespace std;
class foobar {
public:
foobar() { cout << "foobar::foobar()\n"; }
~foobar() { cout << "foobar::~foobar()\n"; }
foobar(int nval ):_ival(nval) { cout << "foobar::foobar(int)\n"; }
foobar( const foobar &rhs ) { cout << "foobar::foobar( const foobar
& )\n"; }
operator int(){ cout<<"foobar::operator int()"<<endl; return _ival; }
private:
int _ival;
};
foobar f( int val )
{
return foobar();
}

void main()
{
foobar foo = f(1024);
}
In the above program,

I see 3 different behaviors:
1. Run the program as it is, only one foobar object is created.
2. Remove the const qualifier from the copy constructor, there are 3 foobar
objects created. Strange thing is that "operator int()" method is used while
copying data to temporary objects. Why?
3. commenting the "operator int()" method will result back in only one
foobar object creation.

Could anyone kindly explain the behavior for me?


Jan 25 '06 #1
1 1008
On Wed, 25 Jan 2006 10:35:28 +0530, "Piggy" <Gi****@yahoo.co.in> wrote:
using namespace std;
class foobar {
public:
foobar() { cout << "foobar::foobar()\n"; }
~foobar() { cout << "foobar::~foobar()\n"; }
foobar(int nval ):_ival(nval) { cout << "foobar::foobar(int)\n"; }
foobar( const foobar &rhs ) { cout << "foobar::foobar( const foobar
& )\n"; }
operator int(){ cout<<"foobar::operator int()"<<endl; return _ival; }
private:
int _ival;
};
foobar f( int val )
{
return foobar();
}

void main()
{
foobar foo = f(1024);
}
In the above program,

I see 3 different behaviors:
1. Run the program as it is, only one foobar object is created.

2. Remove the const qualifier from the copy constructor, there are 3 foobar
objects created. Strange thing is that "operator int()" method is used while
copying data to temporary objects. Why?
3. commenting the "operator int()" method will result back in only one
foobar object creation.

Could anyone kindly explain the behavior for me?


I'm feeling a strong sense of déjà vu here, and no wonder! You posted the
same message back on Jan 9 in microsoft.public.vc.language and got several
good replies there on that same day:

http://groups.google.com/group/micro...cb0b8834b550c6

--
Doug Harrison
Visual C++ MVP
Jan 25 '06 #2

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

Similar topics

6
by: Martin Bless | last post by:
The good news: Along with Python-2.4 comes really good news to Windows users. Yes, you now CAN build extension modules yourself using the SAME C++ compiler and linker Python is built with...
1
by: Novice | last post by:
Hi all, I'm afraid this is the second posting of this information as I didn't get a response on the previous post. I will try to shorten my message (i.e. be more concise) in the hopes that it will...
5
by: Anton Noll | last post by:
We are using Visual Studio 2003.NET (C++) for the development of our software in the fields digital signal processing and numerical acoustics. One of our programs was working correctly if we are...
1
by: Novice | last post by:
Hi all, I'm afraid this is the second posting of this information as I didn't get a response on the previous post. I will try to shorten my message (i.e. be more concise) in the hopes that it will...
0
jwwicks
by: jwwicks | last post by:
Introduction This tutorial describes how to use Visual Studio to create a new C++ program, compile/run a program, resume work on an existing program and debug a program. It is aimed at the...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.