can anybody help me out
-
is there any technical difference between invoking a constructor using class obj(5,20) and class obj=class(10,40). Thanks in advance
-
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
-
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
No, really, it's true...
Cédric Moonen Software developer
Charting control [v1.2] -
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
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 using namespace std;
class MyClass
{
public:
int _a, _b;
MyClass(){}
MyClass(int a, int b):_a(a), _b(b){}
// copy constructor and assignment operator arbitrarly messed up.
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;
}
and to my surprise, the output:
my1: 3 2
my2: 3 2
my3: 0 0
my4: 2 3Well 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] -
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 using namespace std;
class MyClass
{
public:
int _a, _b;
MyClass(){}
MyClass(int a, int b):_a(a), _b(b){}
// copy constructor and assignment operator arbitrarly messed up.
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;
}
and to my surprise, the output:
my1: 3 2
my2: 3 2
my3: 0 0
my4: 2 3Well 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.
Cédric Moonen Software developer
Charting control [v1.2] -
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 using namespace std;
class MyClass
{
public:
int _a, _b;
MyClass(){}
MyClass(int a, int b):_a(a), _b(b){}
// copy constructor and assignment operator arbitrarly messed up.
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;
}
and to my surprise, the output:
my1: 3 2
my2: 3 2
my3: 0 0
my4: 2 3Well 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]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);
}
// copy constructor and assignment operator arbitrarly messed up.
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.
-
No, really, it's true...
Cédric Moonen Software developer
Charting control [v1.2]Cedric Moonen wrote:
I don't need any assistance
Sure? :laugh:
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.
Cédric Moonen Software developer
Charting control [v1.2]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] -
No, really, it's true...
Cédric Moonen Software developer
Charting control [v1.2]I knew *someone* would bite - but I assumed it would be Signor Pallini. And he's the one who gave a sensible reply! Iain.
-
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);
}
// copy constructor and assignment operator arbitrarly messed up.
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! :laugh:
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. :-D
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.
Well, it's friday :rolleyes: That explains a lot :-D
Cédric Moonen Software developer
Charting control [v1.2]