-
August 15th, 2018, 12:33 PM
#1
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
-
August 15th, 2018, 12:57 PM
#2
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)
-
August 15th, 2018, 03:32 PM
#3
Re: Array initialization C14
Ok 2kaud,
Thank you!!! I will change it to vector.
-
August 15th, 2018, 11:50 PM
#4
Re: Array initialization C14
Originally Posted by 2kaud
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.
-
September 14th, 2018, 11:31 AM
#5
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|