problem encountered by calling constructors explicitly.
-
here in the below program i have tried to pass the values of 1d matrix to the object of class Vector by using constructor explicit call.
#include
using namespace std;const int size = 3;
class Vector {
int *v;public:
Vector(){
v = new int[size];
for(int i=0; iv[i] * y.v[i] ;
return sum;
}void display(){ for(int i=0; i
Quote:
cout << "v1 = "; v1.display(); cout << "v2 = "; v2.display();
the above portion of code doesn't works as expected it gives,
Output:
Quote:
6 3 9
6 3 9
v1 = 6 3 9
v2 = 6 3 9
v1 x v2 = 126Expected:
Quote:
Quote:
1 2 3
6 3 9
v1 = 1 2 3
v2 = 6 3 9
v1 x v2 = 39Thank you
-
here in the below program i have tried to pass the values of 1d matrix to the object of class Vector by using constructor explicit call.
#include
using namespace std;const int size = 3;
class Vector {
int *v;public:
Vector(){
v = new int[size];
for(int i=0; iv[i] * y.v[i] ;
return sum;
}void display(){ for(int i=0; i
Quote:
cout << "v1 = "; v1.display(); cout << "v2 = "; v2.display();
the above portion of code doesn't works as expected it gives,
Output:
Quote:
6 3 9
6 3 9
v1 = 6 3 9
v2 = 6 3 9
v1 x v2 = 126Expected:
Quote:
Quote:
1 2 3
6 3 9
v1 = 1 2 3
v2 = 6 3 9
v1 x v2 = 39Thank you
The program shouldn't run as is. In your second constructor, you aren't allocating the memory for the vector. (You also aren't deleting that memory in a destructor.) Also note that you construct the v object in the default constructor. Since there is no assignment operator AND the second constructor is not marked explicit, it is creating a second instance of Vector and then member-wise copying that to the first, clobbering the allocation of v in the first instance. (Why your compiler is letting you get away with this is a mystery to me.) So, add a destructor and then put "explicit" in front of the second constructor. Then, remove the assignments and replace the declaration line with: v1(x), v2(y). (It's still messy code, but in a debugger, you can now see how you are explicitly calling the second constructor and never the first.)
-
The program shouldn't run as is. In your second constructor, you aren't allocating the memory for the vector. (You also aren't deleting that memory in a destructor.) Also note that you construct the v object in the default constructor. Since there is no assignment operator AND the second constructor is not marked explicit, it is creating a second instance of Vector and then member-wise copying that to the first, clobbering the allocation of v in the first instance. (Why your compiler is letting you get away with this is a mystery to me.) So, add a destructor and then put "explicit" in front of the second constructor. Then, remove the assignments and replace the declaration line with: v1(x), v2(y). (It's still messy code, but in a debugger, you can now see how you are explicitly calling the second constructor and never the first.)
-
here in the below program i have tried to pass the values of 1d matrix to the object of class Vector by using constructor explicit call.
#include
using namespace std;const int size = 3;
class Vector {
int *v;public:
Vector(){
v = new int[size];
for(int i=0; iv[i] * y.v[i] ;
return sum;
}void display(){ for(int i=0; i
Quote:
cout << "v1 = "; v1.display(); cout << "v2 = "; v2.display();
the above portion of code doesn't works as expected it gives,
Output:
Quote:
6 3 9
6 3 9
v1 = 6 3 9
v2 = 6 3 9
v1 x v2 = 126Expected:
Quote:
Quote:
1 2 3
6 3 9
v1 = 1 2 3
v2 = 6 3 9
v1 x v2 = 39Thank you
Since you know at compile time the array size, why don't you allocate it on the stack?
#include #include using namespace std;
constexpr size_t SIZE = 3;
class Vector
{
array x{};public:
Vector (const array & a)
{
for (size_t n=0; n x{1,2,3};
array y{6,3,9};Vector v1{x};
Vector v2{y};cout << "v1 " << v1 << endl;
cout << "v2 " << v2 << endl;
cout << "v1*v2 = " << (v1*v2) << endl;
}