Accessing Array of objects

Hi guys

I'm having a little problem in my code, but I don't know how to solve it.

I have a class called Myclass. This class have a public var, called Tag, where I save a int number.

1
2
3
4
5
6
7
class Myclass
{
	public:
		int Tag;
		Myclass();
		
};


Now, in the main function, I creates an array of Myclass, so

 
   Myclass** classes = (Myclass**)malloc(sizeof(Myclass)*100);


later, I assigns a value to each Tag, belonging to each array element

1
2
3
4
5
6
7
8
9
10
11
12
int main()
{
   Myclass** classes = (Myclass**)malloc(sizeof(Myclass)*100);

   for (int i=0; i < 100; i++)
   {
	classes[i] = new Myclass();
	classes[i]->Tag = i;
   }

   printf ("The value of net[5]->Tag = %d\n", (*((Myclass*)classes+5)).Tag);
}


but when I want to print the value of classes[5]->Tag, im getting the address of the value, but no the int number of Tag.

I know that I can use an indexer (like, classes[5]->Tag), but I need to access the element in the shown way.

Somebody can tell me how can I fix this problem.

Thanks in advance
Well I'm confused but you could just create a normal array instead of an array of pointers.

 
Myclass* classes = (Myclass*)malloc(sizeof(Myclass)*100);


Note: you have malloc(sizeof(Myclass)*100) which is probably going to give you more than 100 pointers (possibly - depends on size of class). should be malloc(sizeof(Myclass*)*100) for an array of 100 pointers.

With the normal array you just use classes[i].Tag to get the element
(this cannot be used for polymorphic classes)
Hi mik2718

I have tried with

Myclass* classes = (Myclass*)malloc(sizeof(Myclass)*100);

but when I make

for (int i=0; i < 100; i++)
{
classes[i] = new MyClass();
classes[i].Tag = i;
}

I get an error, because I can't use the the class's constructor

main.cpp: In function `int main()':
main.cpp:46: error: no match for 'operator=' in '*(classes + (+(i * 8))) =
(operator new(8), ((true, (<anonymous>->Myclass::Myclass(), (<anonymous>
<unknown operator> false))), <anonymous>))'
main.cpp:17: error: candidates are: Myclass& Myclass::operator=(const Myclass&)

Why I can't use the constructor???


Thanks

Don't do this:
 
Myclass** classes = (Myclass**)malloc(sizeof(Myclass)*100);
You really should use a standard container. Or if you insist on allocating your array directly, please use new. Just don't use malloc/free in C++. new/delete are operators that can be tuned (overloaded).

I really can't say this enough, but DO NOT CAST. C++ is all about static type checking that the compiler can do at compile time. The language has a constrained object model just to support this singularly important feature. If you're going start casting, you may as well be using something based on the Smalltalk object model where you have all kinds of run-time benefits.

Your print statement should be:
 
printf("The value of net[5]->Tag = %d\n", classes[5]->Tag);
Last edited on
If you use new to allocate the array the constructor is called for each object

 
Myclass* classes = new Myclass [100];


remember to use delete [] to free the memory

where did you get malloc from?
Hello.

But, if the constructor is called for each object, how can I to pass the parameters for the constructor?
You can't. That use implies the default constructor, but that's what your sample code uses.

It's the use of malloc that's the real problem with your code. It's wrong in principle.

If could be written as:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main()
{
   Myclass** classes = new Myclass*[100];
   for (int i=0; i < 100; ++i)
   {
	classes[i] = new Myclass();  // default constructor
	classes[i]->Tag = i;
   }

   printf ("The value of net[5]->Tag = %d\n", classes[5]->Tag);

   for (int i=0; i < 100; ++i)
      delete classes[i]
   delete [] classes;
}
Last edited on
1
2
3
std::vector<foo> array;
array.reserve(n);
array.push_back( foo(params) );
Topic archived. No new replies allowed.