Is it possible to use a string as an objects name?

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Is it possible to use a string as an objects name?

Post by XianForce »

So just to lay it all out, I'm hard core practicing classes right now, I'm going to dev the shittiest shit that was ever shit devved, all night xD.

And to practice some classes I was going to make a simple program that asked the name of your pet, and what animal it was. Every animal would be a class...but would it work to do this:

Code: Select all

string str1;
std::cin >> str1;
Dog.str1;
Would that at all work? if not, could someone point me in the right direction?
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Is it possible to use a string as an objects name?

Post by MarauderIIC »

Um, assuming Dog is a class name not a class instance, no.
If you want more than one copy of something and you don't know in advance how many you'll have, you'll probably want a vector. Or a map.

EDIT If you want a fixed preset limit, use an array.

With a vector, you'd do something like (simple but inefficient example follows...)

Code: Select all

#include <vector>
#include <iostream>
...
using namespace std;
...
vector<Dog> dogs;

string input;
cout << "Name?";
cin >> input;
while (input != "q") {
    Dog dog;
    dog.name = input;
    cout << "Name?";
    cin >> input;
    dogs.push_back(dog);
}

cout << "outputting all dogs" << endl;
//this just says i want an iterator (something that steps through elements) that can iterate through a vector containing Dog[s]. go from beginning to end. (end returns one past the last element)
for (vector<Dog>::iterator i = dogs.begin(); i != dogs.end(); ++i)
    cout << i->name << " other stuff: " << i->MyClassOutputFn() << endl;
Editing in an example with maps...

Code: Select all

#include <map>
...
using namespace std;
...

map<string, Dog> dogs;

cout << "Name?";
cin >> input;
while (input != "q") {
    Dog dog;
    dog.name = input; //populate our dog class
    dogs[input] = dog; //add it to the map (maps auto-insert if the item you're setting does not exist)
    ...
}

cout << "Output what dog?";
cin >> input;
dogs[input].OutputDog();
cout << "Outputting all dogs" << endl
for (map<string, Dog>::iterator i = dogs.begin(); i != dogs.end(); ++i)
    cout << "name: " << i->first << " other stuff: " << i->second.MyClassOutputFn() << endl;
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Re: Is it possible to use a string as an objects name?

Post by XianForce »

Well I got the gist of it, I was only making 1, for now at least, maybe when I get more advanced I'll edit it but its just to get some practice in.

But looking at your example gave me the idea I needed. Since I'm only going to do one I could do:

Code: Select all

std::cout << "Name?";
std::cin >> input;
Dog dog;
dog.SetName(input);

...

SetName(input)
{
     dog.name = input;
}

That would work if I were only making one right?
Last edited by XianForce on Fri Nov 28, 2008 12:03 am, edited 1 time in total.
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Is it possible to use a string as an objects name?

Post by MarauderIIC »

No, your definition is wrong. SetName wouldn't know what dog to change EDIT: because "dog" wouldn't be in scope (unless dog was global but 'globals are bad')

dog.h

Code: Select all

#ifndef DOG_H
#define DOG_H
#include <string>
namespace...
class Dog {
string name;
public:
void setName(string pname);
};
#endif
dog.cpp

Code: Select all

void Dog::setName(string pname) {
    name = pname;
}
main.cpp

Code: Select all

main(...) {
...
Dog dog;
...get input...
dog.setName(input);
...
}
Edit2: also don't quote whole posts, jeez
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Re: Is it possible to use a string as an objects name?

Post by XianForce »

Isn't that what I just said, with using a header file? and the other cpp file?

EDIT: and how would dog be out of Scope? it was all in one function. In my actual code I'm working with, its pet instead of dog, and it declares:

Code: Select all

Dog pet(age, weight, name);
Then I have my constructor set those to the member variables of Dog...
Last edited by XianForce on Fri Nov 28, 2008 12:13 am, edited 1 time in total.
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Is it possible to use a string as an objects name?

Post by MarauderIIC »

No.

Code: Select all

SetName(input)
{
     dog.name = input;
}
(^ this does not know what dog to change)

is not
Dog::setName(input)
(^this always modifies the dog that calls it (Dog:: says that this fn is a member of Dog))
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Re: Is it possible to use a string as an objects name?

Post by XianForce »

