Click here to Skip to main content
16,021,449 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Can this be the cause of a memory leak Pin
CPallini9-May-24 4:58
mveCPallini9-May-24 4:58 
AnswerRe: Can this be the cause of a memory leak Pin
charlieg23-May-24 1:04
charlieg23-May-24 1:04 
QuestionUSB power control Pin
etechX29-May-24 18:27
etechX29-May-24 18:27 
AnswerRe: USB power control Pin
jschell9-May-24 15:31
jschell9-May-24 15:31 
AnswerRe: USB power control Pin
RedDk10-May-24 8:24
RedDk10-May-24 8:24 
AnswerRe: USB power control Pin
charlieg20-May-24 4:44
charlieg20-May-24 4:44 
Questionforeach....recursively - assistance with C++ code wnated Pin
Salvatore Terress8-May-24 6:08
Salvatore Terress8-May-24 6:08 
AnswerRe: foreach....recursively - assistance with C++ code wnated Pin
k50548-May-24 7:03
mvek50548-May-24 7:03 
There's probably a nice template way to do this. But if I understand you're problem, here's a simple C++ program with multi-level tree that you may be able to adapt to your situation:
C++
#include <vector>
#include <iostream>

class Tree {
public:
    int datum;
    std::vector<Tree> children;
    Tree(int d) :datum(d) {}
};

void walk(Tree& t, size_t n, void fn(const Tree& t, size_t n) )
{
    fn(t, n);
    for(auto child: t.children)
        walk(child, n+1, fn);
}

int main()
{
    Tree t(0);
    for(int x = 1; x < 3; ++x)
    {
        Tree tmp(x);
        t.children.push_back(tmp);
    }
    for(int y = 100; y < 500; y += 100 )
    {
        Tree tmp(y);
        t.children[0].children.push_back(tmp);
    }

    walk(t, 0,  [](const Tree& t, size_t indent) { std::cout << std::string(indent*3, ' ') << t.datum << '\n'; });
}
This processes the parent before the children, but you can reverse the lines of the walk function to do the children first, if preferred. foreach is not part of the C++ standard, but the ranged for loop has been since C++-11, so your compiler almost certainly supports it. Whether QT containers will work with ranged for loops is not known to me. Still, the idea is the same. Create a recursive function that processes the parent, then calls itself on each of the children.
I'm sure there's a more generic way to do this using templates, that parameterizes the function, which would mean you don't have to write a separate walk function for every distinct function prototype on fn. Maybe that's an exercise you could undertake to help improve your understanding of templates, and other C++ facilities.
"A little song, a little dance, a little seltzer down your pants"
Chuckles the clown


modified 8-May-24 13:32pm.

GeneralRe: foreach....recursively - assistance with C++ code wnated Pin
Salvatore Terress8-May-24 8:19
Salvatore Terress8-May-24 8:19 
GeneralRe: foreach....recursively - assistance with C++ code wnated Pin
k50548-May-24 8:57
mvek50548-May-24 8:57 
GeneralRe: foreach....recursively - assistance with C++ code wnated Pin
Salvatore Terress8-May-24 17:09
Salvatore Terress8-May-24 17:09 
GeneralRe: foreach....recursively - assistance with C++ code wnated Pin
Salvatore Terress10-May-24 13:23
Salvatore Terress10-May-24 13:23 
Questionvoluntarily REMOVED Which way is correct ? Pin
Salvatore Terress6-May-24 10:19
Salvatore Terress6-May-24 10:19 
AnswerRe: Which way is correct ? Pin
Richard MacCutchan7-May-24 0:29
mveRichard MacCutchan7-May-24 0:29 
AnswerRe: voluntarily REMOVED Which way is correct ? Pin
Richard MacCutchan9-May-24 5:59
mveRichard MacCutchan9-May-24 5:59 
QuestionPassing "parent" error Pin
Salvatore Terress4-May-24 6:51
Salvatore Terress4-May-24 6:51 
AnswerRe: Passing "parent" error Pin
Richard MacCutchan5-May-24 2:45
mveRichard MacCutchan5-May-24 2:45 
Questionchange exe icon not work with multiple stage icon file. Pin
Le@rner1-May-24 2:26
Le@rner1-May-24 2:26 
AnswerRe: change exe icon not work with multiple stage icon file. Pin
Victor Nijegorodov1-May-24 4:21
Victor Nijegorodov1-May-24 4:21 
GeneralRe: change exe icon not work with multiple stage icon file. Pin
jschell1-May-24 13:09
jschell1-May-24 13:09 
AnswerRe: change exe icon not work with multiple stage icon file. Pin
CPallini2-May-24 4:07
mveCPallini2-May-24 4:07 
GeneralRe: change exe icon not work with multiple stage icon file. Pin
Dave Kreskowiak2-May-24 4:11
mveDave Kreskowiak2-May-24 4:11 
GeneralRe: change exe icon not work with multiple stage icon file. Pin
CPallini2-May-24 4:14
mveCPallini2-May-24 4:14 
QuestionSOLVED Posting "debug (window ) view ? Pin
Salvatore Terress30-Apr-24 2:51
Salvatore Terress30-Apr-24 2:51 
AnswerRe: Posting "debug (window ) view ? Pin
Maximilien30-Apr-24 2:56
Maximilien30-Apr-24 2:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.