Thread: objects and pointers

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    2

    objects and pointers

    hi,

    First off I'm very extremely new to C++, like...a few days or so, so I do have a lot of questions. But, here's where I'll start!

    I am wondering if it is possible to have a pointer to an object? Similar to what you can do with structures. So that, when a new instance of the object is created, you can allocate the memory for it? For example, could you do this type of thing, assuming the class is already created in your code:

    Code:
    int main()
    {
    classname *ptr;
    
    ptr = new classname; // to provide sufficient memory
    
    ptr->function(param1, param2);  /* to start of whatever function in the class */
    
    delete ptr;
    
    return 0;
    }
    Would that be possible? I tried and when it returned one of the integers in my class, it was the wrong value. That is, instead of getting 50 back, I got: 3999000. Ugh...not 51, or 52, but 3999000, . but some other data was returned correctly, interestingly. I'm afraid to experiment too much because I'm afraid to write to something I shouldn't, accidently.

    So any help on this matter would be appreciated.

    Brandon

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I am wondering if it is possible to have a pointer to an object?
    It sure is, though most of the time you don't need such a low level thing (technical term). When you must have pointers, use a safer smart pointer such as std::auto_ptr or one of the boost library's smart pointer types. Here is an example of both raw pointers and std::auto_ptr:
    Code:
    #include <iostream>
    #include <memory>
    
    using namespace std;
    
    class test {
    public:
      test ( int init = 0 ) throw()
        : test_val ( init )
      {}
      int gimme() throw() { return test_val; }
    private:
      int test_val;
    };
    
    int main()
    {
      test *ptest = new test ( 50 );
      auto_ptr<test> aptest ( new test ( 50 ) );
    
      cout<< ptest->gimme() <<endl;
      cout<< aptest->gimme() <<endl;
      delete ptest;
      // No need to delete aptest, auto_ptr does that
    }
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Dec 2003
    Posts
    2
    hi,

    Oh thanks. I'll have to look into that auto-ptr more. I think I understand the syntax though.

    an extra edit: I figured out why my one integer was at such a high number. It was fixed when I first set it to 0. I guess it was some other value at first, and when the user enters the input for that integer, it goes up one number at a time through a while loop. so just had to set that to 0 for that to be fixed . thanks again, I successfully put the auto_ptr in my program.

    Brandon

    Originally posted by Prelude
    >I am wondering if it is possible to have a pointer to an object?
    It sure is, though most of the time you don't need such a low level thing (technical term). When you must have pointers, use a safer smart pointer such as std::auto_ptr or one of the boost library's smart pointer types. Here is an example of both raw pointers and std::auto_ptr:
    Code:
    #include <iostream>
    #include <memory>
    
    using namespace std;
    
    class test {
    public:
      test ( int init = 0 ) throw()
        : test_val ( init )
      {}
      int gimme() throw() { return test_val; }
    private:
      int test_val;
    };
    
    int main()
    {
      test *ptest = new test ( 50 );
      auto_ptr<test> aptest ( new test ( 50 ) );
    
      cout<< ptest->gimme() <<endl;
      cout<< aptest->gimme() <<endl;
      delete ptest;
      // No need to delete aptest, auto_ptr does that
    }
    Last edited by SiteResources; 12-02-2003 at 08:40 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Returning an Array of Pointers to Objects
    By randomalias in forum C++ Programming
    Replies: 4
    Last Post: 04-29-2006, 02:45 PM
  2. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  3. Replies: 4
    Last Post: 10-16-2003, 11:26 AM
  4. Vectors of pointers to objects
    By Myownworstenemy in forum C++ Programming
    Replies: 3
    Last Post: 09-01-2003, 11:23 PM
  5. Objects, or pointers to objects?
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 12-18-2001, 12:57 AM