Du bist nicht angemeldet.

Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!

Werbeanzeige

Anonymous

unregistriert

1

29.02.2004, 13:39

const-ante Parameter

Hallo :huhu: :huhu:

Ich hoffe mal ich nerve euch nicht mit meinen zur Zeit sehr häufigen Fragen. Hier eine weitere:

Ich dachte wenn man einen konstanten Parameter übergibt, heißt das nur, dass die Funktion ihn nicht ändern darf. Ich habe aber folgendes probiert:

class NeClass
{
private:
TTyp* m_p;
public:
NeClass(const TTyp* p);
};

NeClass::NeClass(const TTyp* p)
:m_p(p)
{

}

Jetzt nervt mich der Kompiler, wenn ich NeClass instanziiere, mit 'ner Fehlermeldung:
Er könne const TTyp* nicht in TTyp* konvertieren.

Warum?

Samuel G. :) :) [/code]

Klaus

Treue Seele

Beiträge: 245

Wohnort: Stuttgart

Beruf: Schüler

  • Private Nachricht senden

2

29.02.2004, 20:47

Re: const-ante Parameter

Was macht der denn, wenn du das ":m_p(p)" in die Funktion rein verlagerst?
Also:

Quellcode

1
2
3
4
5
6
7
...


NeClass::NeClass(const TTyp* p)
{
    m_p = p;
}



Vielleicht raffts ja nur der Compiler net?!
Mozilla Firefox
The Browser - reloaded

3

29.02.2004, 22:25

Also folgendes:

const TTyp* p

bedeutet dass du nicht vorhast das Datum zu ändern auf das der Zeiger zeigt.


Du versprichst also in deinem Methodenkopf, dass du ein Datum nicht veränderst wenn ich einen Zeiger darauf reinstecke.

Dann machst du aber "klammheimlich" ne nicht-const Kopie vom Zeiger darauf und könntest darüber, ohne dass das weiter kontrolliert werden kann, das Datum doch verändern.

Vielleicht wolltest du aber gar nicht versprechen dass Datum nicht zu verändern, sondern wolltest versprechen, die Adresse die der Zeiger enthält nicht zu ändern, dann ist dein Weg:

TTyp* const p

Hier ein kleiner Auszug aus "Thinking in C++" vom Brutzeleckel dazu:

Zitat


Pointers

Pointers can be made const. The compiler will still endeavor to prevent storage allocation and do constant folding when dealing with const pointers, but these features seem less useful in this case. More importantly, the compiler will tell you if you attempt to change a const pointer, which adds a great deal of safety.

When using const with pointers, you have two options: const can be applied to what the pointer is pointing to, or the const can be applied to the address stored in the pointer itself. The syntax for these is a little confusing at first but becomes comfortable with practice.
Pointer to const

The trick with a pointer definition, as with any complicated definition, is to read it starting at the identifier and work your way out. The const specifier binds to the thing it is “closest to.” So if you want to prevent any changes to the element you are pointing to, you write a definition like this:

const int* u;

Starting from the identifier, we read “u is a pointer, which points to a const int.” Here, no initialization is required because you’re saying that u can point to anything (that is, it is not const), but the thing it points to cannot be changed.

Here’s the mildly confusing part. You might think that to make the pointer itself unchangeable, that is, to prevent any change to the address contained inside u, you would simply move the const to the other side of the int like this:

int const* v;

It’s not all that crazy to think that this should read “v is a const pointer to an int.” However, the way it actually reads is “v is an ordinary pointer to an int that happens to be const.” That is, the const has bound itself to the int again, and the effect is the same as the previous definition. The fact that these two definitions are the same is the confusing point; to prevent this confusion on the part of your reader, you should probably stick to the first form.
const pointer

To make the pointer itself a const, you must place the const specifier to the right of the *, like this:

int d = 1;
int* const w = &d;

Now it reads: “w is a pointer, which is const, that points to an int.” Because the pointer itself is now the const, the compiler requires that it be given an initial value that will be unchanged for the life of that pointer. It’s OK, however, to change what that value points to by saying

*w = 2;

You can also make a const pointer to a const object using either of two legal forms:

int d = 1;
const int* const x = &d; // (1)
int const* const x2 = &d; // (2)

Now neither the pointer nor the object can be changed.



Ich hoffe das hilft dir. Wer`s besser weiss möge mich korregieren.

Anonymous

unregistriert

4

01.03.2004, 14:19

Danke, aber...

Was ihr mir sagtet, ist mir eigentlich schon klar, aber was ich nicht verstehe:

Wenn ich einen Parameter als const deklariere bedeutet das doch nur, das ihn die Funktion nicht ändern darf. Das tue ich aber nicht!

Bidde helft mir bei diesem Verständnisproblem meinerseits!
:crying: :crying: :crying:

Samuel G.

NoName

Treue Seele

Beiträge: 118

Beruf: Student

  • Private Nachricht senden

5

01.03.2004, 14:33

Wenn dir alles, was geschrieben wurde, schon klar ist, wo ist dann noch das Problem?

Osram

Alter Hase

Beiträge: 889

Wohnort: Weissenthurm

Beruf: SW Entwickler

  • Private Nachricht senden

6

01.03.2004, 18:45

Zitat


Wenn ich einen Parameter als const deklariere bedeutet das doch nur, das ihn die Funktion nicht ändern darf.


Und die Funktion darf auch niemand anderem dies erlauben (ausser nem explizitem cast natürlich). Genau das tut sie aber bei Dir, indem sie aus dem "const TTyp* p" den "TTyp* m_p" macht, den dann jeder als nicht-const benutzen kann.

7

01.03.2004, 23:00

Du versprichst quasi nicht nur, das Datum nicht zu ändern, sondern auch, dass sich sein "Änderbarkeitsstatus" nicht ändert, d.h., gab es vor dem Methodenaufruf keine Möglichkeit es zu ändern, gibt es danach auch keine, gab es vorher einen Weg, gibt es danach auch höchstens einen.

Werbeanzeige