Autor Beitrag
maximus
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 896

Win XP, Suse 8.1
Delphi 4/7/8 alles prof
BeitragVerfasst: Di 11.02.03 22:54 
Hallo.

Ich möchte einen typ erstellen, den ich auf multiplen ebenen ansprechen kann. zB:
ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
type 
  TdmRGB = Packed record
    case Integer of
             0 : (b,g,r,a : Byte);
             1 : (Color32 : cardinal);
    end;

    TdmC = TdmRGB as cardinal; // Hier liegt das problem, ich will, dass er TdmC als cardinal behandelt!

...natürlich geht es nicht mit as! Aber man muss dem compiler doch verklickern können, dass er ein 32 bit grosses record als cardinal betrachtet? oder ist es hoffnungslos?

ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
 var col1: TdmC;
var col2: TdmRGB;
var alpha: byte;
...
col1 := $FF45FF12;
col2 := col1;
alpha := col2.a;

Wär halt geil :D (Das man das auch anders typecasten kann ist mir)

Ideen?

mfg mx
AXMD
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 4006
Erhaltene Danke: 7

Windows 10 64 bit
C# (Visual Studio 2019 Express)
BeitragVerfasst: Di 11.02.03 23:20 
Hi,

echt eine harte Nuss. Bist du dir sicher, dass das überhaupt möglich ist?

AXMD
DaRkFiRe
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 526

WinXP Home & Professional
C, C++, Delphi
BeitragVerfasst: Mi 12.02.03 01:08 
Könnte mir das nur über Speicherkopieren Vorstellen

ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
var col1: TdmC; 
var col2: TdmRGB; 
var alpha: byte; 
... 
col1 := $FF45FF12; 
CopyMemory(@col2,@col1,sizeof(col1)); //funzt nur, wenn sizeof(col1)<=sizeof(col2) -> andernfalls: umschreiben
alpha := col2.a;

_________________
Lang ist der Weg durch Lehren - kurz und wirksam durch Beispiele! Seneca
maximus Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 896

Win XP, Suse 8.1
Delphi 4/7/8 alles prof
BeitragVerfasst: Mi 12.02.03 01:13 
Ich bin keinesfalls sicher :) Aber das bringt mich ja nicht davon ab zu fragen...delphi hält ja immernoch möglichkeiten und überraschungen für einen bereit.

Wenn ich schon im typ casten könnte, dann könnte ich mir das an tausend anderen stellen sparen.
maximus Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 896

Win XP, Suse 8.1
Delphi 4/7/8 alles prof
BeitragVerfasst: Mi 12.02.03 01:27 
@DarkFire: klar is des ne möglichkeit...aber dachte eher anne schöne saubere syntax lösung.

ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
var col1: cardinal; 
var col2: ^TdmRGB; 
var alpha: byte; 
... 
col1 := $FF45FF12;
col2 := @Col1;
alpha := col2.a;
ShowMessage(intToStr(alpha));

so funktioniert es wunderbar...aber eigentlich auch nicht was ich wollte! Ist wahrscheinlich die beste alternative zu meiner verrückten idee 8)

mfg
[/quote]
Brueggendiek
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 304

Win 98, Win98SE, Win XP Home
D5 Std
BeitragVerfasst: Mi 12.02.03 02:33 
Titel: Re: Harte nuss! Extrem typecasting schon in der deklaration
Hallo Maximus!

Natürlich geht das! Du mußt nur eine Variable vom Typ TdmRGB deklarieren und kannst dann auf die Felder zugreifen. Die belegen den gleichen Platz im Speicher und sind deshalb voneinander abhängig.

ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
type 
var dmRGB:  TdmRGB;
wert: Cardinal;

dmrgb.b:=127;
dmrgb.g:=125;
dmrgb.r:=0;
dmrgb.a:=0;

wert := dmRGB.color32;


Mit einer einfachen Zuweisung an dmRGB geht das nicht, es muß auch ein Element angegeben werden.


Gruß

Dietmar Brüggendiek
maximus Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 896

Win XP, Suse 8.1
Delphi 4/7/8 alles prof
BeitragVerfasst: Mi 12.02.03 10:39 
8) Is klar, deshalb hab ich den typ ja so erstellt! War wohl nur 'ne fixe idee von mir...äh dass element-frei machen zu wollen. Hätt ja sein können.