Tom
Tom

Reputation: 11

handing pointers in a class

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;


class newclass
{
public:
    string *Date;
    void show(){ cout << this->Date;}

};

int main()
{
    newclass *n = new newclass;
    n->Date = new string ;
    n->Date.assign("hello");   // get an error on this line
    n->show();
    return 0;
}

Can Somebody please explain me how this works?. I am new to this.

Upvotes: 1

Views: 133

Answers (5)

Chad
Chad

Reputation: 19052

You've got a few problems here, let's go over them one at a time:

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;


class newclass
{
public:
   // This is a pointer to a std::string, that doesn't appear to 
   // be allocated.  From this small example, you don't really need
   // a pointer at all:
   // string *Date;

   // now that this object isn't a pointer, you can use dot syntax
   // to access its member functions
   string Date;

   void show()
   {
      // accessing date through this-> is not necessary 
      // here.  You can simply use Date.  However since this
      // doesn't cause any specific problems I mention it only 
      // as informational
      cout << this->Date;
   }

};

int main()
{
    newclass *n = new newclass;

    // This is bad practice, you generally shouldn't be allocating
    // objects within a class outside of that class's implementation.
    // this would better be done in the newclass constructor.
    //  n->Date = new string ;

    // Since we replaced the pointer to string object Date in newclass
    // and replaced it with an automatic string object, this line will
    // now work as written. 
    n->Date.assign("hello");   // get an error on this line
    n->show();

    // At this point your program will exit, and newclass is shown as
    // a memory leak.  While the OS will reclaim this memory at application
    // exit, its good to get used to managing the lifetime of your objects
    // appropriate.  This has do be deleted, or better yet wrapped in a smart 
    // pointer
    delete n;

    return 0;
}

To provide an example that is similar to your original question, let's take the following code:

#include "stdafx.h"
#include <iostream>
#include <string>
#include <memory>
using namespace std;


class newclass
{
public:
   // auto_ptr is a smart pointer class that 
   // wraps a dynamically allocated object and
   // provides cleanup for it when it goes out
   // of scope.  So when our class goes out of 
   // scope the Date object will be cleaned up
   // for us
   auto_ptr<string> Date;

   // this is the constructor of the newclass object
   // its called anytime a newclass object is instantiated
   // we use the initializer list to allocate the Date object
   newclass() : Date(new string)
   {
   }

   // a mutator for setting the Date object (promotes encapsulation)
   void set_date(const std::string& val)
   {
      // necessary to dereference to get at the date object
      (*Date) = val;
   }

   void show() {  cout << this->Date; }
};

int main()
{
    // note the use of an auto_ptr here for automatic cleanup
    auto_ptr<newclass> n(new newclass);

    // use our mutator method to set the date
    n->set_date("hello");
    n->show();

    return 0;
}

Upvotes: 2

Mark Ransom
Mark Ransom

Reputation: 308530

It looks like you're applying idioms from another language. In C++ you should avoid pointers whenever possible, replaced by member or local variables. Your code in that case would look like this:

class newclass
{
public:
    string Date;
    void show(){ cout << this->Date;}
};

int main() {
    newclass n;
    n.Date.assign("hello");
    n.show();
    return 0;
} 

Upvotes: 2

Bob Fincheimer
Bob Fincheimer

Reputation: 18076

n->Date->assign("hello") because n->Date is a pointer, so you must dereference it before calling a function on it.

Please look at this SO Question for a detailed explanation of the syntax.

Upvotes: 7

TeaWolf
TeaWolf

Reputation: 714

Date is itself a pointer to string, you will need to dereference it as well:

n->Date->assign("hello");

Upvotes: 1

Andr&#233; Puel
Andr&#233; Puel

Reputation: 9219

    newclass *n = new newclass;
    n->Date = new string ;
    n->Date->assign("hello");   // use -> when accessing a member of a pointer
    n->show();

Upvotes: 0

Related Questions