0

I'm trying to convert a program to OOP. The program works with a few arrays:

int tipoBilletes[9] = { 500,300,200,100,50,20,10,1,2 };
int cantBilletes[9] = {0};

So for my conversion, I declared in the header file this:

int *tipoBilletes;
int *cantBilletes;

and in the constructor I wrote

tipoBilletes = new int[9];
cantBilletes = new int[9];

tipoBilletes[0] = 500;
tipoBilletes[1] = 300;
tipoBilletes[2] = 200;
...

It works fine.

My question is, is there any way to initialize it like in Java?

int[] tipoBilletes = new int[]{ 500,300 };

rather than having to set each element one by one?

5
  • 2
    Not until the new version of C++ comes out. But you should be using std::vector, not new[]. Also, what benefits are you gaining by changing it from a fixed-size array to a dynamic array? Commented Jan 20, 2011 at 23:12
  • I still can't comprehend how it's not possible to have something as simple as a plain local array of objects without a default constructor in old C++... Have they, like, forgotten about it when making C++03, or what? Commented Jan 20, 2011 at 23:17
  • well i used dynamic array , cuz i thought that i could do the initialization java - like Commented Jan 23, 2011 at 15:04
  • @Kos: What makes you think it's not possible? Commented May 3, 2017 at 12:26
  • @BoundaryImposition sorry, I can't really remember what I have meant, it's been a while. Commented May 3, 2017 at 12:27

3 Answers 3

4

No, but you don't necessarily nead to write out each assignment independently. Another option would be:

const int TIPO_BILLETES_COUNT = 9;
const int initialData[TIPO_BILLETES_COUNT] = { 500,200,300,100,50,20,10,1,2 };
std::copy(initialData, initialData + TIPO_BILLETES_COUNT, tipoBilletes);

(Note that you should almost certainly be using a std::vector for this instead of manual dynamic allocation. The initialization is no different with a std::vector though once you've resized it.)

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

2 Comments

Actually, for the std::vector option, you could initialize the vector using its constructor that takes a pair of iterators instead of std::copy.
@Fred: Yep, you could do that too!
2

If you use std::vector you can use boost::assign:

#include <vector>
#include <boost/assign/std/vector.hpp>  
//... 
using namespace boost::assign;
std::vector<int> tipoBilletes;
tipoBilletes += 500, 300, 200, 100, 50, 20, 10, 1, 2;

On the other hand, you should consider using the fixed-size array if it is small and constant size.

Comments

1

My question is, is there any way to initialize it like in Java?

Yes, starting from C++11 there is a way to do this as shown below. Note that this was proposed in p1009r2.

int *tipoBilletes = new int[] { 500,300,200,100,50,20,10,1,2 };

From new expression documentation the above creates an array of type int[9].

Working demo


Note that you could just use std::vector that will do the memory management for you so that you don't have to worry about memory leaks. But the question was not about how to use std::vector.

Comments

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.