const Zeiger als Wert in einer Map abspeichern



  • Hi,

    #include <map>
    
    int main()
    {
    	long l;
    	const long* pL = &l; // nehme ich das const weg, lässt es sich compilieren
    
    	std::map<int, long*> testMap;
    	testMap.insert(std::map<int, long*>::value_type(5, pL));
    }
    

    Der Code kompiliert wenn ich das const wegnehme, aber ich würde gerne verstehen warum es nicht mit const geht. Kann mir das jemand erklären? 🙂 🙂



  • du liegst da einem großen irtum auf:
    du denkst, dass

    const long* pL
    

    ein const zeiger auf long ist.
    es ist aber eigentlich ein zeiger auf einen const long.
    was du meinst, wird so geschrieben:

    long* const pL
    

Anmelden zum Antworten