|
is there any technical difference between invoking a constructor using
class obj(5,20) and class obj=class(10,40).
Thanks in advance
|
|
|
|
|
philiptabraham wrote: can anybody help me out
Not if you don't read the sticky posting called "how to get answers", and the line that says "please use meaningful subjects". I've yet to see someone post a message with a subject of "ignore this - I don't need any assistance".
But I'll be nice anyway..
1/ The numbers are different, so there will be differences.
2/ Less trivially, the first example just calls the constructor. The second example makes two objects, (obj, and a temp one), then copies temp to obj, then destroys the temp object.
If it's CPoint, then it's trivial. If it opens / closes databases, does a network operation etc, then the difference could be HUGE.
Iain,
-------
Update: See CPallini's and my conversation later. Short version: No actual difference between the two bits of code (except the numbers!)
modified on Friday, February 01, 2008 5:13:33 AM
|
|
|
|
|
|
Cedric Moonen wrote: I don't need any assistance
Sure?
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
[my articles]
|
|
|
|
|
I knew *someone* would bite - but I assumed it would be Signor Pallini. And he's the one who gave a sensible reply!
Iain.
|
|
|
|
|
Iain Clarke wrote: I knew *someone* would bite
WOW I missed something, anyway usually Cedric is a very nice guy.
Iain Clarke wrote: but I assumed it would be Signor Pallini
Pallini is a quite docile guy too, isn't he?
BTW Signor in that context was fantastic!
Iain Clarke wrote: And he's the one who gave a sensible reply!
Oh, well, hence I can assume he is.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
[my articles]
|
|
|
|
|
Well, it's friday That explains a lot
|
|
|
|
|
Iain Clarke wrote: 2/ Less trivially, the first example just calls the constructor. The second example makes two objects, (obj, and a temp one), then copies temp to obj, then destroys the temp object.
I thought the same. But I made a little test (optimization disabled):
#include <iostream>
using namespace std;
class MyClass
{
public:
int _a, _b;
MyClass(){}
MyClass(int a, int b):_a(a), _b(b){}
MyClass(const MyClass & proto)
{
_a = 0;
_b = 0;
}
MyClass & operator = (const MyClass & proto)
{
_a = proto._b;
_b = proto._a;
return *this;
}
};
void main()
{
MyClass my1(3,2);
MyClass my2=MyClass(3,2);
MyClass my3(my1);
MyClass my4;
my4=my1;
cout << "my1: " << my1._a << " " << my1._b << endl;
cout << "my2: " << my2._a << " " << my2._b << endl;
cout << "my3: " << my3._a << " " << my3._b << endl;
cout << "my4: " << my4._a << " " << my4._b << endl;
}
</iostream>
and to my surprise, the output:
my1: 3 2
my2: 3 2
my3: 0 0
my4: 2 3
Well I'm really upset about. What do you think?
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
[my articles]
|
|
|
|
|
It's because the compiler will treat this:
CMyCLass my1 = my2;
As this:
CMyClass my1(my2);
If you would have done that operation on two lines, then the assignment operator would have been called.
|
|
|
|
|
Nope,
the compiler treats
MyClass my2 = MyClass(3,2);
as it was
MyClass my2(3,2);
(I've messed up both copy constructor and assignment operator to detect it)
and IMHO skipping the temporary object construction and the assignment operator is really weird, but I don't know what the C++ standard requires.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
[my articles]
|
|
|
|
|
CPallini wrote: What do you think?
Well, I immediately quibbled your tests, so I did some of my own...
class MyClass
{
public:
int _a, _b;
MyClass()
{
TRACE0("Blank Constructor\n");
}
~MyClass()
{
TRACE0("Destructor\n");
}
MyClass(int a, int b):_a(a), _b(b)
{
TRACE2("Parameter Constructor(%i,%i)\n", a,b);
}
MyClass(const MyClass & proto)
{
_a = 0;
_b = 0;
TRACE0("Copy Constructor\n");
}
MyClass & operator = (const MyClass & proto)
{
_a = proto._b;
_b = proto._a;
TRACE0("Copying\n");
return *this;
}
};
void main()
{
MyClass my1(3,2);
MyClass my2=MyClass(30,20);
}
And I got:
Parameter Constructor(3,2)
Parameter Constructor(30,20)
which came as a suprise. So, the second case is being "Optimised" - even with no compiler optimisation. So C++ is treating the two cases as grammatically equivalent.
This is why I help here - I learn something by code-spelunking too!
Iain.
|
|
|
|
|
Now you have to fix all that database and network stuff!
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
[my articles]
|
|
|
|
|
Is there a Windows API or a method which can convert FILE* to HANDLE of a file or convert HANDLE to FILE* ???
The background of application :I want to monitor the I/O of a special file by hooking WriteFile and the file be opened by 'fopen'.In addition, I can`t modify the function form 'fopen' to 'CreateFile'. Therefore I can`t know which handle of file should be monitored according to the first parameter of WriteFile.So I hope there is a way that can convert HANDLE or FILE* to another.
If you know it ,please tell me .
Thank you.
|
|
|
|
|
I made a line
FILE *p = fopen ("foo", "rw");
and kept debugging into functions until I found the C runtime using an array of information called _osfhnd which stored the mapping between stream opened by _tsopen and HANDLE . This stream is the _file member of FILE .
_get_osfhandle
http://msdn2.microsoft.com/en-us/library/ks2530z6.aspx[^]
The below code omits any error checking whatsoever....
HANDLE FileToHandle (FILE *f)
{
return (HANDLE)(f->_file);
}
There are lots of reasons to be careful - if you're not in control of the FILE *, what's to stop them closing it without you knowing, etc.
Enjoy!
Iain.
|
|
|
|
|
Dear Iain,
Thank you. Your ideal is right.
But there is a mistake in your demo code. Perhaps, you lose a function call. I believe your original thought is following:
<br />
HANDLE FileToHandle (FILE *f)<br />
{<br />
return (HANDLE)(_get_osfhandle(f->_file));<br />
}<br />
I have tried the method and verified it is right.
Enjoy!
June
|
|
|
|
|
Dear Iain,
Now I encounter another problem that is how to get file`s name by HANDLE of a file. Do you know it???
If you know it, please help me.
Thank you.
Enjoy!
June.
|
|
|
|
|
Thanks for using the code I *should* have written to get the HANDLE.
I suspect the filename will be stored in FILE * in a similar fashion.
If you do the same thing I did, and step into the fopen code, you can find out the information too.
Good luck,
Iain.
Iain Clarke appearing by Special Request of CPallini.
|
|
|
|
|
Thank your help.
Best wish for you.
June
|
|
|
|
|
i have a dialog which contains an editbox (dialog1)and whatever data i enter into the editbox is passed to another editbox.(dialog2). now if i enter any value in dialog2 the value should stay in the dialog2 editbox. but that is not happening. the value of dialog1 editbox is appearing. so how do i save this value which i entered in dialog2 editbox?
do i need to do some checks for this?
|
|
|
|
|
What do you mean with save,when you want to start program again you need to these values?
|
|
|
|
|
true. but that is when u restart the application. save means what ever values that is put in the editbox(dialog2) should appear than the original value(dialog 1)
|
|
|
|
|
Ok for this problem you can use of Registry for save values or use of ini files.
|
|
|
|
|
is there any other way out to store this value in a variable and retrieve this?
|
|
|
|
|
No you want to close your program and open it again,you need to hard for save values not memory.
|
|
|
|
|
Hamid. wrote: No you want to close your program and open it again
Well, but that's not what he wants to do (as far as I understood).
|
|
|
|