Hi guys,

How do I pass arguments into into the constructor if I'm using new to creat an array of objects?

So for example:

CParticle* parts;
parts = new CParticle[100];

parts is declared globally, but initialised later on in the code..
my lecturer suggested this strange syntax:

parts = new CParticle[100] = { CParticle(arg1), CParticle(arg2), ... };

He mentioned something about how this creates a temporary object and gets the constructor arguments from it.. but this was on a blackboard and when I tried it at home it didn't work.. I don't want to bother him again...

Any help is greatly appreciated!

Recommended Answers

All 7 Replies

Probably have to do it in a separate loop, something like this:

class CParticle
{
public:
    CParticle(int x = 0)
    {
        initialize(x);
    }
    void initialize(int x = 0)
    {
        m_x = x;
    }
private:
    int m_x;
};

int main(int argc, char* argv[])
{
    CParticle* parts;
    parts = new CParticle[100];
    for(int i = 0; i < 100; i++)
        parts[i].initialize(1);

	return 0;
}

Yeah, listen to Ancient Dragon. Sounds to me like you've got yet another bad instructor. The guy needs to go read his own textbook.

Follow AD's code, but if you want to have different initializers for each object, use an array:

int main( int argc, char *argv[] )
{
  int CParticle_initializers[ 100 ] = { arg1, arg2, arg3, ... }
  CParticle *parts = new CParticle[ 100 ];
  for (int i = 0; i < 100; i++)
    parts[ i ].initialize( CParticle_initializers[ i ] );

  ...
  return EXIT_SUCCESS;
}

Good luck.

thanks for the replies guys..
Yeah, I already have a Init() function of-course, but the point is I don't want to use an Init function.. the question I'm asking is "is it possible to do this in standard c++?"
I did STFW and unfortunately I cannot find any reference on the subject.. neither does my 1000+ page coursebook have any info.. :(

My lecturer is really good.. he founded the company that made the half-life engine and he is a millionaire..
I guess he might have misunderstood what I was asking, but I'm pretty sure he did say there was a way to do it..

Anyway thanks for the replies, really appreciate it..

As long as your array has to be a strict C-type array you must use a loop to initialize it, whether you are using C or C++. That guy's syntax is still wonky.

If you use a vector or a deque though, you can initialize it directly:

#include <iostream>
#include <deque>

using std::deque;
using std::cout;
using std::ostream;

class CParticle {
  public:
    CParticle( int x = 0 ): m_x( x ) { }
    friend ostream &operator << ( ostream &outs, CParticle &p );
  private:
    int m_x;
  };

ostream &operator << ( ostream &outs, CParticle &p ) {
  cout << p.m_x; 
  return outs;
  }

int main() {
  int inits[] = { 1, 2, 3, 4 };
  deque<CParticle> parts( inits, inits + 4 );  // array is 4 elements long

  for (int i = 0; i < 4; i++)
    cout << parts[ i ] << '\n';

  return EXIT_SUCCESS;
  }

For just 100 elements, a vector would do just fine, but if your list is going to be very large, use a deque.

Good luck.

[EDIT] If you get rid of that new keyword and make it an array and not a pointer then the original syntax should work fine. CParticle parts[ 100 ] = { CParticle( 1 ), CParticle( 2 ), ... };

Thanks once again for the reply..

Unfortunately I cannot get rid of new. The object is in-fact one of a random number of particles generated to simulate space. I don't know at compile time how many I will create..

I've considered using vectors/deques but I'm gonna stick to arrays + my init function. I'm just surprised that there isn't a standard way of doing it, and so I guess I will have to get back to my lecturer on this one..

I will try and send you a sample of a program I have with almost the same format.

I have a problem. Developing a C++ program(coding) Recording a Phone call. It should display the time(hrs:mins/Day/Month/Year)...then the Cost of the phone call. Help pliz. I'm abit new in this and need it for my project.

Er, the standard way to do it is to use a vector or a deque. These automatically size to fit and allow you to add or remove elements very easy from the back (and front if a deque), and middle without too much trouble. Both can be accessed with the [] array indexing operator, are fast, don't require you to manage their memory, and have only one "disadvantage" over an array (or a newed array): they can't be treated as a pointer. Additionally, deques allow you to work with arbitrarily large amounts of data (or numbers of particles) without any increase in time.

If you want to be throwing random numbers of particles around, I suggest you use a deque, not an array (whether new or not).

Good luck.

commented: very well said +20
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.