constructors, arrays, and new

This is a discussion on constructors, arrays, and new within the C++ Programming forums, part of the General Programming Boards category; I have a class with a constructor that needs to be called so it may initalize it's variables (some which ...

  1. #1
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681

    constructors, arrays, and new

    I have a class with a constructor that needs to be called so it may initalize it's variables (some which are arrays).

    if I do:
    Code:
    class_name variable_name[array_size];
    it doesn't call the constructors but if I do:
    Code:
    class_name *variable_name = new class_name[array_size];
    it calls the constructors and the world (or at least program) is happy.

    Is this suppose to be happening? And is there any way to get the constructors called using the array notation?

    Thanks

  2. #2
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    I built a quick test program to see if a constructor would be called on my system, and it is called in both circumstances. I was only able to figure that out by putting a break point in the constructor though; if set a break at this line:
    Code:
    class_name   variable_name[array_size];
    It would go right to the next line when I tried to trace into it.

    As far as which should be happening, I wouldn't be able to tell you.

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Ok this is even wierder. I'm finally getting an error message

    cc1plus: warnings being treated as errors
    bubbletest.cpp: In function `int main()':
    bubbletest.cpp:91: warning: deleting array `class Zone zones[5]'
    BTW compiler:
    g++ -v = gcc version 2.95.4 20011002 (Debian prerelease)

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    I think it's about time to see the source in its entirety.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Entire source is a little big. Here are the class and first part of main (where it is declared)
    Code:
    #include <iostream>
    #include <iomanip>
    
    enum dirs { E = 0, NE, N, NW, W, SW, S, SE };
    
    using namespace std;
    
    class Zone
    {
      int connections[8];
    
      public:
        Zone(){
          for (int i=0; i < 8; i++)
            connections[i] = -1;
        }
        void addconn(int p, int z)
        {
          if ( connections[p] == -1 )
          {
            connections[p] = z;
          }
        }
    
        int getconn(int p)
        {
          return connections[p];
        }
        void setnum(int x)
        {
          num = x;
        }
        int getdir (void)
        {
          for (int i=0; i < 8; i++)
            if ( connections[i] == -1 )
              return i;
    
          return -1;
        }
    };
    Code:
    int main(void)
    {
      const int size = 80;
      int base = 0;
      int next = 1;
      int dir;
      Zone *zones = new Zone[size];
    If I change
    Code:
    Zone *zones = new Zone[size];
    to
    Code:
    Zone zones[size];
    It produces the following
    bubbletest.cpp: In function `int main()':
    bubbletest.cpp:76: warning: deleting array `class Zone zones[80]'

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Ok made a test program to test it out in a smaller manner and its working as it should
    Code:
    #include <iostream>
    
    using namespace std;
    const int Size = 8;
    
    class Test {
      int arr[Size];
    
      public:
        Test(){
          for (int i=0; i < Size; i++)
            arr[i] = -1;
    
          cout<<"Test of constructor call"<<endl;
        }
        int get(int x)
        {
          return arr[x];
        }
    };
    
    const int Num = 10;
    
    int main(void)
    {
      Test t1[Num];
      for (int i=0; i < Size; i++)
      {
        for (int j=0; j < Size; j++)
          cout<<t1[i].get(j)<<' ';
        cout<<endl;
      }
      Test *t2 = new Test[Num];
      for (int i=0; i < Size; i++)
      {
        for (int j=0; j < Size; j++)
          cout<<t2[i].get(j)<<' ';
        cout<<endl;
      }
    
    
      delete[] t2;
    }

  7. #7
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Bah the error was because I forgot to remove the delete[] instruction

    Took that out and tried it again and it seems to be working.

    Weird, oh well I guess the planets were all aligned correctly to give me a weird result

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21