Hello, |
Re: Pointer to objects Object o
-Louie |
Re: Pointer to objects llandale - Tue Aug 14 14:02:18 EDT 2012
-Louie
Hello,
/*
* Basic structs.
*/
#include <stdio.h>
#include <string.h>
/* Struct is like a class w/o methods. */
struct fred {
int m;
char str[40];
double d;
};
/* Notice the semicolon above. */
/* Print a fred, preceded by a message. */
void pfred(char *t, struct fred *fr)
{
printf("%s: [ m = %d, str = %s, d = %f ]\n",
t, fr->m, fr->str, fr->d);
}
int main()
{
/* This declares a variable f1 which is a fred, and initializes it
to the values indicated. */
struct fred f1 = { 40, "Howdy", 5.498 };
/* This declares two variables, an uninitialized fred and a pointer
to a fred object. */
struct fred f2, *fp;
f2 = f1;
pfred("f1", &f1);
pfred("f2", &f2);
f2.m += 10;
strcat(f2.str, " there");
f2.d = 16.0001;
pfred("AA", &f2);
fp = &f1;
fp->m = -55;
fp->str[3] = '\0';
fp->d = 111.111;
pfred("ZZ", &f1);
}
|
Re: Pointer to objects SystemAdmin - Tue Aug 14 14:13:26 EDT 2012
Hello,
/*
* Basic structs.
*/
#include <stdio.h>
#include <string.h>
/* Struct is like a class w/o methods. */
struct fred {
int m;
char str[40];
double d;
};
/* Notice the semicolon above. */
/* Print a fred, preceded by a message. */
void pfred(char *t, struct fred *fr)
{
printf("%s: [ m = %d, str = %s, d = %f ]\n",
t, fr->m, fr->str, fr->d);
}
int main()
{
/* This declares a variable f1 which is a fred, and initializes it
to the values indicated. */
struct fred f1 = { 40, "Howdy", 5.498 };
/* This declares two variables, an uninitialized fred and a pointer
to a fred object. */
struct fred f2, *fp;
f2 = f1;
pfred("f1", &f1);
pfred("f2", &f2);
f2.m += 10;
strcat(f2.str, " there");
f2.d = 16.0001;
pfred("AA", &f2);
fp = &f1;
fp->m = -55;
fp->str[3] = '\0';
fp->d = 111.111;
pfred("ZZ", &f1);
}
First off, "DXL" is "C--"'s idiot half cousin. Don't get your hopes up.
Syntax in doors.exe:
void ::=(DxlObjectLHS lhs,_x newValue) setFieldValue
DxlObjectLHS ::->(DxlObject obj,string fieldName) getFieldLHS
_x ::->(DxlObject obj,string fieldName) getFieldValue
void delete(DxlObject theObject) deleteDxlObject
DxlObject automatic() autoDxlObject
DxlObject new() newDxlObject
struct DxlObjectLHS{}; struct DxlObject{};
|
Re: Pointer to objects SystemAdmin - Tue Aug 14 14:13:26 EDT 2012
Hello,
/*
* Basic structs.
*/
#include <stdio.h>
#include <string.h>
/* Struct is like a class w/o methods. */
struct fred {
int m;
char str[40];
double d;
};
/* Notice the semicolon above. */
/* Print a fred, preceded by a message. */
void pfred(char *t, struct fred *fr)
{
printf("%s: [ m = %d, str = %s, d = %f ]\n",
t, fr->m, fr->str, fr->d);
}
int main()
{
/* This declares a variable f1 which is a fred, and initializes it
to the values indicated. */
struct fred f1 = { 40, "Howdy", 5.498 };
/* This declares two variables, an uninitialized fred and a pointer
to a fred object. */
struct fred f2, *fp;
f2 = f1;
pfred("f1", &f1);
pfred("f2", &f2);
f2.m += 10;
strcat(f2.str, " there");
f2.d = 16.0001;
pfred("AA", &f2);
fp = &f1;
fp->m = -55;
fp->str[3] = '\0';
fp->d = 111.111;
pfred("ZZ", &f1);
}
Translating to DXL this would be:
/* Struct is like a class w/o methods. */
struct fred {} // DXL does not support member declaration inside the struct
// Helper Function to make property functions nicer
DxlObject DxlObjectOf (fred f) { return ((addr_ f) DxlObject) }
// so we manually declare property functions for our type
int get_m (fred f) { return ((DxlObjectOf f)->"x") int }
void set_m (fred f, int val) { ((DxlObjectOf f)->"x") = val }
string get_str (fred f) { return ((DxlObjectOf f)->"str") string }
void set_str (fred f, string val) { ((DxlObjectOf f)->"str") = val }
// notice how all property functions have exactly the same structure!
// Note that real in DXL is equivalent to double
real get_d (fred f) { return ((DxlObjectOf f)->"d") real }
void set_d (fred f, real val) { ((DxlObjectOf f)->"d") = val }
fred create_fred (int m, string s, real d) {
DxlObject dx = new()
fred f = addr_ dx
set_m(f, m)
set_str(f, s)
set_d(f, d)
return f
}
/* Print a fred, preceded by a message. */
void pfred(string t, fred gr)
{
print t ": [ m = " (get_m gr) ", str = " (get_str gr) ", d = " (get_d gr) " ]\n"
}
int main()
{
/* This declares a variable f1 which is a fred, and initializes it
to the values indicated. */
fred f1 = create_fred( 40, "Howdy", 5.498 )
/* This declares two variables, an uninitialized fred and a pointer
to a fred object. */
fred f2, *fp;
f2 = f1;
pfred("f1", f1);
pfred("f2", f2);
set_m(f2, get_m f2 + 10);
set_str(f2, (get_str f2) " there");
set_d(f2, 16.0001);
pfred("AA", f2);
fp = &f1;
set_m(*fp, -55);
set_str(*fp, (get_str (*fp))[0:2])
set_d(*fp, 111.111);
pfred("ZZ", f1);
return 0
}
main()
Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS
|