Reputation: 299
I am a student studying MFC.
I want to declare ChildView region variables.
Where do I have to declare variables? header file? or cpp file?
I think both work well. But I want to know general style.
Please let me know which are desirable.
// ChildView.h : interface of the CChildView class
//
#pragma once
// CChildView window
class CChildView : public CWnd
{
// Construction
public:
CChildView();
...
...
// User variables
public:
CFile* pImgFile = NULL;
ULONGLONG imgLength, frameLength;
unsigned char RRR[288][352];
unsigned char GGG[288][352];
unsigned char BBB[288][352];
unsigned char YYY[288][352];
unsigned char UUU[144][176];
unsigned char VVV[144][176];
};
or
// ChildView.cpp : implementation of the CChildView class
//
#include "stdafx.h"
#include "Doyup_YUV_HW7-8.h"
#include "ChildView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CChildView
CChildView::CChildView()
{
CFile* pImgFile = NULL;
ULONGLONG imgLength, frameLength;
unsigned char RRR[288][352];
unsigned char GGG[288][352];
unsigned char BBB[288][352];
unsigned char YYY[288][352];
unsigned char UUU[144][176];
unsigned char VVV[144][176];
}
In addition, I also wondered where define statements are located.
after (3)#pragma once in header file?
or after (9)#endif in cpp file?
Upvotes: 1
Views: 96
Reputation: 3156
This is not an MFC issue. This is a good question regarding what makes a good structured design and a good object oriented design.
Variable lifetime, variable scope (global, object, class, function, block, ...), and variable accessibility (including, but not limited to, public vs. protected vs. private member variables in a class) requires a good understanding of not just the basic semantics, but what makes a good design "good".
A general rule is that you want the lifetime of most variables to be as short as possible but as long as needed. So a for-loop index can usually be declared within the for-loop within a function, and NOT in the class (you only need the index within the loop, not outside the loop, and not within other methods in the class). But you would want the "modified" flag of your CDocument MFC object to be a member of the CDocument class so that other methods of CDocument can look at/set/clear it, and then provide a public accessor GetModified() so that other users of the CDocument object (like its corresponding CView object) can determine if the CDocument has been "modified" (say to put an * at the end of the CView's title bar text to show that it has been modified).
Upvotes: 2
Reputation: 179930
You have little choice, because the place where you declare them greatly affects the behavior. In particular, the lifetime of variables is strongly dependent on the place where they're defined.
If you define the variable inside the class definition (which is almost always in the .h file), then it's a member variable. The lifetime of the member is tied to the lifetime of the containing object. If you have 3 objects, they each have a unique lifetime, and thus you have 3 copies of each member variables with corresponding lifetimes.
If you define a variable inside a member function (which is usually in the .cpp file), then the lifetime matches the function call. If you have 3 objects, one thread, and that one thread calls the member function, you get one variable for the duration of that call.
You can also define a variable in the .cpp file outside a member function. This is a global variable, not associated with any object or class. For this reason, globals are often considered bad style.
Upvotes: 2
Reputation: 926
here we have to specify that Declaration and Implementation are not tied respectively to header file and cpp file. It's a good thing to have them separated in header file and cpp file but not restrictively necesary.
in your example the variables declared in the "header file" are declared inside the class, so they are members of that class, and you can use them from objects of that class, the variables declared in the cpp file are declared inside the constructur so they will be available ONLY in that method and cannot be used anywhere else
that also is about scope, the define in the header file can be used in other files that include it, but it will only be defined when the compiler actually gets to that point, so if you have another file that is compiled before, it will not contain that define. You might want to look at preprocessor definitions too. You might want to set your defines in the "stdafx.h" file. In your case it doesn't seem that you actually need that define because anyway you can use the _DEBUG already declared automaticaly.
Upvotes: 1
Reputation: 5801
You should prefer second way - declare variables in .cpp file in
CChildView::CChildView()
only in case you will need your variables only in constructor and won't need them in other functions.
But I think you will need these variables in other functions, too. In this case you should declare them in .h file. Please note them now you declare them as public variables, this is bad idea. You would better declare them as private variables (read about private and public variables and encapsulation). By default it is better to declare all your variables as private.
As for defines, it is better to add them to .cpp file, then they will be visible in your cpp file only. If you define something in h file, it will be defined everywhere when you include your h file and may cause conflicts.
Upvotes: 2