Bonjour,

Je suis confronté à un petit problème pour l'initialisation d'un membre const avec une fonction.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
#include <map>
#include <string>
#include <iostream>
 
using namespace std ;
 
class Toto
{
	const map<string,string> s_map;
 
protected:
	virtual const map<string,string> init()
	{
		map<string,string> ret ;
		ret["h1"] = "value1" ;
		return ret ;
	}
public:
	Toto() : s_map( init() ) {}
	~Toto() {}
	inline const map<string,string>& getMap() { return s_map; }
};
 
class SuperToto : public Toto
{
protected:
	virtual const map<string,string> init()
	{
		map<string,string> ret = Toto::init();
		ret["h2"] = "value2" ;
		return ret ;
	}
 
public:
	SuperToto() {}
};
 
int main()
{
	SuperToto toto ;
	map<string,string>::const_iterator it = toto.getMap().find("h2");
	if ( it != toto.getMap().end() )
		cout << (*it).second << endl ;
	else
		cout << "Not found" << endl ;
}
Ma question est pourquoi la fonction SuperToto::init() n'est pas appeler automatiquement? Merci d'avance.