Is there any way that i could implement a constructor that accepts braces instead of brackets?
ex
class MyString
{
char *x;
int n;
};
int main()
{
MyString instance[10];
return 0;
}
btw i understand the idea of parentheses and we cant declare a method like so. but i was just wondering if there is a way to do that?
What you are doing when you define an array is that you create multiple instance of the class.
For example your definition
MyString instance[10];
defines ten distinct MyString
objects. I.e. the constructor will be called ten times, once for each object.
If your purpose is to create a single MyString
object, and pass the value 10
to it, then you have to use either parentheses or curly-braces. And of course implement an appropriate constructor taking the value as argument.
And no there's nothing you can do to change it, it's part of the base language syntax.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments