Thread: constructors in arrays

  1. #1
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,743

    Question constructors in arrays

    When you declare an instance of a class, why are you only allowed to use the default constructor of that class? For example:

    class A{
    public:
    A ( ); //default
    A ( int j );
    int myInt;
    }

    legal:

    A myArray[5];

    illegal:

    A myArray[5](10);
    My Website

    "Circular logic is good because it is."

  2. #2
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    you don't have to use the default constructor. You can make an assortment of constructors to do whatever you want depending on what is passed.

    ...
    A(int this)
    {
    my_private_member_data = this;
    }
    ....


    then

    A instantiation[5](2);

    is valid.
    Last edited by Betazep; 12-06-2001 at 11:37 PM.
    Blue

  3. #3
    Unregistered
    Guest
    A instantiation[5](2);

    is valid.
    no, it's not valid...

  4. #4
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    The problem with...

    >>> A instantiation[5](2);

    ... is ambiguity, which member of the array will have the value 2? The correct way to do this is...

    A instatiation[5] = {2,2,2,2,2};

    ... one value for each constructor, no ambiguity.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  2. Arrays and Constructors
    By Verdagon in forum C++ Programming
    Replies: 1
    Last Post: 07-20-2005, 07:20 PM
  3. Constructors for creating 2d arrays
    By Welshy in forum C++ Programming
    Replies: 5
    Last Post: 06-29-2005, 03:50 PM
  4. constructors, arrays, and new
    By Thantos in forum C++ Programming
    Replies: 6
    Last Post: 05-30-2004, 06:21 PM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM