Thread: Calling custom constructor of element in array whose class has const members?

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    127

    Calling custom constructor of element in array whose class has const members?

    If I have an array of some class, and that class has const members, is there some way I can call a custom constructor on elements of the array?

    I can't seem to reinitialize an element in foos in the example below. A thread on stack overflow mentioned the copy constructor show allow it, but I get "no match for call to '(Foo) (Foo&)'" when I try it.

    Code:
    class Foo
    {
    public:
      Foo();
    
      Foo(int x, int y);
    
      Foo(const Foo &foo);
    
    private:
    
      const int m_x;
      const int m_y;
    };
    
    Foo::Foo() : m_x(0), 
                 m_y(0)
    {
    }
    
    Foo::Foo(int x, int y) : m_x(x), 
                             m_y(y)
    {
    }
    
    Foo::Foo(const Foo &foo) : m_x(foo.m_x),
                               m_y(foo.m_y)
    {
    }
    
    int main()
    {
      Foo foo;
      Foo foo2(2, 3);
      Foo foos[10];
    
      foos[0](foo2);
      foos[0](2, 3);
      foos[0] = Foo(2,3);
    
      return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Is there a reason these variables need to be const?

    Jim

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    127
    Quote Originally Posted by jimblumberg View Post
    Is there a reason these variables need to be const?

    Jim
    The only time they are intended to be set is during initializing the object. Technically this is being done when the array is created, but that doesn't do much good because everything is set to the same value. The point of the array, for me, is just to reserve space in memory. I want to (and really only can, since I'm not aware of any syntax that lets me define an array where each element uses a different constructor) initialize it later.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Have you thought of using vector instead of arrays?

    Jim

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Um, do this:

    Code:
    Foo foo;
    Foo foo2(foo);
    
    Foo foos[10] = {
      foo, foo2, 
    };
    It doesn't really have anything to do with constness, but if foos itself is const, then you must do code like the above. The const data members of a Foo object - as in a Foo array element - would be initialized by any of the constructors in the class. In the example code, the first two foos were premade, the rest will be the result of the default Foo constructor.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Const class members
    By Phenom in forum C++ Programming
    Replies: 3
    Last Post: 04-15-2011, 11:39 AM
  2. calling class function from constructor
    By l2u in forum C++ Programming
    Replies: 3
    Last Post: 12-17-2006, 04:01 PM
  3. Replies: 2
    Last Post: 12-16-2006, 06:53 PM
  4. Problems with static const class members
    By sigfriedmcwild in forum C++ Programming
    Replies: 5
    Last Post: 12-05-2004, 07:57 AM
  5. Calling constructor of the base class of a derived class..
    By CaptainPenguin in forum C++ Programming
    Replies: 5
    Last Post: 02-19-2003, 01:47 PM