Welcome to Gaia! ::


Is there a way I can do something like
std::string name;
if(name == "Fred"){//blah blah blah}

The Committee Staff Gaian

9,775 Points
  • Elysium's Gatekeeper 100
  • Noble Shade 100
  • Battle: Cleric 100
Dudironimis
Is there a way I can do something like
std::string name;
if(name == "Fred"){//blah blah blah}


Yes, how you've done it is fine. There are other ways to do so too but the string object is capable of making comparisons between instances of string objects, string literal and etc so what you have is fine.

Of course given that code the string will always be an empty string so obviously implement some sort of logic before the comparison.
lm a Kitty Cat
Dudironimis
Is there a way I can do something like
std::string name;
if(name == "Fred"){//blah blah blah}


Yes, how you've done it is fine. There are other ways to do so too but the string object is capable of making comparisons between instances of string objects, string literal and etc so what you have is fine.

Of course given that code the string will always be an empty string so obviously implement some sort of logic before the comparison.


Oh I can just use ==? I figured it wouldn't work because it isn't a primitive.

The Committee Staff Gaian

9,775 Points
  • Elysium's Gatekeeper 100
  • Noble Shade 100
  • Battle: Cleric 100
Dudironimis
lm a Kitty Cat
Dudironimis
Is there a way I can do something like
std::string name;
if(name == "Fred"){//blah blah blah}


Yes, how you've done it is fine. There are other ways to do so too but the string object is capable of making comparisons between instances of string objects, string literal and etc so what you have is fine.

Of course given that code the string will always be an empty string so obviously implement some sort of logic before the comparison.


Oh I can just use ==? I figured it wouldn't work because it isn't a primitive.


Ahhh yes, well it doesn't just work automatically as it may appear before you delve into studying class objects.

The string object, while not a primitive, can be used with the comparison operator. This is only because the string class has had that operator overloaded, which means the string class actually defines what == means when used with an instance of string.

Here is some documentation on the string's == operator overloads

http://www.cplusplus.com/reference/string/string/operators/

According to the documentation it just calls the Compare method of the string object. It will work though, I promise. The string object has been defined to allow it to work.
lm a Kitty Cat
Dudironimis
lm a Kitty Cat
Dudironimis
Is there a way I can do something like
std::string name;
if(name == "Fred"){//blah blah blah}


Yes, how you've done it is fine. There are other ways to do so too but the string object is capable of making comparisons between instances of string objects, string literal and etc so what you have is fine.

Of course given that code the string will always be an empty string so obviously implement some sort of logic before the comparison.


Oh I can just use ==? I figured it wouldn't work because it isn't a primitive.


Ahhh yes, well it doesn't just work automatically as it may appear before you delve into studying class objects.

The string object, while not a primitive, can be used with the comparison operator. This is only because the string class has had that operator overloaded, which means the string class actually defines what == means when used with an instance of string.

Here is some documentation on the string's == operator overloads

http://www.cplusplus.com/reference/string/string/operators/

According to the documentation it just calls the Compare method of the string object. It will work though, I promise. The string object has been defined to allow it to work.

Oh I see.

This is unrelated, but while your here, I don't understand what my professor is asking for here.

"If the argument object holds the same data as the calling object" What is a calling object?

The Committee Staff Gaian

9,775 Points
  • Elysium's Gatekeeper 100
  • Noble Shade 100
  • Battle: Cleric 100
Dudironimis

Oh I see.

This is unrelated, but while your here, I don't understand what my professor is asking for here.

"If the argument object holds the same data as the calling object" What is a calling object?


Hmmmm, is there any other context? The question doesn't really make sense to me.
lm a Kitty Cat
Dudironimis

Oh I see.

This is unrelated, but while your here, I don't understand what my professor is asking for here.

"If the argument object holds the same data as the calling object" What is a calling object?


Hmmmm, is there any other context? The question doesn't really make sense to me.


"A function named equalMonth that accepts a Month object as an argument. If the argument object holds the same data as the calling object, the function should return true. Otherwise, it should return false"

The Committee Staff Gaian

9,775 Points
  • Elysium's Gatekeeper 100
  • Noble Shade 100
  • Battle: Cleric 100
Dudironimis
lm a Kitty Cat
Dudironimis

Oh I see.

This is unrelated, but while your here, I don't understand what my professor is asking for here.

"If the argument object holds the same data as the calling object" What is a calling object?


Hmmmm, is there any other context? The question doesn't really make sense to me.


"A function named equalMonth that accepts a Month object as an argument. If the argument object holds the same data as the calling object, the function should return true. Otherwise, it should return false"


This is some odd terminology, I've never been too keen on words personally, but anyway if the parameters, or argument object, that is passed has the same exact data as the object of which this particular method/function is associated with then I'd say it would certainly return true.

This would mean the 'calling object' is equivalent to the 'argument object'. But it's possible your professor is looking for something else. Really an odd question imo, it isn't very straightforward to me.
lm a Kitty Cat
Dudironimis
lm a Kitty Cat
Dudironimis

Oh I see.

This is unrelated, but while your here, I don't understand what my professor is asking for here.

"If the argument object holds the same data as the calling object" What is a calling object?


Hmmmm, is there any other context? The question doesn't really make sense to me.


"A function named equalMonth that accepts a Month object as an argument. If the argument object holds the same data as the calling object, the function should return true. Otherwise, it should return false"


This is some odd terminology, I've never been too keen on words personally, but anyway if the parameters, or argument object, that is passed has the same exact data as the object of which this particular method/function is associated with then I'd say it would certainly return true.

This would mean the 'calling object' is equivalent to the 'argument object'. But it's possible your professor is looking for something else. Really an odd question imo, it isn't very straightforward to me.


k thx anyway
Dudironimis
lm a Kitty Cat
Dudironimis

Oh I see.

This is unrelated, but while your here, I don't understand what my professor is asking for here.

"If the argument object holds the same data as the calling object" What is a calling object?


Hmmmm, is there any other context? The question doesn't really make sense to me.


"A function named equalMonth that accepts a Month object as an argument. If the argument object holds the same data as the calling object, the function should return true. Otherwise, it should return false"
I assume you're saying that you have a Month class, which has a member function called equalMonth that has a signature like:
bool Month::equalMonth(const Month& other) const
And you'd call it like:
Month a;
Month b;
if (a.equalMonth(b)) {
// ....
}


That's the best I can parse out of your question with so little information. It's not at all clear what you mean by "calling object."

You'd implement that function by doing == comparisons with all of the value members of "this" with "other."

8,950 Points
  • Gaian 50
  • Member 100
  • Contributor 150
In psychic stalker's example, the "calling object" is Month a (the object whose method is being invoked) and the "argument object" is Month b, the object which is being passed as an argument to the invoked method.

The terminology is somewhat misleading and arbitrary in the context of C++, but I've heard it used in the context of dynamic scripting languages.

Edit: Actually I can think of a case in which it might be relevant in C++ or Java, so I guess it's not entirely misleading.

Quick Reply

Submit
Manage Your Items
Other Stuff
Get GCash
Offers
Get Items
More Items
Where Everyone Hangs Out
Other Community Areas
Virtual Spaces
Fun Stuff
Gaia's Games
Mini-Games
Play with GCash
Play with Platinum