473,508 Members | 2,360 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

(VC7) Basic Object Const/Dest question

ibm
I am running a simple test

/*******CODE************/
class A
{
int _x;
public:
A(int i)
{
_x = i;
printf("A %d\n", _x);
}
A(const A& a)
{
_x = a._x;
printf("CopyA %d\n", _x);
}
~A()
{
printf("~A %d\n", _x);
}
};

A& f1()
{
A a(1);
return a;
}
A& f2()
{
return A(2);
}
A f3()
{
A a(3);
return a;
}
A f4()
{
return A(4);
}

int _tmain(int argc, _TCHAR* argv[])
{
A a1 = f1();
A a2 = f2();
A a3 = f3();
A a4 = f4();
printf("destroying all\n");
return 0;
}
/*******OUTPUT**********/
A 1
~A 1
CopyA -85899346 //copycon for 1
A 2
~A 2
CopyA -85899346 //copycon for 2
A 3
CopyA 3 //copycon for 3
~A 3
A 4 //copycon for 4
destroying all
~A 4
~A 3
~A -858993460
~A -858993460
-------------------------
This output is very much understandable bcz the rule is never return a
local reference from a function.

Now I remove the copy constructor from the code and get the following
ouput

/******OUTPUT with copycon ***********/
A 1
~A 1
A 2
~A 2
A 3
~A 3
A 4
destroying all
~A 4
~A 3
~A 2
~A 1
--------------------------------
Q1, if the rule "never return a local reference from a function"
remains intact than is this ( ~A 2, ~A 1 after destroying all )
something to do with how VC++ compiler is handling it. Also I think in
some compilers returning a reference from a function returns a compiler
error.
Q2, Why in both the codes a destructor for A 4 is not called before
destroying all statement that is how exactly is A a;return a;
differnt from return A;

thanks

Jul 23 '05 #1
1 1251
ibm wrote:
[...] --------------------------------
Q1, if the rule "never return a local reference from a function"
remains intact than is this ( ~A 2, ~A 1 after destroying all )
something to do with how VC++ compiler is handling it. Also I think in
some compilers returning a reference from a function returns a compiler
error.
I am sure I don't understand your question (if there is one). Returning
a reference to a local object is legal but causes undefined behaviour *if*
the reference is used. Compilers are not required to identify undefined
behaviour. If some do, that's fine. If some don't, that's just as OK.
Q2, Why in both the codes a destructor for A 4 is not called before
destroying all statement that is how exactly is A a;return a;
differnt from return A;


It's not different at all.

What you should also print in the destructor is the value of 'this'. You
cannot judge which object you're destroying and what led to its creation
if you can't tell if it was the one you constructed or it's a temporary to
help transfer some value from a function or during the initialisation.

V
Jul 23 '05 #2

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

Similar topics

17
2631
by: codeslinger | last post by:
What is the point of the construct in the subject line? Why the use of two const labels? Does that do something different than const void *x? And what would void * const x indicate? Thanks in...
2
1636
by: Hendrik Schober | last post by:
Hi, I have some expression template code that I want to get to work. I had a typo in it and just spent two hours to find it, because VC7.1 gave a really weird error message for it. This is the...
1
7458
by: Minh | last post by:
I've just installed VS.NET 2003 on my Athlon XP 1800+. However I couldn't get any project with STL includes to compile even if I create a new empty project (and added #include <string>). It gave me...
3
3124
by: tomrmgc | last post by:
I have some code that compiles and links fine in VC6 but does not link in VC7. Here are the relevant parts of the code: class AView : public CScrollView { .... public: AFX_DISPMAP_ENTRY*...
5
3312
by: Bill Pursell | last post by:
Suppose I have a structure with many members, and I pass a pointer to that structure to a function. I'd like the prototype of the function to specify that it will only be changing certain members...
1
10340
by: iiitsunny | last post by:
i have ported one project which was working in VC6 to VC7. Now the project is working fine in VC7 also. I have made some additions in VC7 environment only. But the problem is that the project is not...
2
5450
by: flyingxu | last post by:
Hi, I run into a cstring related link problem in VC7. My solution has 3 projects, one MFC exe, two MFC extersion DLL. the two MFC extersion DLL export functions which use CString as parameters....
10
3377
by: Erik Knudsen | last post by:
Hi! In converting applications from ansi to unicode, it is a problem that the std::wcout accepts a const char * without complaining compile time. This compiles and runs:...
2
1228
by: Oltmans | last post by:
Hi all, I'm stuck in a situation where I need help. Any help will be highly appreciated. I've created an object, after creating the object, I'm assigning one of its functions to an event handler...
0
7115
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
7321
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,...
1
7036
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...
0
5624
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4705
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
414
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.