Thread: Vectors & objects/pointers

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    101

    Question Vectors & objects/pointers

    Hello!

    I'm learning my way through std vectores and c++ and can't understand one thing: how do I write code that uses the automatic way of handling memory (constructores & destructores)?

    What I want to do (inside a function that returns a pointer to the created object):
    - Create one Item with the atribute name set to "ball"
    - Put it inside a global vector
    - return the pointer to the created Item.

    Code:
    Item* makeItem(){
      std::vector<Item> bag;
      bag.push_back(*(new Item()));
      bag.back().name = "ball";
      return &(bag.back());
    }
    This sample code is the best idea of how to use I came up with. I'm pretty sure I'm missing something.
    Is this really the way handle the references vs objects?

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Are you referring to cleaning up items allocated with new automatically? If so, you need smart pointers.
    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.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by kotoko View Post
    Code:
    Item* makeItem(){
      std::vector<Item> bag;
      bag.push_back(*(new Item()));
      bag.back().name = "ball";
      return &(bag.back());
    }
    Don't do it like that. If you want to hold onto the address of the item inserted then you're most likely going to need to use a list, not a vector, and certainly not one that is a local variable. You don't use the new keyword to push_back an item. Assuming an appropriate constructor that takes a string, here you'd just do:
    Code:
    std::list<Item> bag;
    
    Item* makeItem() {
        bag.push_back(Item("ball"));
        return &bag.back();
    }
    But sreiously, I strongly suspect that you're only creating problems for yourself by taking pointers to the items you insert.
    One needs to look at the bigger picture. How does this fit into the rest of the program? What will it be used for?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with vectors of objects!
    By bobthebullet990 in forum C++ Programming
    Replies: 3
    Last Post: 08-11-2006, 01:16 PM
  2. Creating vectors of objects!
    By bobthebullet990 in forum C++ Programming
    Replies: 17
    Last Post: 08-08-2006, 04:51 PM
  3. vectors and objects
    By barneygumble742 in forum C++ Programming
    Replies: 3
    Last Post: 07-04-2005, 08:53 AM
  4. Replies: 4
    Last Post: 10-16-2003, 11:26 AM
  5. Vectors of pointers to objects
    By Myownworstenemy in forum C++ Programming
    Replies: 3
    Last Post: 09-01-2003, 11:23 PM

Tags for this Thread