PDA

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



kjohansen
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.

maddog39
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.

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



class LinkedList
{
public:
...

private:
struct Node
{
int data;
Node* next;
};

Node* m_head;
};