"Dynamic" Object Structures?

R

Randy Yates

Having done a bit of Access Basic programming, I'm realizing
that AB does seem to have (as much as I hate to admit it since
I think it's a toy language) an advantage over C++.

Let's say I have a table called "tblCars" that has the fields fYear
(integer), fModel (string), fMake (string). When I perform a query in
AB, I do something like:

Dim rstCars As Recordset
Dim year As Integer

set rstCars = dbCurrent.OpenRecordset("SELECT * FROM tblCars")

Now here's the rub: I can then access the fields of the recordset
as

year = rstCars!fYear

AB has syntax that allows me to make such a construction at compile-time
that will yield a dynamic result at run-time.

So my basic question is, is there any mechanism in C++ to do a similar
thing?

The closest I can come is to define a recordset object that has a member
function

void * Fields(char* fName);

so that I can say

int year;
recordset rstCars("SELECT * FROM tblCars");

year = rstCars.Fields("fYear");

Is this a reasonable way to do this? Is there any mechanism in C++ to
do it more elegantly?
--
% Randy Yates % "Watching all the days go by...
%% Fuquay-Varina, NC % Who are you and who am I?"
%%% 919-577-9882 % 'Mission (A World Record)',
%%%% <[email protected]> % *A New World Record*, ELO
http://home.earthlink.net/~yatescr
 
V

Victor Bazarov

Randy said:
Having done a bit of Access Basic programming, I'm realizing
that AB does seem to have (as much as I hate to admit it since
I think it's a toy language) an advantage over C++.

Let's say I have a table called "tblCars" that has the fields fYear
(integer), fModel (string), fMake (string). When I perform a query in
AB, I do something like:

Dim rstCars As Recordset
Dim year As Integer

set rstCars = dbCurrent.OpenRecordset("SELECT * FROM tblCars")

Now here's the rub: I can then access the fields of the recordset
as

year = rstCars!fYear

AB has syntax that allows me to make such a construction at compile-time
that will yield a dynamic result at run-time.

I think you're forgetting that there is a significant difference between
BASIC and C++ when it comes to "compiling". AB probably simply creates
some kind of byte code or equivalent and the attempt to resolve the names
of such "members" is still made at run-time.
So my basic question is, is there any mechanism in C++ to do a similar
thing?
No.

The closest I can come is to define a recordset object that has a member
function

void * Fields(char* fName);

so that I can say

int year;
recordset rstCars("SELECT * FROM tblCars");

year = rstCars.Fields("fYear");

Is this a reasonable way to do this? Is there any mechanism in C++ to
do it more elegantly?

Any way is reasonable as long as it works. And, again, no, there is no
such mechanism in C++, and you have come up with something that seems
quite alright.

There are libraries for that sort of thing and there are extensions to
the language, all essentially serving the same purpose -- doing at run-
time what _cannot_ be done at compile-time.

V
 
?

=?ISO-8859-15?Q?Juli=E1n?= Albo

Randy said:
The closest I can come is to define a recordset object that has a member
function

void * Fields(char* fName);

so that I can say

int year;
recordset rstCars("SELECT * FROM tblCars");

year = rstCars.Fields("fYear");

Is this a reasonable way to do this? Is there any mechanism in C++ to
do it more elegantly?

You can write an operator [ ] or operator ( ) instead of a function. But
surely many people will disagree about what way is more elegant.

You can also create a Field class, create objects of this class with the
field names, and use those objects as parameters instead of string
literals.
 
R

Randy Yates

=?ISO-8859-15?Q?Juli=E1n?= Albo said:
Randy said:
The closest I can come is to define a recordset object that has a member
function

void * Fields(char* fName);

so that I can say

int year;
recordset rstCars("SELECT * FROM tblCars");

year = rstCars.Fields("fYear");

Is this a reasonable way to do this? Is there any mechanism in C++ to
do it more elegantly?

You can write an operator [ ] or operator ( ) instead of a function. But
surely many people will disagree about what way is more elegant.

That sounds like a great idea - at least the syntax would be even closer
to VB.
You can also create a Field class, create objects of this class with the
field names, and use those objects as parameters instead of string
literals.

Don't want to do that - duplication of effort. The fields should be inherently
identified by the class via the SQL query text.

Thanks for the ideas!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top