lol I'm getting confuzzled...dog is the Dog I declared. So it should know that the Dog I wanted to edit was dog since I said dog.SetName
Last edited by XianForce on Fri Nov 28, 2008 12:18 am, edited 1 time in total.
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Is it possible to use a string as an objects name?

Post by MarauderIIC »

If SetName is defined in dog's class def, like:

class Dog {
....setName(input) { ... }
}

then it should be
class Dog {
...
setName(input) { name = input }
}

not

{ dog.name = input }

Otherwise you need

class Dog {
setName(input);
};

Dog::setName(input) { name = input }

You can't call dog.setName unless setName is at least declared (prototyped) in Dog's class definition, because setName would then not be part of Dog. If it is part of Dog, then you need to remove the "dog." part.

If dog is not defined in the class at all, you need
SetName(Dog& dog, ... input) { dog.name = input }

because otherwise dog is not in SetName's SCOPE.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Re: Is it possible to use a string as an objects name?

Post by XianForce »

Ok, I see its not in its scope, but then why would it be in scope for other accessor functions? (sorry if my noobish questions piss you off)

I have three accessor functions, and I define and declare them the same way. I use Pet.GetName/Age/Weight...Just like the examples in my book...it just doesn't work for GetName...So why is it only for GetName and not the others?

EDIT: nevermind, it doesn't work at all...

The reason I keep trying this is because my book has coding examples that have compiled fine such as:

Code: Select all

class Cat
{
     ...
     GetAge();
     ...
}

main()
{
     Cat Frisky;
     std::cout << Frisky.GetAge() << " is Frisky's age!";
}
Isn't this the same thing I'm doing?
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Is it possible to use a string as an objects name?

Post by avansc »

people use the "this" keydword for gods sake please.

Code: Select all

class bla
{
      int count;
      ....
     ....
}

void bla::setCount(int count)
{
       this.count = count;
}
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Is it possible to use a string as an objects name?

Post by Falco Girgis »

"this" is a pointer to the current object, btw.

Wouldn't it have to be

Code: Select all

this->member
?
User avatar
trufun202
Game Developer
Game Developer
Posts: 1105
Joined: Sun Sep 21, 2008 12:27 am
Location: Dallas, TX
Contact:

Re: Is it possible to use a string as an objects name?

Post by trufun202 »

avansc wrote:people use the "this" keydword for gods sake please.
At work, I'm on the .NET Council, and we define coding standards for the company.

I tried to make the use of "this" as a standard, but I was out voted. :(

Apparently, not everyone is a fan of it, but I think it makes the code easier to read.
-Chris

YouTube | Twitter | Rad Raygun

“REAL ARTISTS SHIP” - Steve Jobs
User avatar
programmerinprogress
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Wed Oct 29, 2008 7:31 am
Current Project: some crazy stuff, i'll tell soon :-)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++!
Location: The UK
Contact:

Re: Is it possible to use a string as an objects name?

Post by programmerinprogress »

GyroVorbis wrote:"this" is a pointer to the current object, btw.

Wouldn't it have to be

Code: Select all

this->member
?
I was just to about to put that before I scrolled down to your reply ;)

I guess you could use this.member if you were using VC#, this.close() makes your form close, but in C++ i'm sure only this->member would only work.

but who the hell cares about C# :P
---------------------------------------------------------------------------------------
I think I can program pretty well, it's my compiler that needs convincing!
---------------------------------------------------------------------------------------
And now a joke to lighten to mood :D

I wander what programming language anakin skywalker used to program C3-PO's AI back on tatooine? my guess is Jawa :P
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Re: Is it possible to use a string as an objects name?

Post by XianForce »

I got it to work, and I did it how I thought I could do it...I still have no idea what the hell Marauder was saying...I think there was some miscommunication somewhere...either that or I'm just a dumb ass, which is highly possible.
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Is it possible to use a string as an objects name?

Post by avansc »

GyroVorbis wrote:"this" is a pointer to the current object, btw.

Wouldn't it have to be

Code: Select all

this->member
?

actually thats a good question, not allways.

im not exactly sure now. but i think if its a pointer you do this->bla and if its a reference its this.bla.

"->" dereferences the pointer for you. you could say (type*)this.bla. but thats getting a bit technical.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Post Reply