 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Tobias Wollgam Guest
|
Posted: Thu Apr 08, 2004 10:10 am Post subject: non-const aus const aufrufen |
|
|
Hallo.
Gibt es eine Möglichkeit eine non-const Funktion aus einer const Funktion
aufzurufen?
Die Signatur der aufzurufenden Funktion kann ich nicht ändern, das Interface
meiner Klasse soll aber korrekt sein.
TIA,
Tobias
--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
|
|
| Back to top |
|
 |
Torsten Robitzki Guest
|
Posted: Fri Apr 09, 2004 9:02 pm Post subject: Re: non-const aus const aufrufen |
|
|
Hallo Tobias,
Tobias Wollgam wrote:
| Quote: | Hallo.
Gibt es eine Möglichkeit eine non-const Funktion aus einer const Funktion
aufzurufen?
Die Signatur der aufzurufenden Funktion kann ich nicht ändern, das Interface
meiner Klasse soll aber korrekt sein.
|
woher hast Du die Referenz auf das Objekt? Ist sie ein Datum Deiner
Klasse? Wenn ja, kannst Du das Datum mit 'mutable' qualifizieren und
damit ausdrücken, das eine Änderung an diesem Datum nichts an dem Status
des Objektes verändert. Das wird z.B. beutzt, wenn man eine mutex
benötigt um Thread-Synchronisierung zu erreichen, wobei natürlich auch
nur lesende Programmpfade mit synchronisiert werden sollen oder man hat
einen std::ostream zum loggen und möchte auch nur lesende Anfragen
protokollieren.
Möchtest Du eine Funktion einer Basisklasse rufen und diese ist nicht
korrekt const qualifiziert, must Du den const qualifier mit const_cast<>
weg casten. Wie mit allen casts sollte man damit sehr vorsichtig umgehen:
mfg Torsten
--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
|
|
| Back to top |
|
 |
Thomas Maeder Guest
|
Posted: Fri Apr 09, 2004 9:15 pm Post subject: Re: non-const aus const aufrufen |
|
|
Tobias Wollgam <tobias.wollgam (AT) materna (DOT) de> writes:
| Quote: | Gibt es eine Möglichkeit eine non-const Funktion aus einer const Funktion
aufzurufen?
|
Wenn Du weisst, dass das Objekt, auf das Du die Funktion anwendest, nicht
const ist, kannst Du const_cast verwenden.
--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
|
|
| Back to top |
|
 |
Arne Adams Guest
|
Posted: Sat Apr 10, 2004 5:12 am Post subject: Re: non-const aus const aufrufen |
|
|
Hallo Tobias,
"Tobias Wollgam" <tobias.wollgam (AT) materna (DOT) de> schrieb im Newsbeitrag
news:c538fi$7vt$1 (AT) pentheus (DOT) materna.de...
| Quote: | Gibt es eine Möglichkeit eine non-const Funktion aus einer const Funktion
aufzurufen?
|
ggf. genügt es auch, wenn Deine const Funktion in Wirklichkeit den Zustand
der Klasse verändern kann:
class B
{
const std::vector<int>& getResults() const;
private:
// hier ist caching nicht Teil des vom Verwender detektierbaren
Konzepts,
// deshalb mutable.
mutable std::vector<int> cachedResults_;
};
const std::vector<int>& B::getResults() const
{
if(cachedResults_.empty()) // oder wenn der Cache vermutlich ungültig
ist
{
for(int i = 0; i < 10; ++i)
cachedResults_.push_back(i * (i + 1));
}
return cachedResults_;
}
HTH
Arne
--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
|
|
| Back to top |
|
 |
Tobias Wollgam Guest
|
Posted: Tue Apr 13, 2004 8:52 am Post subject: Re: non-const aus const aufrufen |
|
|
Torsten Robitzki wrote:
| Quote: | Hallo Tobias,
Tobias Wollgam wrote:
Hallo.
Gibt es eine Möglichkeit eine non-const Funktion aus einer const Funktion
aufzurufen?
Die Signatur der aufzurufenden Funktion kann ich nicht ändern, das
Interface meiner Klasse soll aber korrekt sein.
|
[mutable] (ist mir bekannt)
| Quote: | Möchtest Du eine Funktion einer Basisklasse rufen und diese ist nicht
korrekt const qualifiziert, must Du den const qualifier mit const_cast
weg casten. Wie mit allen casts sollte man damit sehr vorsichtig umgehen:
|
So ganz ist mir das noch nicht klar, daher folgendes kleines Beispiel:
class A
{
public:
A();
void doSomething();
};
class B : public A
{
public:
B();
void doSomethingToo() const
{
doSomething();
}
};
A kann ich wie gesagt nicht ändern/korrigieren, aber B ist meine Klasse und
soll ein korrektes Interface haben.
Wäre die Lösung dann:
void doSomethingToo() const
{
B* b = const_cast<B*>(this);
b->doSomething();
}
TIA und Gruß,
Tobias
--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
|
|
| Back to top |
|
 |
Rolf Magnus Guest
|
Posted: Wed Apr 14, 2004 12:55 pm Post subject: Re: non-const aus const aufrufen |
|
|
Tobias Wollgam wrote:
| Quote: | Wäre die Lösung dann:
void doSomethingToo() const
{
B* b = const_cast<B*>(this);
b->doSomething();
}
|
Ja.
--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
|
|
| Back to top |
|
 |
Michael Karcher Guest
|
Posted: Wed Apr 14, 2004 5:34 pm Post subject: Re: non-const aus const aufrufen |
|
|
Rolf Magnus <ramagnus (AT) t-online (DOT) de> wrote:
| Quote: | Wäre die Lösung dann:
void doSomethingToo() const
{
B* b = const_cast<B*>(this);
b->doSomething();
}
Ja.
|
Oder ohne Temporärzeiger:
void doSomethingToo() const
{
const_cast<B*>(this)->doSomething();
}
Michael Karcher
--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|