best way to pass argument to constructors of array of objects?

Basically I'm making like a gazillion objects of this particular class and so im making the objects in a 1 dimensional array. The objects need access to the memory address of some data from another class. I was able to pass that memory address to the constructor of a single object like so (and then store in a variable in that object)

 
hypothesis hypObj(strObj.pData[0]);


and when i want to make an array of say 10,000 hypObjects like so

 
hypothesis hypObj[10000];


that works fine also. but what is the best way to do both at the same time?

by the way, if it is of any consequence, both those lines are in main.cpp btw and both the classes are fairly strait forward and just included in main.cpp


thanks for any help!

p.s. as a side note do you guys think beginner is still the right place for a question like this? i dont feel like im anything other than a beginner but most of the beginner questions seem more beginnerish than this one.
Last edited on
It depends on how the class objects are implementing the data, but in a general reply I would be inclined to send the data in via a pointer. Copying large amounts of data into methods or functions can be a costly procedure. especially with 10000 objects. If you give a snippet of the class objects and a description of what you intend to accomplish I may be able to suggest a more accurate answer
it might look something like this:

1
2
3
4
5
strobjtype(whatever that is )* pstrObj = &strObj;

hypothesis hypObj(pstrObj->pData[0]);

Last edited on
sure i can do that np.

it is a pointer that im passing btw because i thought through that same problem myself.

just a little back story. I'm creating the the most basic framework for illustrating the basic principal behind evolutionary/learning algorithms. It will make hypotheses, test them, apply a fitness, cull the low fitness classes and replace them with new random hypotheses. no breeding and mutation yet, just random guesses, fitness and culling, ill work into the harder stuff later.

strObj.pData is the stream of data that it will be trying to predict. strObj.pData[99] is the most recent, and 98, 97, 96 ect show the past. stream.update() will move the data forward one iteration.

ok so here is main.cpp

1
2
3
4
5
6
7
8
9
10
11
#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include "stream.h"
#include "hypothesis.h"

int _tmain(int argc, _TCHAR* argv[])
{
	stream strObj;
	hypothesis hypObj(strObj.pData[0]);
}


stream.h

1
2
3
4
5
6
7
8
9
10
11
12
#pragma once
class stream
{
public:
	stream();
	void update();
	bool* pData[100];
	~stream();
private:
	int counter;
	bool data[100][10];
};


stream.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include "stream.h"

stream::stream()
{
	counter = 0;

	for (int i1 = 0; i1 < 100; ++i1) {
		for (int i2 = 0; i2 < 10; ++i2) {
			if (i1 % 10 == i2) {
				data[i1][i2] = 1;
			}
			else {
				data[i1][i2] = 0;
			}
		}
	}

	update();
}

void stream::update()
{
	++counter;

	for (int i = 0; i < 100; ++i) {
		pData[i] = data[(i + counter) % 100];
	}
}

stream::~stream(){}

*just noticed i need to reset this counter before it goes out of int bounds*

hypothesis .h

1
2
3
4
5
6
7
8
9
#pragma once

class hypothesis
{
public:
	hypothesis(bool*);
	bool* element[2];
	~hypothesis();
};


and finally hypothesis.cpp

1
2
3
4
5
6
7
8
9
10
#include "hypothesis.h"
#include <stdlib.h>

hypothesis::hypothesis(bool* incoming)
{
	element[0] = &incoming[rand() % 10];
	element[1] = &incoming[rand() % 10];
}

hypothesis::~hypothesis() {}


anyway you can imagine why i need a lot of hypothesis' and why they need access to the stream of data inorder to make their guesses.

thanks again!
Last edited on
bump
Topic archived. No new replies allowed.