Register for your free account! | Forgot your password?

Go Back   elitepvpers > Coders Den > C/C++
You last visited: Today at 21:31

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



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

Discussion on [Question]How to create memver for array of class instance within the C/C++ forum part of the Coders Den category.

Reply
 
Old   #1
 
Lazeboy's Avatar
 
elite*gold: 0
Join Date: Jun 2008
Posts: 451
Received Thanks: 410
[Question]How to create memver for array of class instance

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
Lazeboy is offline  
Old 10/18/2012, 15:59   #2
 
elite*gold: 9
Join Date: Dec 2009
Posts: 1,071
Received Thanks: 819
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!
.Infinite is offline  
Old 10/18/2012, 17:32   #3
 
elite*gold: 5
Join Date: Sep 2006
Posts: 385
Received Thanks: 218
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();
}
Nightblizard is offline  
Thanks
1 User
Old 10/18/2012, 18:35   #4
 
Lazeboy's Avatar
 
elite*gold: 0
Join Date: Jun 2008
Posts: 451
Received Thanks: 410
but can i implment sth like the foos.size in mein own class that i dont need vector
Lazeboy is offline  
Old 10/18/2012, 18:51   #5
 
elite*gold: 5
Join Date: Sep 2006
Posts: 385
Received Thanks: 218
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.
Nightblizard is offline  
Old 10/18/2012, 23:24   #6
 
Lazeboy's Avatar
 
elite*gold: 0
Join Date: Jun 2008
Posts: 451
Received Thanks: 410
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 ?
Lazeboy is offline  
Old 10/19/2012, 00:18   #7
 
elite*gold: 9
Join Date: Dec 2009
Posts: 1,071
Received Thanks: 819
C:\Program Files\Microsoft Visual Studio 10.0\VC\include\vector

see yourself. looks quite complex to me though
.Infinite is offline  
Old 10/19/2012, 03:11   #8
 
Lazeboy's Avatar
 
elite*gold: 0
Join Date: Jun 2008
Posts: 451
Received Thanks: 410
thank you xD but i think i will use vector and dont try to understand the background this times 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
Lazeboy is offline  
Old 10/19/2012, 11:42   #9


 
MrSm!th's Avatar
 
elite*gold: 7110
Join Date: Jun 2009
Posts: 28,908
Received Thanks: 25,409
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.
MrSm!th is offline  
Old 10/19/2012, 12:22   #10
 
Lazeboy's Avatar
 
elite*gold: 0
Join Date: Jun 2008
Posts: 451
Received Thanks: 410
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.
Lazeboy is offline  
Reply


Similar Threads Similar Threads
Wich class are best to create for goldboting?
02/24/2012 - Silkroad Online - 5 Replies
Hey guys, i know that all class are good in boting. But with some are better to do "gold boting" I was thinking to create wizzard/cleric. But i dont know i think wizzards are dieng fast. Then i thought about nuker Cold/Fire/Light. Im only interested in gold boting. I need that account kill fast and dont die.. So wich classes are best? Please tell me ;< Need fast answer.. Because i wanna farm some novas in Alexandria and cash^^
In Mangos Character create Race/class block
07/21/2010 - WoW Private Server - 4 Replies
Hallo, ich wollte mir folgendes Patch auf meinen Server einspielen:Patch. Gut und schön die dem entsprechenden Stellen hinzugefügt bei den dementsprechenden Datein, nur spuckt mir c++ ständig syntaxfehler und was weiß ich noch alles aus. Kann mir vll jemand erklären wie man sowas einfügt bzw was ich falsch mach?
[question] sql basis data array code XD
02/15/2010 - Dekaron Private Server - 2 Replies
what is sql basis data array code alteration method? not php character encoding problem if see screenshot but it seems to be problem of sql data array code XD Help me :( http://pcmaket.com/images/guild.gif
question-adding new adress to CE+array of bytes
11/10/2009 - Dekaron - 8 Replies
Hello There. Lets say i wanna add a new Hack to Cheat engine besides the hacks i allready have lets say i want to add vacum hack and i don't have.just for example. I have the script and every thing needed now what do i do? another question is can any one give me the array of bytes for 0.5 teleporter? the one cheat that teleports me to certain maps when stepping on 0.5 cordinates Thanks in advance for you'r support!



All times are GMT +1. The time now is 21:33.


Powered by vBulletin®
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2026 elitepvpers All Rights Reserved.