View Full Version : [C++] Struct in class

March 27th, 2009, 06:07 PM
I have the below in my header file:

struct Node
double a;
double * weights; //incoming weights
double * weights_change_lag; //for momentum
double bias_weight;
double bias_weight_change_lag; //for momentum
double delta;
double in;

struct Layer
int num_nodes;
Node * nodes;

class NeuralNetwork
//some prototypes

Is there a convention for where the structs should go?

Should the structs be where they are or should they go inside the class definition?

These structs could be private, they do not need to be used outside of the implementation.

March 27th, 2009, 07:00 PM
You can do it either way. The compiler treats structures as classes with public as the default. So you can either be inside the class as private or protected sub classes or you can leave them the way they are. Use the method that makes the most sense for you application.

March 27th, 2009, 09:25 PM
It is not uncommon to see nested classes/structs. For example:

class LinkedList

struct Node
int data;
Node* next;

Node* m_head;