c++ lists with fields

I don't know if it's possible but i need a list with fields like i.e.: first name, last name, age.

Please, help me,
(blu)
thank you very much for fast reply
Last edited on
is there a way to return the address of a class element ?

1.John Tomas 39
2.Mike Hunt 34
I want the address of 1.
You want the address of a class object of of a class member?
---first name---last name---age---addr_next+---first name---last name---age---addr_next+...
I need addr_next.

LE.: yes, the object address.
Last edited on
As with any other variable:
1
2
YourClass your_object;
&your_object // == address of your_object 


- Notice that you can use a list/deque/vector/whatever_container_you_like of class objects so you can use iterators
Can you give me a example of a list of class objects? Thank you very much!
Using classes is the same as other types
eg:
1
2
3
4
5
6
7
8
9
class Foo
{
     public:
         int bar; // member
         Foo ( int bar ) : bar ( bar ) {} // constructor, initializes 'bar'
};

list <Foo> foolist ( 4, Foo(5) ); // foolist will contain 4 Foo objects with bar == 5
foolist.push_back( Foo(6) ); // adds a Foo object with bar == 6  
Last edited on
Thank you! You're the best!
I've tried something like this, but i can't understand how to push_back into a list, as i get some compilation errors.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<iostream>
#include<list>
using namespace std;

class date {
    string n; //nume
    float m; //media
  public:
    void set (string nume, float media) {
      n = nume;
      m = media;
    }
    void nume () {cout << n << '\xA';}
    void media () {cout << m << '\xA';}
};

int main () {
  list <date> s; list <date>::iterator it;
  s.push_back(set ("Bob", 8.3));
  it.begin().nume(); it.begin().media();
  return 0;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class date {
    string n; //nume
    float m; //media
  public:
    date (string nume, float media) { // constructor - member function with the same name of the class and no return type -
      n = nume;
      m = media;
    }
    void nume () {cout << n << '\xA';}
    void media () {cout << m << '\xA';}
};

int main () {
  list <date> s; 
  s.push_back(date ("Bob", 8.3)); // call constructor
  s.begin()->nume(); // s.begin() is an iterator
  s.begin()->media();
  return 0;
}
uau. Nice one! Thanks!

A question: Can I push_back without constructor?

I made it with:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class date {
    string n; //nume
    float m; //media
  public:
    void set_values (string nume, float media) {
      n = nume;
      m = media;
    }
};

int main () {
  list <date> s;
  date cache;
  cache.set_values("Bob", 8.3);
  s.push_back(cache);
  return 0;

But, can i do it without declaring cache?
Last edited on
Or you use a variable, or you use a constructor. There are no other ways.
- Using the constructor makes you creating a temporary object of date -
Why don't you like the constructor?

If you change 'set_values' you can do this:
1
2
3
4
5
6
7
date &set_values (string nume, float media) {
      n = nume;
      m = media;
      return *this;
    }
//...
s.push_back( date().set_values("Bob", 8.3) ); // notice that 'date()' is the default constructor 
Last edited on
I just wanted to know other ways. Thanks! I'll use the constructor.
Last edited on
Topic archived. No new replies allowed.