Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,557 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,925 people online right now. Registration is fast and FREE... Join Now!




Dynamicaly naming objects

 
Reply to this topicStart new topic

Dynamicaly naming objects, How can I name an object by the contents of a variable?

Tom Savage
11 Apr, 2008 - 11:45 AM
Post #1

New D.I.C Head
*

Joined: 11 Apr, 2008
Posts: 2


My Contributions
Hi all.

I am quite new to C++ and object oriented programming. Although I understand the concepts well enough, one question has been bothering me. Is there a way to name an object using the contents of a variable?

An example of how I would want to use would probably help. Say I wanted to create an object called jeep1 when a user presses a key and then when the key is pressed again, I would want to create jeep2, then jeep 3, etc.

So far I have just been using:
CODE
Vehicle jeep;
to create my objects but I think that there are other ways.

Any help is apreciated.
User is offlineProfile CardPM
+Quote Post

jeronimo0d0a
RE: Dynamicaly Naming Objects
11 Apr, 2008 - 12:32 PM
Post #2

D.I.C Head
**

Joined: 3 Mar, 2008
Posts: 141


My Contributions
You can accomplish this in a few different ways.
If you know the total # of jeeps in advance you can use an array.
vehicle jeep[20] ; // max 20 jeeps
if not you can use a linked list of some type or a container class.
You CANNOT name variables dynamically but there is such a thing as an associative container which holds name/value-object pairs. You could say:

prog: what do you want to name your jeep?
user: OldBlue
prog: your jeep has been created, to access it, call it by name.
user: start_jeep OldBlue
prog: OldBlue is now running

The template libraries work well but if you're learning I'd do my own linked list for the experience. You can list all jeeps, sort them by name, mileage, year and learn a lot doing the programming.
User is offlineProfile CardPM
+Quote Post

Tom Savage
RE: Dynamicaly Naming Objects
12 Apr, 2008 - 05:00 AM
Post #3

New D.I.C Head
*

Joined: 11 Apr, 2008
Posts: 2


My Contributions
Ah, ok. I will give that a go.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/2/08 11:27PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month