0

What I want is a function that makes a new different instance every time i call it (with a different name...)

Just like this:

void person::new_person(){
  person *(id+index) = new person(name_temp, age_temp, quote_temp);
}

But it doesn't work... I don't know how should i do that... (index is add by one every time i make a new instance). And i realized every time i make a pointer and just add spaces on it, like that:

int*p;
*(p+1) = 5;

It compiles, but freezes while running(I supose its getting memmory thats not allowed), so that "person *(id+index)" may not work too. What do you think?

  • 1
    Not clear what you're trying to do. You can't have dynamically generated variable names. You can create new objects on every function call but you need a dedicated place to store them, whether in a specific variable or array or vector or some other collection... – Joe Oct 30 '12 at 20:45
  • 12
    In the short term, you need a container, ideally something like `std::vector`. In the long term, you need a [good book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Etienne de Martel Oct 30 '12 at 20:46

3 Answers3

8

Try keeping your persons in a standard container:

std::vector<person> people;
void person::new_person(std::string name, int age, std::string quote){
      people.push_back(person(name, age, quote));
}
Robᵩ
  • 163,533
  • 20
  • 239
  • 308
  • 1
    Without globals ? Pretty please ? – slaphappy Oct 30 '12 at 20:55
  • Pedagogical question: Should I have make the args `const std::string&`, which will eventually be the right answer, or pass by value to avoid confusing OP? – Robᵩ Oct 30 '12 at 20:55
  • 3
    @Robᵩ you are going to copy `name` anyway, so you should make it a value argument and then do `people.emplace_back(std::move(name), age, std::move(quote));`. –  Oct 30 '12 at 20:56
  • @Zoidberg'--: Which might confuse OP even more, but would also be even more right... – Xeo Oct 30 '12 at 20:56
  • 2
    @kbok - doing that would, I think, make too many confusing assumptions about the OP's program. – Robᵩ Oct 30 '12 at 20:58
  • i didnt put it there, but i have like "cout << "type name"; cin >> name". But if i make 3 new people, how do i have access to the values of the third? – Dhiego Magalhães Oct 30 '12 at 21:04
  • 1
    @Robᵩ Keep it simple, absolutely. One new thing at a time. – john Oct 30 '12 at 21:05
  • "62 C:\Dev-Cpp\MyCompilations\Class Person.cpp `vector' is not a member of `std' " – Dhiego Magalhães Oct 30 '12 at 21:11
5

You mean something like this?

#include <string>

person make_person()
{
    static unsigned i = 0;
    return person(std::to_string(i++));
}
R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510
StackedCrooked
  • 34,653
  • 44
  • 154
  • 278
3

“What I want is a function that makes a new different instance every time i call it (with a different name...) ”

That’s known as a constructor, and it's a special kind of member function, with the same name as the class. It doesn’t have a function result type, not even void. It can go like this:

class Person
{
private:
    string name_;

public:
    Person( string name )
        : name_( name )
    {}
};

Then there are a variety of ways to call it, depending on where you want the new instance stored. E.g.,

int main()
{
    Person a( "A" );    // Local variable
    Person( "B" );      // A temporary, it's already destroyed... ;-)
    vector< Person > v;
    v.emplace_back( "C" );    // As a new item at the end of vector.
}

Plus some, but I guess the three ways above are the most relevant to a complete beginner.

Note that with the vector, every time you push_back or emplace_back you’re creating a new last item, with a new index. Call the size method to find out how many items you currently have in a vector. Check the documentation for more information.

By the way, you should really get yourself a textbook.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
  • Interesting interpretation, but due to `person(name_temp, age_temp, quote_temp)` in the question, I'd assume this is an incorrect interpretation. – Mooing Duck Oct 30 '12 at 21:09
  • 62 C:\Dev-Cpp\MyCompilations\Class Pessoa.cpp `vector' is not a member of `std' – Dhiego Magalhães Oct 30 '12 at 21:14
  • @DhiegoMagalhães: you have to add the relevant `#include`s and `using` statements. A textbook helps. But just to get you going, for `string` you need `#include `, for `vector` you need `#include `, and for both you need to either add `std::` in front of the names, or add `using` statements like `using std::string`. – Cheers and hth. - Alf Oct 30 '12 at 21:15