a confusable question in VIsual C++

G

Guest

I'm a Chinese student
I found a very confusable proble
when I studied Visual C++ 6.0
The problem is

#include <iostream.h

class confusable
private
int x,y

public
confusable(int x1,int y1)
x=x1; y=x1
cout<<"In construct !\n"


confusable(confusable & obj)
*this=obj
cout<<"In copy construct !\n"


// 下é¢çš„æžæž„函数 有与没有 令人奇怪 !!
//very confusable below
//~confusable( ){ cout<<"deconstruct ... \n";
}

void main(


{ // 为了便于观察,故æ„将对象数组设置为局部å˜é‡
confusable confuse[2]={ confusable(1,1),confusable(2,2) }





/
1. If I don't define ~confusable( ),the resul
is below

In construct
In copy construct
In construct
In copy construct

2.If I define ~confusable( ),the resul
is below

In construct
In construct
deconstruct ..
deconstruct ..

question
Where is the "In copy construct !"
In my opinion
The result should be

In construct
In copy construct
In construct
In copy construct
deconstruct ..
deconstruct ...

Please tell me why
The reason please!
Easily and clearly

*
 
B

Bronek Kozicki

#include <iostream.h>
You are using deprecated header, change it to:
confusable(confusable & obj){
*this=obj;
bad! "this" has not been constructed yet, you may not assign to it!
Instead you should perform:
x=obj.x; y=obj.y;
1. If I don't define ~confusable( ),the result
is below:

In construct !
In copy construct !
In construct !
In copy construct !

result will be different in optimized build of your program.
Where is the "In copy construct !"?

It has been optimized away. Compiler is allowed per language rules to
optimize away copy-construction of object in many situations.

Your problem is not with Visual C++, but with C++ language itself.
Visual C++ is not doing anything unusual here. Look for good handbooks
of C++ (not "Visual C++", as there is no such language and to use
compiler properly you need to know language first).


B.
 

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

Top