Pointer array,

Hello everybody.

First of all, thanks to everybody who will be kind to help me?
I have created a class personne, with a variable (num_pers) which increments each time an object is created.
I would like to store my objects in a dynamic table by using a pointer with an index. The index is my num_pers variable.
When I create a dynamic object in the main the storage works but I would like to create my dynamic object in the constructor and there it is not working… and I don’t understand why.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
 #include <mere.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <iostream>
#include <vector>
#include <string>
using namespace std;

class personne
{
public:
	static int num_pers;		// Counter for the new created objects
	const personne *P;					// Pointer to my objects array
	char* nom;
	char* prenom;
	int age;

public:
	personne();				
	personne(char* , char* , int);
	~personne();
	void affiche();
};

int personne::num_pers=0;		// initialize the counter

personne::personne()		// initialize an empty object
{
	nom="Inconnu";
	prenom="Inconnu";
	age=0;
}

personne::personne(char* n, char* p, int a)	// initialize an object with the three parameters
{
	cout<<"Creation d'un objet !!!\n Numero de la personne: "<<num_pers<<endl;
	
	nom=n;
	prenom=p;
	age=a;

	(P[num_pers+1])=new personne;	 // creation of an new object with P pointing to, P index will also incremented each time a new object is created.
	
	P[num_pers]->nom=n;	
	P[num_pers]->prenom=p;
	P[num_pers]->age=a;
	
	num_pers++;				// index incrementation
}

personne::~personne()
{
	cout<<"Destruction des objets !!!\n";
	delete[] P;		
}

void personne::affiche()
{
	cout<<"Nom: "<<nom<<endl;
	cout<<"Prenom: "<<prenom<<endl;
	cout<<"Age: "<<age<<endl<<endl;
}

int main()
{	
	personne A("Dupont ","Marcel ",40), B("Duchemin ","Fred ",35), C("","Siegfried ",17), D;

	A.affiche();
	B.affiche();
	C.affiche();
	D.affiche();

	cout<<"Creation d'une personne dynamique !!!\n\n";
	personne *P=new personne;
	P->nom="Loulou";		
	P->prenom="Chouchou";
	P->age=12;

	cout<<"Nom: "<<P->nom;
	cout<<", Prenom: "<<P->prenom;
	cout<<", Age: "<<P->age<<endl<<endl;
	
	_getch();
	return 0;
}
const personne *P;

This is a constant field and therefore must be initialized using the initializer list in the constructor. For example:

1
2
3
personne::personne(char* n, char* p, int a) : P(new personne[20]) {
   // do stuff
}


The question is, why is this array not a static field? Why must every person have their own list? If that's not the case, and the pointer points at a pre-made array, that brings us back to: why is it not a static field?
It doesn't work because every personne will have their own copy of const personne *P;

You could make it "work" by marking P as static (as you did for the num_pers counter) but that's a bad idea -- it's a bad idea because all personne objects will be able to access it, so then how do you decide which one delete[]'s it?

My advice, keep personne simple and unaware of how they're stored.
In other words: keep personne *P; external to the personne class.

Also you are overusing pointers.

15
16
	char* nom;
	char* prenom;


This is bad. You must understand that pointers hold a memory address, not a copy of the information itself.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

char * bad_bad()
{
    char s[] = "Will this work?";
    return s;
} // <--- s array goes out of scope here

// the pointer returned by bad_bad() is always invalid!

int main()
{
    std::cout << bad_bad() << std::endl;
}


So if you get the person name from a function, the personne::nom will become invalid.

This all only works now because you use string literals such as "Loulou". String literals are built-in into the program and are always valid while the program runs.

What's more, all copies will be shallow, and not deep.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

struct Person
{
    char *name;
};

int main()
{
    char tmp[] = "Kyle";
    Person a, b;

    a.name = tmp;
    b = a;
    tmp[0] = 'L'; // how much does this affect?
    std::cout << a.name << ' ' << b.name;
}
Lyle Lyle


So instead of char pointers, you should go the C++ style and use std::string.

1
2
3
4
5
6
#include <string>

// ...

std::string nom;
std::string prenom;

Topic archived. No new replies allowed.