-1

I have a class

class Foo
{
    public:
    char* ptr;
    Foo()  { ptr=new char [10]; }
    ~Foo()  { delete [] ptr;  }
};

I have learnt that returning an object of this class is not possible as the dynamically allocated pointer is delete 'ed and creates a dangling pointer in the calling function

So how can I return An object of this class??

Foo Bar ()
{
    Foo obj;
    return obj;
}

Can it be solved by adding a copy constructor

Foo::Foo(const Foo& obj)
{
    ptr = new char [10];
    for( int i = 0 ; i < 10 ;++i )
        ptr [i] = obj.ptr[i];
} 

And the function as

Foo Bar ()
{
    Foo obj;
    return Foo(obj);     //invokes copy constructor
}

Note These are just representation of the actual class I want and is not created according to recommended standards (ie. Please don't tell me to use std::string or std::vector).

WARhead
  • 643
  • 5
  • 17
  • @PaulMcKenzie It doesnt explain what to do to overcome it. – WARhead Dec 10 '16 at 05:27
  • 1
    The link shows you that you need a copy constructor, assignment operator, and destructor. – PaulMcKenzie Dec 10 '16 at 05:28
  • 1
    @WARhead, have you looked the section **Managing Resources** in the first answer? – R Sahu Dec 10 '16 at 05:29
  • @RSahu Yes It still doesnt explain what to do. Unless it is just me not understanding – WARhead Dec 10 '16 at 05:32
  • 1
    @WARhead The link has an example that is practically the same as your code, with a full explanation of the situation and what to do. Are you not reading the **Managing Resources** section? – PaulMcKenzie Dec 10 '16 at 05:32
  • @PaulMcKenzie and yes I know the rule of three, i was just putting up an example. And I asked if a copy constructor would solve the problem. – WARhead Dec 10 '16 at 05:34
  • 1
    @WARhead -- If you know the rule of three, why are you asking if it will solve the problem? The rule of 3 is explicitly required to solve your problem. – PaulMcKenzie Dec 10 '16 at 05:35
  • @PaulMcKenzie Yes I read it it just points out the unpleasant parts of raw pointers and why to avoid them only. – WARhead Dec 10 '16 at 05:35
  • 2
    @WARhead -- Are you reading the link that I pointed out? You see exactly how to write a copy constructor and assignment operator. Are you trolling? – PaulMcKenzie Dec 10 '16 at 05:36
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/130269/discussion-between-warhead-and-paulmckenzie). – WARhead Dec 10 '16 at 05:43

1 Answers1

1

So how can I return An object of this class??

You need to implement the copy constructor and the copy assignment operator that manage memory properly.

// Copy constructor
Foo(Foo const& copy) : ptr(new char[strlen(copy.ptr)+1])
{
  strcpy(ptr, copy.ptr);
}

// Copy assignment operator
Foo& operator=(Foo const& rhs)
{
   // Don't do anything for self assignment
   if ( this != &rhs )
   {
      delete [] ptr;
      ptr = new char[strlen(rhs.ptr)+1]);
      strcpy(ptr, rhs.ptr);
   }
   return *this;
}

If ptr is always going to be an array of 10 chars, you'll need to rethink the copy constructor and copy assignment.

// Copy constructor
Foo(Foo const& copy) : ptr(new char[10])
{
  memcpy(ptr, copy.ptr, 10);
}

// Copy assignment operator
Foo& operator=(Foo const& rhs)
{
   // Don't do anything for self assignment
   if ( this != &rhs )
   {
      memcpy(ptr, rhs.ptr, 10);
   }
   return *this;
}
R Sahu
  • 204,454
  • 14
  • 159
  • 270