#include <iostream>
class Army{
int units;
double power;
public:
int get_units(){
return units;
}
void createarmy(int troops,double pow) {
units = troops;
power = pow;
}
}
void display(Army* armies,int size)
// How can i make this function to get all the values at
// armies[0] and at armies[1]?
}
int main(){
Army armies[2]{};
armies[0].createarmy(100,500);
armies[1].createarmy(180,400);
display(armies,2);
}
-
The `Army` class can make the `display` function a `friend`. – Eljay Jun 03 '22 at 17:15
-
1@Eljay why? There's already a public function `get_units()` which can be used?? – πάντα ῥεῖ Jun 03 '22 at 17:17
-
1`Void`, `Class` -- Please post real code, not fake code. – PaulMcKenzie Jun 03 '22 at 17:17
-
1And look at `std::array` and `std::vector` and range-for. – Goswin von Brederlow Jun 03 '22 at 17:17
-
2The terrible intendation here hides the fact that `Void display(Army* armies,int size)` is declared inside the class body. Btw: there are multiple issues here such as keywords `class` and `void` being upper case and semicolons missing. Also the function name `createarmy` gives a wrong impression: The `Army` object is created by the constructor. The function is just setting some menber variables... – fabian Jun 03 '22 at 17:18
-
1@OP -- You may have got your question closed, probably as a side-effect of posting fake code, plus the horrible indentation. I don't blame anyone who misread the code you posted. – PaulMcKenzie Jun 03 '22 at 17:20
-
Aside: Instead of a pointer to array and size, use a `std::vector`. – aschepler Jun 03 '22 at 17:21
-
_@sukhvir_ Your code doesn't compile because of simple typos in 1st place. Fix that 1st, and then ask about what your real problem is! – πάντα ῥεῖ Jun 03 '22 at 17:26
-
@PaulMcKenzie well, I reopened it, because the duplicate didn't apply here. Just plain close vote about _not reproducible because of typos_. – πάντα ῥεῖ Jun 03 '22 at 17:29
-
@fabian: So, putting `display()` inside the class really made no sense, as OP was calling it as a stand-alone function. Corrected and answered. – einpoklum Jun 03 '22 at 17:57
-
@GoswinvonBrederlow: While those are certainly useful classes, I don't quite see how they're relevant to this question. – einpoklum Jun 03 '22 at 17:57
-
@aschepler: It's a bad idea to force the construction of an `std::vector` in order to take a sequence of arrays. What if they're in another contiguous container? What if they're on the stack? etc. No, the flexible thing to do is use a span; see my answer below. – einpoklum Jun 03 '22 at 17:59
-
@einpoklum to iterate over the armies – Goswin von Brederlow Jun 03 '22 at 18:10
-
@GoswinvonBrederlow: Then see my answer (and my reply to aschepler). – einpoklum Jun 03 '22 at 19:00
-
@einpoklum I meant more for creating the data in the first place. Yes, a span is a better function interface. (Or possibly a template taking any range, depending.) – aschepler Jun 04 '22 at 11:35
1 Answers
How can I make this function to get all the values at armies[0] and at armies[1] using a pointer
While there are ways to access private members of a class from outside that class - let us instead give more thought to whether you should really be facing this problem.
You, or whoever designed the Army class, decided that an army's units member is (read-only) accessible, with the get_units() method; but that its power is not accessible, i.e. it is an implementation detail which should not be relevant from the outside.
Does this make sense? Your question suggests that the answer is negative: You want to "display an army's characteristics", including its power. That means the power is not an implementation detail, and is relevant to the outside.
Assuming I'm right, what you probably want to do is simply add a similar getter method for power as the one you have for units:
class Army {
// etc. etc.
double get_power() { return power; }
// etc. etc.
};
Additional notes:
Both these getter methods can be marked as
const- since they don't alter the state of theArmyobject. Thus,double get_units() const { return units; } double get_power() const { return power; }Instead of repeating the type of your members whenever you work with them, you might want to define an
Army::size_typeand aArmy::power_type; or these might be type definitions outside your class.Let's hope these armies stay in games and we are spared their power and units in real life.
The
display()function should not be able to modify armies, so better make itvoid display(const Army* armies, int size).display()is not a great choice for a function name; it's a bit ambiguous; I'd choose something likevoid list_statistics(const Army* armies, int size).Don't pass a sequence of elements in memory the old C-style way: A pointer and a size. Instead, pass them in an
std::span:void list_statistics(std::span<const Army> armies) { for(const Army& army : armies) { // code for "displaying" a single army } }
- 102,731
- 48
- 279
- 553
-
At that point I would write `ranges::for_each(armies, list_statistic)`, where the last is a function for displaying one army. – Goswin von Brederlow Jun 03 '22 at 21:24
-
@GoswinvonBrederlow: Fair enough, but: 1. For one army it's still multiple statistics... 2. I don't want to burden the example with another function declaration, or a lambda. – einpoklum Jun 03 '22 at 21:57