[Question]How to create memver for array of class instance

10/18/2012 15:19 Lazeboy#1
Hey,
i dont know how do search in google for this question so i decided to ask it here.
I am trying to do something like that.

Code:
class A
{
private:
	int variable;
};

int main()
{
	A a[10];
	for(int i = 0; i < a->size;i++)
		a[i].variable = i;		
}
it should only demonstrate my plan very easy. The variable size should only be used by an array of the class.

Does anybody knows how to do that ?
sorry for the mistake in the title
mfg Lazeboy
10/18/2012 15:59 .Infinite#2
I don't really get what you're trying to do...

Code:
for(int i = 0; i < sizeof(a) / sizeof(a[0]);i++)
//...
If you want to use 'variable' outside of the class you have to declare it as public!
10/18/2012 17:32 Nightblizard#3
In C++ you almost always want to use std::vector instead of plain old unsafe C-arrays.

Code:
#include <vector>
#include <iostream>

class Foo
{
private:
	int bar;
public:
	Foo() : 
		bar(0)
	{
	}

	int getBar()
	{
		return bar;
	}

	void setBar(int i)
	{
		bar = i;
	}
};

int main()
{
	const std::size_t SIZE = 10;

	std::vector<Foo> foos(SIZE);

	for(int i = 0; i < foos.size(); ++i)
		foos[i].setBar(i);


	std::cin.get();
}
10/18/2012 18:35 Lazeboy#4
but can i implment sth like the foos.size in mein own class that i dont need vector
10/18/2012 18:51 Nightblizard#5
Why don't you want to use vector? As I've said, there is almost no reason not to. Furthermore why do you want to store foos size inside each element? Could you give me some more information about what you're trying to achieve? It doesn't sound reasonable to me.
10/18/2012 23:24 Lazeboy#6
Quote:
Originally Posted by Nightblizard View Post
Why don't you want to use vector? As I've said, there is almost no reason not to. Furthermore why do you want to store foos size inside each element? Could you give me some more information about what you're trying to achieve? It doesn't sound reasonable to me.
i can use vector thats right but only for my knowledge i want to know how vector class does it.

Code:
class A
{
private:
	int variable;
};

int main()
{
	A a[10];
	for(int i = 0; i < a.size;i++) // i want in my class A sth that give me the range of a created array of instance
		a[i].variable = i;	

//the same like
        std::vector<A > a(10);
	for(int i = 0; i < a.size;i++) 
		a[i].variable = i;	
	
}
how does the vector class realize that ?
10/19/2012 00:18 .Infinite#7
C:\Program Files\Microsoft Visual Studio 10.0\VC\include\vector

see yourself. looks quite complex to me though ;)
10/19/2012 03:11 Lazeboy#8
thank you xD but i think i will use vector and dont try to understand the background this times :D i am going on working at my 2d mario remake more important. i only was interested in it because it would be nice to do sth like that.

A a[99]
a->size // size = 99 :D
10/19/2012 11:42 MrSm!th#9
What you are trying to achive is not possible. The difference is that you are creating a class A and then creating an array with 10 elements of A while vector is a class that represents the array itself.
Just compare it to an int array:

Code:
 int x[10];
for(int i=0; i < x.size; i++)
Since the native C array does not store the size unlike in Java, in your argumentation you wouls have to add it to int as you are trying to add it to A.

See? If you want to understand vector (which is basically not that complicated.. It's just Microsoft's standard headers that seem a bit complicated to beginners), you have to write your own class that stores other elements. You just created a simple class and made an array of it; how should the elements know about the array size? And the array itself cannot know it, because it's a C style array and C does not know classes.

Btw. for arrays you can use the ARRAYSIZE macro or the C++ class std::array.
10/19/2012 12:22 Lazeboy#10
Quote:
Originally Posted by MrSm!th View Post
What you are trying to achive is not possible. The difference is that you are creating a class A and then creating an array with 10 elements of A while vector is a class that represents the array itself.
Just compare it to an int array:

Code:
 int x[10];
for(int i=0; i < x.size; i++)
Since the native C array does not store the size unlike in Java, in your argumentation you wouls have to add it to int as you are trying to add it to A.

See? If you want to understand vector (which is basically not that complicated.. It's just Microsoft's standard headers that seem a bit complicated to beginners), you have to write your own class that stores other elements. You just created a simple class and made an array of it; how should the elements know about the array size? And the array itself cannot know it, because it's a C style array and C does not know classes.

Btw. for arrays you can use the ARRAYSIZE macro or the C++ class std::array.

yes after sleeping i saw that i understand the vector wrong ^^. thank you for the help. :)