Simple Program Structure Question.

Started by
4 comments, last by hellz 20 years ago
Hey all. Simple question, let''s assume you have a container class setup something like the following:

// SomeClass.h

class CSomeClass
{
   public:
      SOMESTRUCT struct;
};
Where exactly would you define the struct? Would you put it above the class declaration in the header file, or would you still create a separate file for it? Pedantic I know, but I''m thinking more along the lines of maintainability and program management. Thanks in advance, -hellz
Advertisement
Depending on how complex the struct is, it probably isn''t necessary to declare it in its own .h

Of course there are many uses for struct in c++, it can be used as a kind of "poor man''s class", or to create function objects, to do various other strange things, and even as a sort of fake namespace.

But if it''s just a C-style struct (i.e. containing variable and type declarations), then there''s probably no need to make a song and dance.

Mark
Well,

There are multiple ways to go around about this. The way you are doing it is ok, thats how I use them sometimes. You could do something like:

// SomeClass.hstruct CSomeStruct{public:      void myFunction(void);      int myInteger;private:      int hiddenInt;}class CSomeClass{public:      CSomeStruct myStruct;};Then you could call on myFunction like:   CSomeClass *myClass = new CSomeClass;myClass->myStruct.myInteger = 1;// or for a functionvoid CSomeStruct::myFunction(void) {      myInteger = 5;      hiddentInt = 3;}Remember, since myFunction is called within CSomeStruct, myInteger can be called directly from your function. Now you can not call hiddenInt outside of the class/structure, only functions inside can.You can also make a typedef struct to have extra variables attached.   typedef struct {	int variable;} extraVars;// add under public area in CSomeStructextraVars vars;// thenmyClass->myStruct.vars.variable = 5;// vars is extraVars and "variable" is inside the struct


It's usually a good idea to keep one header file with all the stuctures and classes inside. At least in my opinion. Then when you are ready to use them just include one .h file that holds all your structures, classes and typedef structs.

Also dont forget the:

#ifndef MYHEADER_H
#define MYHEADER_H

// code

#endif

In your header file.


Hope this all made sense,
[BDS]StackOverflow

[edited by - BlueDev on March 23, 2004 4:03:31 PM]
[/quote]
Thanks for the replies guys.

I guess the reason I''m questioning where to put the struct, instead of just making a separate header file for it as normal, is because of the relationship it holds with the class that contains it.

I guess I''ll just stick it in a header file anyway. Thanks again!

-hellz
Hm, just to buzz in... What usually works is to think whether there are any other parts (classes, functions etc) that may need to use your struct but not your class. If that''s the case it might be a good idea to put it in a separate header (if you need to loosen up dependancies), otherwise you can just stick it on top of the class'' header.
Yeah, amag, that was initially what caused my confusion. I know that only the class I''m designing will need to have any kind of interaction with the struct; I can''t see anything else needing it.

-hellz

This topic is closed to new replies.

Advertisement