C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

non-const aus const aufrufen

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ (German)
View previous topic :: View next topic  
Author Message
Tobias Wollgam
Guest





PostPosted: Thu Apr 08, 2004 10:10 am    Post subject: non-const aus const aufrufen Reply with 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.

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





PostPosted: Fri Apr 09, 2004 9:02 pm    Post subject: Re: non-const aus const aufrufen Reply with quote



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





PostPosted: Fri Apr 09, 2004 9:15 pm    Post subject: Re: non-const aus const aufrufen Reply with quote



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





PostPosted: Sat Apr 10, 2004 5:12 am    Post subject: Re: non-const aus const aufrufen Reply with quote

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





PostPosted: Tue Apr 13, 2004 8:52 am    Post subject: Re: non-const aus const aufrufen Reply with quote

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





PostPosted: Wed Apr 14, 2004 12:55 pm    Post subject: Re: non-const aus const aufrufen Reply with quote

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





PostPosted: Wed Apr 14, 2004 5:34 pm    Post subject: Re: non-const aus const aufrufen Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ (German) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.