-1

When using a string object, you can set the object equal to a string literal. How is this possible? The object consists of multiple functions and variables, so how can you just set it equal to one string literal?

Thanks!

Edit:

Sorry for the confusing question.

It makes sense to me that you can set a variable or array equal to a value. For example:

char word[3] = {'c', 'a', 'r'};

But a string is not simply a variable, so what in its class allows you to write a statement setting the object to a value?

string word = "car";

I would love to do this in my objects, but thus far I have been using something like this, where objectTemp is the class & tempF is a member holding the temperature value in Fahrenheit:

objectTemp cat;
cat.tempF = 102;
sharkByte
  • 33
  • 3
  • 1
    *How is this possible?* Are you asking how `std::string str = "Hello"` is possible? – Dean Seo Oct 15 '18 at 23:36
  • 1
    What C++ textbook are you learning from? –  Oct 15 '18 at 23:36
  • Welcome to stackoverflow! It's not very clear exactly what you're asking. Could you maybe post some specific code that you can't understand, with specific questions? – JMAA Oct 15 '18 at 23:37
  • Yes, Dean Seo, that's exactly what I am asking. – sharkByte Oct 16 '18 at 00:06
  • JMAA, I edited the question to try to clarify it, and added some code. – sharkByte Oct 16 '18 at 00:07
  • 1
    It's worth noting that functions are stored _per-class_ in your program, not per-object. Every `string` has the exact same members functions as any other `string`, and the compiler is well aware of this, and so doesn't store that information with each `string` object, referring to the correct functions instead simply by type inference (ignoring `virtual` functions for now). – alter_igel Oct 16 '18 at 01:10
  • @alterigel Oh got it! That makes more sense. Thanks for clearing that up for me :) – sharkByte Oct 16 '18 at 01:49

2 Answers2

4

C++ supports operator overload which allows to call the function:

string& operator= (const char* s); 

of class std::string. This simply takes a const char* (literal) and create internally the new std::string object.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
-1

That is possible because C++ knows of "magic" functions that are called upon object instantiation called constructors.

The other "magic" function you should look up is the assignment operator.

But a string is not simply a variable, so what in its class allows you to write a statement setting the object to a value?

string word = "car";

As said before, one of the constructors of std::string makes this magic happen:

std::string::string(char const*);

It takes a pointer to constant chars which are interpreted as a "C-string" (a zero-terminated string).

I would love to do this in my objects, but thus far I have been using something like this, where objectTemp is the class & tempF is a member holding the temperature value in Fahrenheit:

objectTemp cat;
cat.tempF = 102;

Write a constructor for your class (=don't be afraid of the magic) which takes a numeric value as its parameter. Eg.

class objectTemp {
    int tempF;
public:
    objectTemp(int tempF)  // constructors are named the same as the struct/class
    : tempF{ tempF }  // initializer-list
    {}  // an empty body
}

usage:

objectTemp cat = 102;

or:

objectTemp cat(102);

or:

objectTemp cat{ 102 };
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Swordfish
  • 12,971
  • 3
  • 21
  • 43