Ich habe den folgenden Code geschrieben, um mein Problem zu erklären. Wenn ich die Zeile 11 (mit dem Schlüsselwort "using") kommentiere, kompiliert der Compiler die Datei nicht und zeigt diesen Fehler an: invalid conversion from 'char' to 'const char*'
. Es scheint die Methode void action(char)
der Parent
Klasse in der Son
Klasse nicht zu sehen.Warum sollte ich das Schlüsselwort "using" verwenden, um auf meine Basisklassenmethode zuzugreifen?
Warum verhält sich der Compiler so? Oder habe ich etwas falsch gemacht?
class Parent
{
public:
virtual void action(const char how){ this->action(&how); }
virtual void action(const char * how) = 0;
};
class Son : public Parent
{
public:
using Parent::action; // Why should i write this line?
void action(const char * how){ printf("Action: %c\n", *how); }
};
int main(int argc, char** argv)
{
Son s = Son();
s.action('a');
return 0;
}
Bitte sagen Sie mir: Was ist, wenn Sie const in "const char how" entfernen? –
Sie müssen nicht "Son s = Son();" eingeben. Das erstellt nur ein temporäres und ruft dann den Kopierkonstruktor auf. Geben Sie einfach 'Son s;' –
Wir bekommen diese Frage eine Menge: [http://stackoverflow.com/questions/1835988](http://stackoverflow.com/questions/1835988) [http://stackoverflow.com/ Fragen/411103] (http://stackoverflow.com/questions/411103) [http://stackoverflow.com/questions/1480085](http://stackoverflow.com/questions/1480085) [http: // stackoverflow. com/questions/1799497] (http://stackoverflow.com/questions/1799497) [http://stackoverflow.com/questions/888235](http://stackoverflow.com/questions/888235) [http: // stackoverflow.com/questions/72010](http://stackoverflow.com/questions/72010) –