Visualizzazione dei risultati da 1 a 4 su 4
  1. #1
    Utente di HTML.it
    Registrato dal
    Jun 2003
    Messaggi
    4,826

    [c++ MFC]passaggio array come indirizzo:

    ciao.
    Non riesco a capire (e è un po che ci sbatto la testa) come passare un array di puntatori ad una funzione che li "riempie"
    Nel seguente caso sto cercando di riempire gli array nodes e pItems
    codice:
    bool CSelectionFolder::TreeNodeClick(HTREEITEM* hItem)
    {
    
    	HTREEITEM *nodes = NULL;//array da riempire
    	CBaseSelectionSection* pItems= NULL;//array da riempire
    	GetAllChildren(hItem,&nodes,&pItems);
    	
    	for (int i=0;i<(2);i++)
    	{
    		CBaseSelectionSection* pItem =pItems[0];
    			
    			
    	}
    	
    	
    	
    	return true;
    }
    
    bool CSelectionFolder::CreateListViewItem(CBaseSelectionSection sel)
    {	
    	
    	return true;
    }
    
    void CSelectionFolder::GetAllChildren(HTREEITEM* hmyItem,HTREEITEM *&hNodes,CBaseSelectionSection*& TagNodes)
    {
    	if (m_tree->ItemHasChildren(*hmyItem))
    	{
    		HTREEITEM hNextItem;
    		HTREEITEM hChildItem = m_tree->GetChildItem(*hmyItem);
    		int idx=0;
    		while (hChildItem != NULL)
    		{
    			hNextItem = m_tree->GetNextItem(hChildItem,  TVGN_CHILD );
    			hNodes[idx]=hNextItem;
    			
    			CBaseSelectionSection* pItem =(CBaseSelectionSection*) m_tree->GetItemData(hChildItem);
    			TagNodes[idx]=*pItem;
    			
    			hChildItem = hNextItem;
    			idx++;
    		}
    	}
    }
    scusate se è un caso gia visto ma ho cercato nel forum ma non sono riuscito a trovare quello che vi chiedo ora.

  2. #2
    Utente di HTML.it L'avatar di shodan
    Registrato dal
    Jun 2001
    Messaggi
    2,381
    Ti sei accorto che stai passando un CBaseSelectionSection** ad una funzione che accetta un CBaseSelectionSection*& che non è la stessa cosa?
    E che quando eseguirà questa istruzione: TagNodes[idx]=*pItem; il programma s'impallerà visto che passi NULL come parametro?
    (Stesso discorso per l'altro puntatore ovviamente).
    This code and information is provided "as is" without warranty of any kind, either expressed
    or implied, including but not limited to the implied warranties of merchantability and/or
    fitness for a particular purpose.

  3. #3
    Utente di HTML.it
    Registrato dal
    Jun 2003
    Messaggi
    4,826
    ho visto shodan,ma come faccio a dichiarare un array di CBaseSelectionSection fuori da una funzione che lo "carica" senza sapere quanto quest'array è lungo?
    devo usare per forza un contenitore?
    se si quale contenitore mfc mi consigli?
    Grazie.

  4. #4
    Utente di HTML.it L'avatar di shodan
    Registrato dal
    Jun 2001
    Messaggi
    2,381
    Quello è un altro problema. Qui stavi cercando di far passare un quadrato nel buco di un triangolo. Se nel prototipo vuoi ricevere un reference al puntatore, alla chiamata passi solo il puntatore, non l'indirizzo del puntatore.

    Detto questo. Se non sai prima deve essere lungo il tuo array C, allora è più indicata una lista (con quello che ne consegue). Non conoscendo container MFC (forse un SafeArray ma vado a spanne), io userei un std::vector.
    codice:
    bool CSelectionFolder::TreeNodeClick(HTREEITEM* hItem)
    {
    
    	std::vector<HTREEITEM*> nodes ;//vector da riempire
    	std::vector<CBaseSelectionSection*> pItems ;//vector da riempire
    	GetAllChildren(hItem,nodes,pItems);
    	
    	for (int i=0;i<(2);i++)
    	{
    		CBaseSelectionSection* pItem =pItems[0];
    			
    			
    	}
    	
    	
    	
    	return true;
    }
    
    bool CSelectionFolder::CreateListViewItem(CBaseSelectionSection sel)
    {	
    	
    	return true;
    }
    
    void CSelectionFolder::GetAllChildren(HTREEITEM* hmyItem,std::vector<HTREEITEM*>& hNodes,std::vector<CBaseSelectionSection*>& TagNodes)
    {
    	if (m_tree->ItemHasChildren(*hmyItem))
    	{
    		HTREEITEM* hNextItem; // è un puntatore immagino.
    		HTREEITEM* hChildItem = m_tree->GetChildItem(*hmyItem); // anche questo
    		while (hChildItem != NULL)
    		{
    			hNextItem = m_tree->GetNextItem(hChildItem,  TVGN_CHILD );
    			hNodes.push_back(hNextItem);
    			
    			CBaseSelectionSection* pItem = reinterpret_cast<CBaseSelectionSection*>(m_tree->GetItemData(hChildItem));
    //			CBaseSelectionSection* pItem =(CBaseSelectionSection*) m_tree->GetItemData(hChildItem);
    			TagNodes.push_back(pItem);
    			
    			hChildItem = hNextItem;
    		}
    	}
    }
    Una nota sul cast. Il reinterpret_cast<> è il tipo di cast che più si avvicina al cast stile C, ma sarebbe meglio usarlo solo se uno static_cast fa fallire la compilazione. In caso il puntatore di cui fare il cast sia di una classe polimorfica, è meglio fare un dynamic_cast<> e controllare se il puntatore sia valido. (Cosa che il reinterpret_cast<> non fa.)
    This code and information is provided "as is" without warranty of any kind, either expressed
    or implied, including but not limited to the implied warranties of merchantability and/or
    fitness for a particular purpose.

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.