private class Node
{
public int Value;
public Node[] ChildNodes;
public bool Selected;
}
The class Node contains an array of Nodes.
private class Node
{
public int Value;
public Node[] ChildNodes;
public bool Selected;
}
The class Node contains an array of Nodes.
My C++ is very rusty. Before we tackle the coding there are some things you need to know.
Classes can not directly have access modifiers like public
, protected
, private
or in C++. Meaning we are getting a problem already at
private class Node
.
This you can go around by using an Nested Class
This brings (psuedo) code to this:
class Outer
{
private:
class Node
{
public:
}
};
We then have to add the the fields, usually (at least used to) in C++ you are using a prefix for variable names, much like capitalizing fields in C#.
int mValue;
Node* mNodes;
bool mSelected;
Which brings us to the final Class:
outer.h
#ifndef OUTER_H
#define OUTER_H
class Outer
{
private:
class Node
{
public:
int mValue;
Node* mNodes;
bool mSelected;
}
};
#endif
More of the "Include guard" can be read here. In some enviroment you can use #pragma once
instead of the guards.
I hope this gets you started. This code has not been tested.
This is a bit of XYZ problem, we don't know for what reason you want to write it in C++ instead of C#. Working with C++ can be a hassle. I also (in both cases) recommend to work with a List in C# a Vector in C++ instead of an Array.
First of all, private classes in C# must be contained within an outer type, so we can assume that this type is already nested (i.e., neither C# nor C++ allows a private class at the namespace level). Next, it's much easier to use std::vector instead of actual arrays:
#include <vector>
//class Outer { - your existing outer class
class Node
{
public:
int Value = 0;
std::vector<Node*> ChildNodes;
bool Selected = false;
};
//}; - end of your existing outer class