By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,212 Members | 1,068 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,212 IT Pros & Developers. It's quick & easy.

"Dynamic" Object Structures?

P: n/a
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)',
%%%% <ya***@ieee.org> % *A New World Record*, ELO
http://home.earthlink.net/~yatescr
Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Randy Yates wrote:
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
Jul 22 '05 #2

P: n/a
Randy Yates wrote:
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.

--
Salu2
Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.