1

I have a probably very simple question in C++. Let's say I have class defined as:

class PS { private:

int value;

int m;

public:

PS();
PS(int value, int m);
PS(int m);

};

Now I want to define an array with elements of this type. For example,

PS t[3];

However, I want all of the elements in this array to have m=2. How would I do that? I am assuming I have to use inheritance some how, right? So for example I don't want to do something like this:

>PS t[3]; 
>t[0].PS(2);
>t[1].PS(2);
>t[2].PS(2);

I want to do it one show for all elements of t.

4
  • 3
    There is no such thing as "blank" in C++. If you refrain from initializing a variable then its value is undefined (AKA "junk"). Commented Jul 25, 2014 at 17:52
  • Decide what you have to do, not what you don't want/like. Take time and frame questions properly, seeing answers and editing won't help you much ! Commented Jul 25, 2014 at 18:06
  • There is a difference between data structure e.g an array and an array of Objects e.g array of PS Object. Lets say you have a member variable "M". You create a constructor that initializes your member variable "M" like this: PS(){m = 2;}. Now every time you create an instance of PS Object like: PS *t = new PS(); then call PS->m, you will get that m = 2, everything is done in the constructor. Now you can add this pointer of PS Object on your array whose length is 3 or more by using a for loop. This problem has nothing to do with inheritance, inheritance is multiple classes with same attributes. Commented Jul 25, 2014 at 18:32
  • I think that the thing is about declaration like PS t[3](2,0); but I know that it's not valid (test.cpp:7:25: error: bad array initializer pair<int,int> T[3](0,0);). Commented Jul 25, 2014 at 19:45

3 Answers 3

1

Using your constructor, you can simply use brace initialization :

PS t[] = { PS(2) , PS(2), PS(2) };

Or as suggested by @0x499602D2, since PS has a non explicit constructor, simply :

PS t[] = { 2, 2, 2 };

I would also suggest you to use std::array<> instead of C-style arrays.

Sign up to request clarification or add additional context in comments.

1 Comment

Since PS has a non-explicit constructor you can also do PS t[] = { 2, 2, 2 };.
1

It is not really safe not to initialize a value but you can use the C++11 feature that allows you to initialize variable directly in your class definition :

class PS { 
private: 
  int value;
  int m = 2;

public:
  PS() {};
};

If you are using an older version of C++ you can consider overloading the default constructor

class PS { 
private: 
  int value;
  int m;

public:
  PS(int _m = 2) : m(_m) {};
};

Comments

1

The STL Vector class is preferred to c-arrays. Using one, you could do:

std::vector<PS> t(3, PS(2));

4 Comments

You should almost always use initialisation lists. The first code example is very bad coding style, even though it probably won't make a difference for int members.
@ChristianHackl yipes, and that was worth a -1? I'm trying to show the OP that this was a possibility, and I even talk about how field initialization lists are preferred. I'll edit the answer to further reflect the bad practice of going without them.
Reverted the -1 because you mentioned the bad practice in the answer. Yes, I think it was worth it initially, because IMO you presented the bad practice as the default way of doing things. Especially seeing as how this is one of the typical mistakes made by C++ beginners coming from Java.
@ChristianHackl Fair point, I'll make sure to point that out in future answers, thank you for giving chastisement where it was due.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.