CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2003
    Location
    Brazil
    Posts
    335

    Array initialization C14

    hi!

    Is there an way of initialize a array of classes with the same object without the need to repeat them?
    For exemple:

    Code:
    class myclass
    {
       public:
       int first,second third;
    
      myclass(int pfirst,int psecond,int pthird) : first(pfirst),second(psecond),third(pthird)
      {
      }
    };
    
    class myotherclass
    {
      public:
      myclass myclassArray[100];
    
      //--- As far as I know I´d have to initialize all 100 members (with the same values I want exemple (5,7,9) 
      //--- as:
    
      myotherclass(): myclassArray{{5,7,9},    //   0
                                   {5,7,9},    //   1
                                   {5,7,9},    //   2
                                    ...        //   3,4,5,... 95,96,97  
                                   {5,7,9},    //   98
                                   {5,7,9}}    //   99
    {
    }
    I want a direct construction type trought an initializer list, without using a for into the boby of myotherclass constructor. Is there an easiest way of doing that?

    Thank you

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Array initialization C14

    if myclassArray is a vector rather than an array, then you can do it like this

    Code:
    class myClass
    {
    public:
    	int first, second, third;
    
    	myClass(int pfirst, int psecond, int pthird) : first(pfirst), second(psecond), third(pthird) {}
    };
    
    class myotherclass
    {
    public:
    	vector<myClass> myClassArray;
    
    	myotherclass() {
    		myClassArray.assign(100, {5, 7, 9});
    	}
    };
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Feb 2003
    Location
    Brazil
    Posts
    335

    Re: Array initialization C14

    Ok 2kaud,

    Thank you!!! I will change it to vector.

  4. #4
    Join Date
    Feb 2017
    Posts
    677

    Re: Array initialization C14

    Quote Originally Posted by 2kaud View Post
    Code:
    	myotherclass() {
    		myClassArray.assign(100, {5, 7, 9});
    	}
    As an alternative it's possible to use a constructor of std::vector (that does the same as the assign method) like this,
    Code:
    	myotherclass() : myClassArray(std::move(std::vector<myClass>(100, {5, 7, 9}))) {
    	}
    Using the std::move function isn't strictly necessary but it should be more efficient (since it avoids the copying of the constructed vector to myClassArray that may otherwise take place).

    Yet another option is to not initialize myClassArray in the constructor but by direct assignment like this,

    Code:
    class myotherclass
    {
    public:
    	std::vector<myClass> myClassArray = std::vector<myClass>(100, {5, 7, 9});
    
    	myotherclass() = default;
    };
    Last edited by wolle; August 16th, 2018 at 01:36 AM.

  5. #5
    Join Date
    Feb 2003
    Location
    Brazil
    Posts
    335

    Re: Array initialization C14

    That was exactly what I was looking for!!!

    Thank you wolle!!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured