Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » Community » Newbie corner » Void pointer to NTL Array and VectorMap
Void pointer to NTL Array and VectorMap [message #26390] Thu, 29 April 2010 19:14 Go to next message
manfhe is currently offline  manfhe
Messages: 5
Registered: April 2010
Location: Brasil
Promising Member
Hello

I have a void pointer to an NTL Array and VectorMap, how do I receive the elements through the key?
The code looks like this:


	typedef VectorMap<String, String> ntlMap;
	typedef Array<String> ntlArray;
	
	void* ptra;
	void* ptrm;
	
	ptra = new ntlArray;
	ptrm = new ntlMap;
	
	for(int i = 0; i < ((ntlMap*)ptrm)->GetCount(); i++)
		((ntlArray*)ptra)->???? = ((ntlMap*)ptrm)->????;


And I would like to thank all contributors to this project. For beginners in C + + as I, U + + is a true teacher.

Grateful, Alan
Re: Void pointer to NTL Array and VectorMap [message #26392 is a reply to message #26390] Thu, 29 April 2010 19:45 Go to previous messageGo to next message
dolik.rce is currently offline  dolik.rce
Messages: 1789
Registered: August 2008
Location: Czech Republic
Ultimate Contributor

Hi Allan,

You can always use operator[](). It looks a bit ugly, but it should work fine:
	for(int i = 0; i < ((ntlMap*)ptrm)->GetCount(); i++)
		((ntlArray*)ptra)->operator[](i) = ((ntlMap*)ptrm)->operator[](i);
Maybe there is a nicer way to do this, but in U++ there is so little cases where pointers are needed that I never really bothered to investigate any other possible solutions Cool

Best regards,
Honza
Re: Void pointer to NTL Array and VectorMap [message #26393 is a reply to message #26392] Thu, 29 April 2010 20:54 Go to previous messageGo to next message
manfhe is currently offline  manfhe
Messages: 5
Registered: April 2010
Location: Brasil
Promising Member
Thanks Honza.

Work well.
Re: Void pointer to NTL Array and VectorMap [message #26394 is a reply to message #26393] Thu, 29 April 2010 22:16 Go to previous messageGo to next message
hojtsy is currently offline  hojtsy
Messages: 241
Registered: January 2006
Location: Budapest, Hungary
Experienced Member
Hi, a shorter version is below. But there is usually a better alternative to using void* type in the first place.

for(int i = 0; i < ((ntlMap*)ptrm)->GetCount(); i++)
    (*(ntlArray*)ptra)[i] = (*(ntlMap*)ptrm)[i];


Regards,
Sandor
Re: Void pointer to NTL Array and VectorMap [message #26400 is a reply to message #26390] Fri, 30 April 2010 00:01 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
[quote title=manfhe wrote on Thu, 29 April 2010 13:14]Hello

I have a void pointer to an NTL Array and VectorMap, how do I receive the elements through the key?
The code looks like this:


	typedef VectorMap<String, String> ntlMap;
	typedef Array<String> ntlArray;
	
	void* ptra;
	void* ptrm;
	
	ptra = new ntlArray;
	ptrm = new ntlMap;
	
	for(int i = 0; i < ((ntlMap*)ptrm)->GetCount(); i++)
		((ntlArray*)ptra)->???? = ((ntlMap*)ptrm)->????;


Or you can do this:

ntlMap& map = *(ntlMap *)ptrm;
ntlArray& array = *(ntlArray *)ptra;
for(int i = 0; i < map.GetCount(); i++)
     array[i] = map[i];


BTW, I only hope that you are doing something really special Smile

Using 'new' should be reserved only for really desperate cases Smile

Quote:


And I would like to thank all contributors to this project. For beginners in C + + as I, U + + is a true teacher.

Grateful, Alan


Only be aware that U++ will teach you something little bit different from "classic" approach...
Re: Void pointer to NTL Array and VectorMap [message #26401 is a reply to message #26394] Fri, 30 April 2010 00:15 Go to previous messageGo to next message
manfhe is currently offline  manfhe
Messages: 5
Registered: April 2010
Location: Brasil
Promising Member
One more doubt about void* type. Rolling Eyes

To destroy it is better: delete (ntlMap*)ptrm (This calls the destructor of the class?)
Or just: delete ptrm (So only destroy the pointer?)

Sorry, this is about a C + + and not U + +. I'm sorry, but I'm a beginner in C + +.

Thanks
Re: Void pointer to NTL Array and VectorMap [message #26409 is a reply to message #26401] Fri, 30 April 2010 10:46 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
manfhe wrote on Thu, 29 April 2010 18:15

One more doubt about void* type. Rolling Eyes

To destroy it is better: delete (ntlMap*)ptrm (This calls the destructor of the class?)



Yes. Once you cast it, compiler thinks it is pointer to ntlMap.

Once again, I hope that you are doing something really special. You should not use void pointers in 'normal' code. Actually, you should avoid pointers whenever possible.

Mirek
Re: Void pointer to NTL Array and VectorMap [message #26415 is a reply to message #26409] Fri, 30 April 2010 11:01 Go to previous messageGo to next message
cbpporter is currently offline  cbpporter
Messages: 1401
Registered: September 2007
Ultimate Contributor
Actually, I think it is an unwritten rule derived from years of practice both in C and C++ that you shouldn't use void pointers except for function parameters and some struct members, and even then only when there is a need to Smile. It is generally better to use typed pointers.
Re: Void pointer to NTL Array and VectorMap [message #26419 is a reply to message #26409] Fri, 30 April 2010 13:40 Go to previous messageGo to next message
manfhe is currently offline  manfhe
Messages: 5
Registered: April 2010
Location: Brasil
Promising Member
Thanks.

I need use void* to manage different types at runtime, and my understanding in C++ void* is the unique solution.
I'm trying to make a crazy table, similar to a Lua table.
Re: Void pointer to NTL Array and VectorMap [message #26426 is a reply to message #26419] Fri, 30 April 2010 15:03 Go to previous message
dolik.rce is currently offline  dolik.rce
Messages: 1789
Registered: August 2008
Location: Czech Republic
Ultimate Contributor

I'm not sure how crazy is your crazy table, but in most cases such problems can be solved using Value type. See documentation (paragraph 6) for details.

Honza
Previous Topic: image drawing
Next Topic: [SOLVED]destroying self from array
Goto Forum:
  


Current Time: Wed Apr 24 06:11:31 CEST 2024

Total time taken to generate the page: 0.01708 seconds