Page 1 of 2

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

Posted: Thu Nov 27, 2008 10:49 pm
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?

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

Posted: Thu Nov 27, 2008 11:13 pm
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;

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

Posted: Thu Nov 27, 2008 11:27 pm
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?

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

Posted: Thu Nov 27, 2008 11:43 pm
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

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

Posted: Fri Nov 28, 2008 12:09 am
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...

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

Posted: Fri Nov 28, 2008 12:11 am
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))

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

Posted: Fri Nov 28, 2008 12:15 am
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

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

Posted: Fri Nov 28, 2008 12:18 am
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.

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

Posted: Fri Nov 28, 2008 1:12 am
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?

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

Posted: Fri Nov 28, 2008 11:11 am
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;
}

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

Posted: Fri Nov 28, 2008 12:12 pm
by Falco Girgis
"this" is a pointer to the current object, btw.

Wouldn't it have to be

Code: Select all

this->member
?

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

Posted: Fri Nov 28, 2008 12:20 pm
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.

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

Posted: Fri Nov 28, 2008 12:27 pm
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

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

Posted: Fri Nov 28, 2008 2:59 pm
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.

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

Posted: Fri Nov 28, 2008 3:23 pm
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.