Initialisierung einer const-Referenz
Verfasst: Fr Apr 26, 2013 10:46 pm
Hi, ich habe ein Problem beim Initialisieren einer Referenz aus einer Basisklasse durch eine abgeleitete Klasse:
GCC 4.6.4 meldet mir:
LG Glocke
Code: Alles auswählen
#include <iostream>
class Typ {};
class Base {
protected:
Typ& ref;
public:
Base(Typ const & ref)
: ref(ref) {}
};
class Derived: public Base {
public:
Derived(Typ const & ref)
: Base(ref) {}
};
int main() {
Typ t;
Derived d(t);
}
Ohne const (in Typ const &) funktioniert es. Allerdings weiß ich nich wie ich es zum Laufen bekomme, wenn ich explizit eine Referenz auf eine Konstante haben möchte.main.cpp: In Konstruktor »Base::Base(const Typ&)«:
main.cpp:10:22: Fehler: ungültige Initialisierung einer Referenz des Typs »Typ&« von Ausdruck des Typs »const Typ«
LG Glocke
