Solved

Pointer Problem

Posted on 2006-06-09
6
241 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Turn a spreadsheet into a vba executable. 2 66
deburging in oracle form 12 77
move a line in eclipse 3 60
Fibonacci challenge 11 84
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now