reconstruction
i have recently purchased a book updated book on c++
the book stated that this.
Point3d test;
test = Point3d(1,1,1); //this is an assisment
i thought that was a reconstruction.
reboot the dam thing
It is clearly an assignment; you are assigning a new value to the variable test by means of (its) Point3d.operator=(). I am curious, however, as I have never before heard of the term ''reconstruction'': What do you mean by that?
quote: Original post by Miserable
It is clearly an assignment; you are assigning a new value to the variable test by means of (its) Point3d.operator=(). I am curious, however, as I have never before heard of the term ''reconstruction'': What do you mean by that?
i assumed that once the default constructor was used to construct test doing this ( test = Point3d(1,1,1) ) would be tring to reconstruct the object test.
i say this because futher in the book on effecient programing
the auther stated that dealing with large class objects such as
a matrix and given an example of this class with the defined overloaded operators for addition subtraction etc and optimizations made to c++ that this
matrix MatrixAdd(paramList)
{
matrix result;
//do work
return result
}
would be rewritten to
void matrixAdd(matrix &result,paramList)
{
//directly compute into result
{
/////please remember this is just for example
and so doing this
Matrix test2; //default construct
test2 = test3 + test4;
would invoke your defined operator and the compiler would rewrite
matrix operator+(paramList);
to this
void operator+(test2,paramList);
because the first thing done to the object (test2) is to applied the constructor that has been applied to the local object.The author stated that test2 should not be allready
constructed and so this would construct the object test2 a second
time.
if that is so then the orginal post or this
matrix test2;
test2 = matrix(paramList);
sould also construct the object again while this
martix test2 = matrix(paramList); //is OK
reboot the dam thing
quote: Original post by sage2003
i assumed that once the default constructor was used to construct test doing this ( test = Point3d(1,1,1) ) would be tring to reconstruct the object test.
To the best of my knowledge, you cannot call a constructor on an already constructed object.
quote:
i say this because futher in the book on effecient programing
the auther stated that dealing with large class objects such as
a matrix and given an example of this class with the defined overloaded operators for addition subtraction etc and optimizations made to c++ that this
matrix MatrixAdd(paramList)
{
matrix result;
//do work
return result
}
would be rewritten to
void matrixAdd(matrix &result,paramList)
{
//directly compute into result
{
/////please remember this is just for example
I think I can see where you are coming from - it would be more efficient to pass a mutable object by reference than to return a large object by value. The only problem I see is that the object will have to be fully constructed beforehand, which is less efficient than what might be done in the function. As for returning by value - well, passing any large objects by value is always a slow operation. However, I suspect that this sort of thing can, in many cases, easily be optimised away by a halfway decent compiler (I''ve never studied such things, but it ought to be easy, or at least feasible, to just let the function allocate its object in the same space where the caller allocates its object).
quote:
and so doing this
Matrix test2; //default construct
test2 = test3 + test4;
would invoke your defined operator and the compiler would rewrite
matrix operator+(paramList);
to this
void operator+(test2,paramList);
because the first thing done to the object (test2) is to applied the constructor that has been applied to the local object.The author stated that test2 should not be allready
constructed and so this would construct the object test2 a second
time.
You lost me there. What does matrixAdd() have to do with which operator+() will be called? Or did I misunderstand what you are trying to say?
quote:
if that is so then the orginal post or this
matrix test2;
test2 = matrix(paramList);
sould also construct the object again while this
martix test2 = matrix(paramList); //is OK
The first one should call operator(), while the second one is perfectly equivalent to matrix test2(paramList);
quote: Original post by sage2003
i assumed that once the default constructor was used to construct test doing this ( test = Point3d(1,1,1) ) would be tring to reconstruct the object test.
There''s no such thing as reconstruction. It is assigning another Point3d object to test.
quote: Original post by MiserableOriginal post by sage2003
i assumed that once the default constructor was used to construct test doing this ( test = Point3d(1,1,1) ) would be tring to reconstruct the object test.
To the best of my knowledge, you cannot call a constructor on an already constructed object.
quote:
i say this because futher in the book on effecient programing
the auther stated that dealing with large class objects such as
a matrix and given an example of this class with the defined overloaded operators for addition subtraction etc and optimizations made to c++ that this
matrix MatrixAdd(paramList)
{
matrix result;
//do work
return result
}
would be rewritten to
void matrixAdd(matrix &result,paramList)
{
//directly compute into result
{
/////please remember this is just for example
I think I can see where you are coming from - it would be more efficient to pass a mutable object by reference than to return a large object by value. The only problem I see is that the object will have to be fully constructed beforehand, which is less efficient than what might be done in the function. As for returning by value - well, passing any large objects by value is always a slow operation. However, I suspect that this sort of thing can, in many cases, easily be optimised away by a halfway decent compiler (I''ve never studied such things, but it ought to be easy, or at least feasible, to just let the function allocate its object in the same space where the caller allocates its object).
quote:
and so doing this
Matrix test2; //default construct
test2 = test3 + test4;
would invoke your defined operator and the compiler would rewrite
matrix operator+(paramList);
to this
void operator+(test2,paramList);
because the first thing done to the object (test2) is to applied the constructor that has been applied to the local object.The author stated that test2 should not be allready
constructed and so this would construct the object test2 a second
time.
You lost me there. What does matrixAdd() have to do with which operator+() will be called? Or did I misunderstand what you are trying to say?
quote:
if that is so then the orginal post or this
matrix test2;
test2 = matrix(paramList);
sould also construct the object again while this
martix test2 = matrix(paramList); //is OK
The first one should call operator(), while the second one is perfectly equivalent to matrix test2(paramList);
////////////////////////////////////////////////////////////
My mistake i clearly wanted to say that the matrix example is an optimization made by the complier when it see
a return type of class type in a function as so i was told.
My whole point is that i was at a stump as with what the auther stated in his book when he said that this is disastrous when the compiler tries to optimize this function in the same way.
which is from the matrixAdd example and so that from this
Pointe3d test; (default construct)
test = operator+(test2 , test3)
to
operator+(test,test2,test3)
because test is allready constructed this optimization expected it to be raw storage and will try to construct it again as i was told. my thought was that it would just overwrite the data like an assisment.
/////////////////////////////////////
reconstrustion was a poor choice of words
i wanted to say trying to construct and allready constructed object
reboot the dam thing
int five()
{
return 5;
}
int OneToFive = 1;
OneToFive = five(); //returns 5, so OneToFive = 5;
class cClass
{
public:
cClass(); // equals cClass (return) cClass();
~cClass();
};
.lick
[edited by - Pipo DeClown on February 25, 2003 2:17:44 PM]
[edited by - Pipo DeClown on February 25, 2003 2:17:57 PM]
{
return 5;
}
int OneToFive = 1;
OneToFive = five(); //returns 5, so OneToFive = 5;
class cClass
{
public:
cClass(); // equals cClass (return) cClass();
~cClass();
};
.lick
[edited by - Pipo DeClown on February 25, 2003 2:17:44 PM]
[edited by - Pipo DeClown on February 25, 2003 2:17:57 PM]
quote: Original post by Miserable
To the best of my knowledge, you cannot call a constructor on an already constructed object.
You can, but the syntax is an abomination, and it should never be performed implicitly by the compiler.
#include <iostream>class Temp { public: Temp(int i) : _i(i) { std::cout << "Temp()" << std::endl; } ~Temp() { std::cout << "~Temp()" << std::endl; } operator int() { return _i; } private: int _i;};int main() { { Temp t(1); std::cout << t << std::endl; new (&t) Temp(5); std::cout << t << std::endl; } return 0;}
Should output:
Temp()
1
Temp()
5
~Temp()
sage2003: it sounds like you are thinking about return value optimization (RVO) which involves the elmination of temporaries. A standards compliant compiler will not have the kind of behavior you seem to be describing for arbitrary classes. That is to say, it should always try to call operator=, making it an assignment.
It could be that, for a specific assignment that an additional temporary will get optimized out, making it appear that a "reconstruction" was performed; however this is an optimization done at the intermediate representation level and is no longer subject to C++ definitions (Much in the same way that it''s meaningless to discuss instruction reordering at the C++ source level).
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement