Constructors in c++ making an object with [] instead of brackets

Ari Muayad

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?

Some programmer dude

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

C# Constructors overloading useless brackets

From Dev

Creating constructors using "this" instead of simply returning an object

From Dev

C# Basic OOP - Making a Dictionary of a Class with Constructors

From Dev

Making threads in static constructors

From Dev

Object Constructors

From Dev

square brackets before object c#

From Dev

Making a diff of JSON objects with lists return object instead of array

From Java

How can Java assignment be made to point to an object instead of making a copy?

From Java

How to avoid making long constructors

From Dev

Objective C: Making Object in .h Files

From Dev

C++ Making a thread and passing Object by reference

From Dev

In C++ are constructors called before or after object creation?

From Dev

How to best handle C++ object initialization: empty constructors or pointers?

From Dev

How to set breakpoint simultaneously on all constructors in gdb for a C++ object?

From Dev

"select new constructor" query returns java.lang.Object instead of instance of constructors class

From Dev

Joshua Bloch Item #1 Static Factory Methods Instead of Constructors - Object creation

From Dev

passing an unnamed object as an argument when making a new object in C++

From Dev

Static factory methods instead of constructors

From Dev

Object with constructors in javascript

From Dev

forEach and object constructors

From Dev

Instantiating an object by chaining constructors

From Dev

Constructors as object keys

From Dev

Making the constructors/assignment operators of a typedef friend functions

From Dev

new operator with parentheses "(", ")" instead of brackets "[", "]"

From Dev

in C++, can template function taking class object instantiate object with it's constructors arguments?

From Dev

C# WriteLine method shows numbers inside brackets instead of what I want the to be replaced with

From Dev

Making two objects in C more object-oriented

From Dev

c# making object in different ways (abstraction used)

From Dev

Delegating constructors in c++ () or {}

Related Related

  1. 1

    C# Constructors overloading useless brackets

  2. 2

    Creating constructors using "this" instead of simply returning an object

  3. 3

    C# Basic OOP - Making a Dictionary of a Class with Constructors

  4. 4

    Making threads in static constructors

  5. 5

    Object Constructors

  6. 6

    square brackets before object c#

  7. 7

    Making a diff of JSON objects with lists return object instead of array

  8. 8

    How can Java assignment be made to point to an object instead of making a copy?

  9. 9

    How to avoid making long constructors

  10. 10

    Objective C: Making Object in .h Files

  11. 11

    C++ Making a thread and passing Object by reference

  12. 12

    In C++ are constructors called before or after object creation?

  13. 13

    How to best handle C++ object initialization: empty constructors or pointers?

  14. 14

    How to set breakpoint simultaneously on all constructors in gdb for a C++ object?

  15. 15

    "select new constructor" query returns java.lang.Object instead of instance of constructors class

  16. 16

    Joshua Bloch Item #1 Static Factory Methods Instead of Constructors - Object creation

  17. 17

    passing an unnamed object as an argument when making a new object in C++

  18. 18

    Static factory methods instead of constructors

  19. 19

    Object with constructors in javascript

  20. 20

    forEach and object constructors

  21. 21

    Instantiating an object by chaining constructors

  22. 22

    Constructors as object keys

  23. 23

    Making the constructors/assignment operators of a typedef friend functions

  24. 24

    new operator with parentheses "(", ")" instead of brackets "[", "]"

  25. 25

    in C++, can template function taking class object instantiate object with it's constructors arguments?

  26. 26

    C# WriteLine method shows numbers inside brackets instead of what I want the to be replaced with

  27. 27

    Making two objects in C more object-oriented

  28. 28

    c# making object in different ways (abstraction used)

  29. 29

    Delegating constructors in c++ () or {}

HotTag

Archive