Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. problem encountered by calling constructors explicitly.

problem encountered by calling constructors explicitly.

Scheduled Pinned Locked Moved C / C++ / MFC
graphicshelp
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tarun Jha
    wrote on last edited by
    #1

    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 = 126

    Expected:

    Quote:

    Quote:

    1 2 3
    6 3 9
    v1 = 1 2 3
    v2 = 6 3 9
    v1 x v2 = 39

    Thank you

    J C 2 Replies Last reply
    0
    • T Tarun Jha

      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 = 126

      Expected:

      Quote:

      Quote:

      1 2 3
      6 3 9
      v1 = 1 2 3
      v2 = 6 3 9
      v1 x v2 = 39

      Thank you

      J Offline
      J Offline
      Joe Woodbury
      wrote on last edited by
      #2

      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.)

      T 1 Reply Last reply
      0
      • J Joe Woodbury

        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.)

        T Offline
        T Offline
        Tarun Jha
        wrote on last edited by
        #3

        thank you

        1 Reply Last reply
        0
        • T Tarun Jha

          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 = 126

          Expected:

          Quote:

          Quote:

          1 2 3
          6 3 9
          v1 = 1 2 3
          v2 = 6 3 9
          v1 x v2 = 39

          Thank you

          C Offline
          C Offline
          CPallini
          wrote on last edited by
          #4

          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;
          }

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups