Returning a pointer from a class

This is a discussion on Returning a pointer from a class within the C++ Programming forums, part of the General Programming Boards category; Hey everybody, I am trying to do exactly as my instructor has told me to do on a homework assignment ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    2

    Question Returning a pointer from a class

    Hey everybody, I am trying to do exactly as my instructor has told me to do on a homework assignment and it just isn't working...

    HIS Pseudocode:
    Class:- MagicWarrior
    Method:- dropStaff
    tempStaff = staff
    staff = null
    return tempStaff

    BUT, in the other derived class MeleeWarror, the same function needs to return NULL since a MeleeWarrior cannot hold a staff.

    So what I'm doing is attempting to use pointers. I have tried:

    Staff *tmp = new Staff(*pStaff); // Copy the staff he's holding
    delete pStaff; // Delete the staff he's holding
    pStaff = NULL; // Get the pointer ready for next staff
    return tmp; // Return a copy of the staff

    Everything works fine until the very end of the program where it gives me an Unhandled Exception error and crashes...

    Any help would be appreciated
    Last edited by Bleiddynn; 02-24-2011 at 01:29 PM. Reason: Typo

  2. #2
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    682
    You must be sure what happens with your pointer (the one which you return), you have to delete it as well. You need to post complete code and use code tags << !! Posting Code? Read this First !! >>
    I never put signature, but I decided to make an exception.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    2
    Hey thanks for your help. I figured it out =)

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,033
    This might be of some use, especially smart pointers: SourceForge.net: Raw pointer issues - cpwiki
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 12-06-2010, 01:28 PM
  2. Replies: 25
    Last Post: 10-29-2007, 05:08 PM
  3. base class pointer problems
    By ... in forum C++ Programming
    Replies: 3
    Last Post: 11-16-2003, 11:27 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 07:51 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21