Click here to Skip to main content
15,909,242 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Release VS Debug compiles Pin
Richard MacCutchan1-Feb-11 6:13
mveRichard MacCutchan1-Feb-11 6:13 
AnswerRe: Release VS Debug compiles Pin
Andrew Brock1-Feb-11 4:50
Andrew Brock1-Feb-11 4:50 
GeneralRe: Release VS Debug compiles [modified] Pin
Aescleal1-Feb-11 7:58
Aescleal1-Feb-11 7:58 
AnswerRe: Release VS Debug compiles Pin
User 74293381-Feb-11 10:02
professionalUser 74293381-Feb-11 10:02 
GeneralRe: Release VS Debug compiles Pin
Khan Shere1-Feb-11 20:57
Khan Shere1-Feb-11 20:57 
Questionuse the member pointer via a Get function and allocate memory outside, not clear why in this way Pin
George Nistor1-Feb-11 1:36
George Nistor1-Feb-11 1:36 
AnswerRe: use the member pointer via a Get function and allocate memory outside, not clear why in this way Pin
Niklas L1-Feb-11 1:49
Niklas L1-Feb-11 1:49 
AnswerRe: use the member pointer via a Get function and allocate memory outside, not clear why in this way Pin
Andrew Brock1-Feb-11 2:17
Andrew Brock1-Feb-11 2:17 
Firstly, I agree with Niklas, this is terrible style and structure. A class should be responsible for allocating and deallocating all its memory.

To answer your question, returning a pointer allows you to modify the data that is pointed to (in your case this is NULL, so it would result in an access violation).
Returning a reference allows you to modify the value of the original variable returned (think of reference as "another name for the variable X")
So, in order to modify the value of the variable pt rather than the data it points to, you can either return a reference to the variable, or a pointer to the variable itself.
You are returning the reference, to return a pointer you would use it like:
//class CTest
CInner** mGetPt(void) {
	return &pt;
}

//_tmain()
*obj->mGetPt() = new CInner();


Having said that, this is still bad style, and you would be much better off with something like:
class CInner {
	public:
		CInner() : mx(1) { } //This is an initialiser list, and is equivalent to "mx = 1". This is quicker and cleaner than using "mx = 1"
		int GetMX() { return mx; }
		void SetMX(int nNewMx) { mx = nNewMx; } //This allows you to validate the value getting set, and is good style even if you don't need to validate it

	private:
		int mx; //It is good style to have variables as private, or protected, then have a get/set function which can validate the value
};


For CTest you have 2 choices, you can either allocate pt = new CInner in the constructor or when it is needed.
//Preallocate solution
class CTest {
	public:
		CTest() : pt(new CInner) { } //Allocate in the constructor
		~CTest() {
			if (pt != NULL) {
				delete pt; //Don't forget to delete the memory
			}
		}
		CInner *mGetPt() { return pt; } //Don't return by reference, it is bad style

	private:
		CInner *pt;
};

//Allocate on demand solution
class CTest {
	public:
		CTest() : pt(NULL) { } //Set to NULL
		~CTest() {
			if (pt != NULL) {
				delete pt; //Don't forget to delete the memory
			}
		}
		CInner *mGetPt() { //Don't return by reference, it is bad style
			if (pt == NULL) {
				pt = new CInner;
			}
			return pt;
		}

	private:
		CInner *pt;
};


You can then use it like:
int _tmain(int argc, TCHAR *argv[]) {
	CTest *obj = new CTest();
	cout << obj->mGetPt()->GetMX();
	cin.get();
	return 0;
}

Questionerror C2011: 'CMemDC' : 'class' type redefinition Pin
VCProgrammer31-Jan-11 21:25
VCProgrammer31-Jan-11 21:25 
AnswerRe: error C2011: 'CMemDC' : 'class' type redefinition Pin
Cool_Dev31-Jan-11 21:38
Cool_Dev31-Jan-11 21:38 
GeneralRe: error C2011: 'CMemDC' : 'class' type redefinition Pin
VCProgrammer31-Jan-11 21:41
VCProgrammer31-Jan-11 21:41 
GeneralRe: error C2011: 'CMemDC' : 'class' type redefinition Pin
PJ Arends1-Feb-11 18:14
professionalPJ Arends1-Feb-11 18:14 
GeneralRe: error C2011: 'CMemDC' : 'class' type redefinition Pin
Malli_S1-Feb-11 20:40
Malli_S1-Feb-11 20:40 
QuestionPreprocessor Directive Pin
pix_programmer31-Jan-11 17:34
pix_programmer31-Jan-11 17:34 
AnswerRe: Preprocessor Directive PinPopular
Andrew Brock31-Jan-11 17:48
Andrew Brock31-Jan-11 17:48 
GeneralRe: Preprocessor Directive Pin
Niklas L31-Jan-11 21:15
Niklas L31-Jan-11 21:15 
GeneralRe: Preprocessor Directive [modified] Pin
Stefan_Lang31-Jan-11 23:25
Stefan_Lang31-Jan-11 23:25 
GeneralRe: Preprocessor Directive Pin
Andrew Brock31-Jan-11 23:46
Andrew Brock31-Jan-11 23:46 
GeneralRe: Preprocessor Directive Pin
Stefan_Lang31-Jan-11 23:55
Stefan_Lang31-Jan-11 23:55 
GeneralRe: Preprocessor Directive Pin
Andrew Brock31-Jan-11 23:57
Andrew Brock31-Jan-11 23:57 
GeneralRe: Preprocessor Directive Pin
Niklas L1-Feb-11 1:50
Niklas L1-Feb-11 1:50 
GeneralRe: Preprocessor Directive Pin
Niklas L1-Feb-11 1:41
Niklas L1-Feb-11 1:41 
GeneralRe: Preprocessor Directive Pin
Stefan_Lang1-Feb-11 0:11
Stefan_Lang1-Feb-11 0:11 
AnswerRe: Preprocessor Directive Pin
Malli_S31-Jan-11 21:10
Malli_S31-Jan-11 21:10 
QuestionMFC - Printing - Changing page orientation from a custom pagesetup dialog [SOLVED] Pin
Un Suthee31-Jan-11 15:11
Un Suthee31-Jan-11 15:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.