Thread: C++ Arrays
View Single Post
Old 11-09-2006, 11:32 AM   #1 (permalink)
milt
Forum Expert
 
milt's Avatar
 
Join Date: Nov 2003
Location: Lancaster, PA
Age: 20
Posts: 1,606
Send a message via AIM to milt Send a message via Yahoo to milt Send a message via Skype™ to milt
Default C++ Arrays

Okay, so I know that in C#, I can create a class with a custom constructor, and then make an array of that class and call the constructors in the initialization. This may sound confusing, so I'll give an example.
Code:
public class Class1
{
     public string string1;

     public Class1(string s)
     {
          string1 = s;
     }
}

public class Class2
{
     public Class2()
     {
          Class1[] classes = new Class1[]
          {
               new Class1("Hello"), //Calling custom constructors in initialization of array
               new Class1("Goodbye")
          };
     }
}
What I am trying to do, is something similar in C++.
Code:
class Class1
{
     public:
          string string1;
          Class1(string s)
          {
               string1 = s;
          }
}
class Class2
{
     public:
          Class1 *classes;
          Class2()
          {
               classes = new Class1[] //Syntax error
               {
                    Class1("Hello"),
                    Class2("Goodbye")
               };
          }
          ~Class2()
          {
               delete [] classes;
          }
}
}

That C++ code gives me syntax errors in the initialization of "classes". What would be the best way to do what I am trying to do? I can't really seem to find a solution, so any help is appreciated.

Thanks
__________________
--Milt, AKA Pokey
milt is offline   Reply With Quote