milt
Knight
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.
What I am trying to do, is something similar in C++.
}
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
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")
};
}
}
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