Refering to an object by composing variables in C++ BUILDER

A

Armando

I would like to know how (if its possible) to declare two (or more)
string variables into one and use this string to point to an (existing)
Object.

I tried the following but it goves me an error at run-time.

On the form I placed a Label (called Label1) and a Button. The
following code is in the On Click event of the button.

String lsFirst = "Label";
String lsLast = "1";

String lsString = lsFirst + lsLast;

TLabel * lbl = (TLabel *) &lsString;

lbl->Caption = "Hello"; //Gives an error at run-time!

Thanks for any help.

Armando V.
 
I

Ian Collins

Armando said:
I would like to know how (if its possible) to declare two (or more)
string variables into one and use this string to point to an (existing)
Object.

I tried the following but it goves me an error at run-time.

On the form I placed a Label (called Label1) and a Button. The
following code is in the On Click event of the button.

String lsFirst = "Label";
String lsLast = "1";

String lsString = lsFirst + lsLast;

TLabel * lbl = (TLabel *) &lsString;

lbl->Caption = "Hello"; //Gives an error at run-time!

Thanks for any help.
How can we tell without knowing what a String or a TLabel is?
 
A

Alf P. Steinbach

* Armando:
I would like to know how (if its possible) to declare two (or more)
string variables into one and use this string to point to an (existing)
Object.

I tried the following but it goves me an error at run-time.

On the form I placed a Label (called Label1) and a Button. The
following code is in the On Click event of the button.

String lsFirst = "Label";
String lsLast = "1";

String lsString = lsFirst + lsLast;

TLabel * lbl = (TLabel *) &lsString;

lbl->Caption = "Hello"; //Gives an error at run-time!

Thanks for any help.

The above code is based on a misconception of what kind of language C++ is.

Some languages, such as command interpreter languages, are based on run
time interpretation of the program text, which can be executed directly;
this is achieved by having the program text executed by a /program/.

Some languages, such as Lisp and JavaScript and even to some degree Java
and C#, are in-between in that they allow program text to be translated
to something executable at run time; this is achieved by some pretty
ingenious mechanisms that allow executable code to be added at run-time,
and then it's just a matter of plugging in the code produced by some
translation function.

Some languages, such as C++, are based on all translation of the program
text to executable form having been done prior to execution of the
program: no further translation at run time is supported (in standard
C++). In other words, standard C++ does not have a mechanism for adding
executable code at run time, and does not openly support that. However,
a very primitive and course-grained such mechanism, dynamic libraries,
is tacitly supported by suitably vague language in the standard, so that
actual C++ compilers for Windows and *nix do support dynamic libraries,
but in platform-specific and compiler-specific ways (off-topic here).

Having everything translated prior to execution allows for better
efficiency and more strict and global static type checking, but as you
have now experienced, it limits what you can do "directly".

Chances are that the Label and Button and other GUI classes you're using
do support look-up by name, and by using that functionality you can
achieve what you're trying to do without translating text to executable
code at run time.

However, that's off-topic in this group (it's not a C++ issue), and
furthermore, chances are that you're trying to do something really
simple in a really convoluted and complex way, i.e., that a solution to
the problem indicated above would be Very Bad Solution to the Real
Problem (which you should post in Borland or Windows programming group).
 
R

Robbie Hatley

Armando said:
I would like to know how (if its possible) to declare two (or more)
string variables into one

How do you declare one object "into" another? That string of
words conveys nothing to me.
and use this string to point to an (existing)
Object.

If by "string" you mean "std::string", then no, strings are not
pointers; they don't "point to" anything. They are objects.
You can change their value, yes. You can assign one to another,
yes. For example:

std::string s1 ("Good Golly ");
std::string s2 ("Miss Molly ");
std::string s3 ("Fred");

s3 = s1 + s2; // s3 is now "Good Golly Miss Molly"
I tried the following but it goves me an error at run-time.

On the form I placed a Label (called Label1) and a Button. The
following code is in the On Click event of the button.

String lsFirst = "Label";
String lsLast = "1";

String lsString = lsFirst + lsLast;

TLabel * lbl = (TLabel *) &lsString;

lbl->Caption = "Hello"; //Gives an error at run-time!

What is "String" with a capital "S"?

What is "TLabel"?

Why are you typecasting the address of a "String" to a "TLabel"
pointer? In my humble opinion, that is ill-advised no matter what
"TLabel" and "String" are. Typecasts are a good way to make code
that shuts-up the compiler's errors and warnings by telling it
sweet-sounding lies at compile time, at the cost of crashing a lot
at run time. Don't use typecasts. They're the work of Morgoth.


--
Cheers,
Robbie Hatley
East Tustin, CA, USA
lone wolf intj at pac bell dot net
(put "[usenet]" in subject to bypass spam filter)
http://home.pacbell.net/earnur/
 
J

Jerry Coffin

I would like to know how (if its possible) to declare two (or more)
string variables into one and use this string to point to an (existing)
Object.

Not directly -- but you might want to look up std::map.
 

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

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top