Solved

Pointer Problem

Posted on 2006-06-09
6
248 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
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.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

624 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