Solved

Pointer Problem

Posted on 2006-06-09
6
245 Views
Last Modified: 2013-11-20
Could anyone tell me why the following code does not work?


#include "stdafx.h"

class TestClass : public CObject
{
private:
      CString fname;
      CString lname;

public:
      TestClass(LPCTSTR first, LPCTSTR last);
      CString getFirstName();
      CString getLastName();
};


TestClass::TestClass(LPCTSTR first, LPCTSTR last)
{
fname=first;
lname=last;
}

CString TestClass::getFirstName()
{
return fname;
}

CString TestClass::getLastName()
{
return lname;
}

TestClass* createObject()
{
TestClass t("ABC", "DEF");

return &t;
}



int _tmain(int argc, _TCHAR* argv[])
{
TestClass *tth=NULL;
tth=createObject();//Gives Error
CString test_cstr=tth->getLastName();      

      return 0;
}

0
Comment
Question by:csound
  • 3
6 Comments
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 50 total points
ID: 16868702
TestClass* createObject()
{
TestClass t("ABC", "DEF");

return &t;
}

t goes out of scope and is destroyed.


TestClass* createObject()
{
TestClass* t = new TestClass("ABC", "DEF");

return t;
}

don't forget you need to use delete on the returned pointer when no longer required.
0
 
LVL 7

Assisted Solution

by:nafis_devlpr
nafis_devlpr earned 20 total points
ID: 17080219
I agree with Andy. But you can use auto_ptr rather than using new and delete. auto_ptr will be automatically deleted when it is not in use anymore. For using auto_ptr you have to add the two lines at the top of your source file

#include <memory>  
using std::auto_ptr; // auto_ptr class definition

TestClass* createObject()
{
     auto_ptr< TestClass > t( new TestClass("ABC", "DEF") );
     return t;
}

Nafis
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 17080433
To be honest I am not a fan of the garbage collection or mechanisms like that.
If I have a memory leak then it means I have made a mistake in the logic/coding.  Anything that automatically cleans up will hide coding mistakes like that from the programmer.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 17294135
My comment pointer out what was going wrong and how to correct it.
0
 

Author Comment

by:csound
ID: 17295371
Thank you for your help!=)
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
sumNumber challenge 16 140
sumHeights2  challenge 7 130
My project did see openJDK that I installed. What could be the problem 7 161
wordmultiple challenge 12 156
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question