Re: Design orientated 'C' question
- From: Robby <Robby@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Sun, 26 Mar 2006 17:16:03 -0800
Hi David,
How are you, hope everything is Okay!
All right, I got your point #1, In 'C', 'typedef' if I want to use that
funny tag.... prefix so I can then declare a variable without the klutzy
struct key word.
And in C++, I can simply difine my structure without using 'typedef' and the
tag... prefix so I can do stuff like this:
struct IdRoomIndex
{
int iRoomNumber;
TCHAR *szRoomName;
};
idRoomIndex z;
z.iRoomIndex = 5;
Now, point #2, you say:
"The only things that should go in header files are structure (or class in
C++) definitions and function declarations (prototypes). No variables! "
I used the wrong word in my comment above the structure. Instead of
"Declaration of structure" it should of been:
//Definition of structure
struct tagIdRoomIndex
{
int iRoomNumber;
TCHAR *szRoomName;
};
So isn't this structure definition allowed in my header?
As for point #3, you say:
"Every .cpp file that uses structure/class definitions or function
declarations from a header must include that header."
So then, in relation to my original post, in C or C++, it is *OBLIGATORY*
that I include Globals.h in WndProc_CW1.cpp and in LIB1_FUNCTIONS.cpp.
So what you are saying is that my program in my innitial post would not
compile if I had defined the structure in WndProc_CW1.cpp only and removed
Globals.h!
What I am getting at here..., is that I would of prefered to include the
Globals.h file only in WndProc_CW1.cpp and somehow pass the structure and
aswell as the array of structures through the function so that F1() would
have everthing it needs to access any member of a structure within the array
of strucutres. But I guess not!
I guess thats why I was going crazy looking at all kinds of web sites and
looking for some example showing this kind of design, but it didin't really
exist!
Anyways I did see some samples on the Internet doing something similar, but
they were using pointers and anyways... I am just being over inquisitive at
this point, maybe someday this will puzzle together..... Its been a pleasure
David, talk to you soon, and sorry if I was stuburn on the issue....
P.S., Eugenio thanks for your post aswell
--
Best regards
Robert
"David Wilkinson" wrote:
Robby wrote:.
Hi,
Please consider the following code:
Globals.h
=================================================
//Declaration of structure
struct tagIdRoomIndex
{
int iRoomNumber;
TCHAR *szRoomName;
};
=================================================
WndProc_CW1.cpp
==================================================
#include <windows.h>
#include "Globals.h"
//Function declaration
void L1_txtDISPLAY_ArSt2MEMB( HDC, struct tagIdRoomIndex RIndex[] );
//Declaration of an array of structures
tagIdRoomIndex RIndex[] =
{
1 ,TEXT("ABC"),
2 ,TEXT("DEF"),
3 ,TEXT("GHI"),
4 ,TEXT("JKL"),
};
LRESULT CALLBACK WndProc_CW1 (HWND hwnd, UINT message,
WPARAM wParam, LPARAM lParam)
{
switch(message)
{
WM_PAINT:
F1( hdc,RIndex); //Calling function
break;
}
//Other code....
}
==================================================
LIB1_FUNCTIONS.cpp (Function implementations)
==================================================
#include <windows.h>
//#include "Globals.h" //Already included in WndProc.cpp
F1(HDC hdc,struct tagIdRoomIndex RIndex[])
{
//Other code ...
//Errors point to the < RIndex[i].iRoomNumber)); > part of line below
TextOut (hdc,0,0,szBuffer,
wsprintf(szBuffer,TEXT("%d"),RIndex[i].iRoomNumber));
}
==================================================
This code compiles OKAY if I include the following line of code in the
function implementation file called LIB1_FUNCTIONS.cpp
#include "Globals.h"
However if I don't include "Globals.h" I get several errors. Here are 2 of
them:
c:\_DTS_PROGRAMMING\vc++\MY_APPS_LAB\APPLICATION_1\LIB1_FUNCTIONS.cpp(74) :
error C2036: 'tagIdRoomIndex []' : unknown size
c:\_DTS_PROGRAMMING\vc++\MY_APPS_LAB\APPLICATION_1\LIB1_FUNCTIONS.cpp(74) :
error C2027: use of undefined type 'tagIdRoomIndex'
My question is, when I am passing the array of structures by the calling
function:
F1(hdc,RIndex);
Am I not passing the array and the structure. I have tried the following
ways to pass the array of structures in the calling function:
F1( hdc,RIndex); //Calling function
OR
F1( hdc,tagIdRoomIndex RIndex); //Calling function
And I always get errors.....
I find it redundant if I have to include the "Globals.h" once in WndProc_CW1
and once again in LIB1_FUNCTIONS.cpp
I don't mind, I can include it twice! *I could live with this* However Isn't
this bad coding habits?
As far as where my declarations take place, I believe that declaring a
structure in a header would not really be a bad idea, right...I mean, I have
seen it in book samples!
But then again, they were not books that showed serious examples under the
context dealing with structuring of programming.
I would appreciate all feedback on this issue... Thankyou all.
All suggestions are appreciated
Robby:
A few things:
1. The reason you use a funny name like tagIdRoomIndex is so you can
typedef it
typedef struct tagIdRoomIndex
{
int iRoomNumber;
TCHAR *szRoomName;
} IdRoomIndex;
Now you can use the identifier IdRoomIndex without the klutzy struct
keyword. Actually, if you are compiling your programs as C++, this
device is not necessary, because the struct keyword is not needed and
you can do
struct IdRoomIndex
{
int iRoomNumber;
TCHAR *szRoomName;
};
and use IdRoomIndex without the struct keyword.
2. The above is actually called a structure definition (not
declaration). The only things that should go in header files are
structure (or class in C++) definitions and function declarations
(prototypes). No variables!
3. Every .cpp file that uses structure/class definitions or function
declarations from a header must include that header. Remember, every
..cpp file must compile by itself. It is absolutely NOT bad to include a
header in more than one .cpp file. In fact the purpose of the header is
to ensure that every .cpp file that uses the things in it is using the
same version.
Once again I advise you, when you get confused, to try compiling each
..cpp file separately. If it doesn't compile, figure out why (the answer
does not depend on anything in any other .cpp file).
David Wilkinson
- Follow-Ups:
- Re: Design orientated 'C' question
- From: David Wilkinson
- Re: Design orientated 'C' question
- References:
- Design orientated 'C' question
- From: Robby
- Re: Design orientated 'C' question
- From: David Wilkinson
- Design orientated 'C' question
- Prev by Date: Re: Function call evaluation order
- Next by Date: Re: Rounding float value
- Previous by thread: Re: Design orientated 'C' question
- Next by thread: Re: Design orientated 'C' question
- Index(es):
Relevant Pages
|