the literal string: "hello world"
it's considered a const char*?
(in some overload operators parameters we must use const char* instead char*... i need understand)
You have something that cannot be changed. Immutable. Const. Would you loan it to someone that does not promise that they won't attempt to modify your object?
In U foo( const T * bar );
* the foo promises to you to keep its fingers away from bar
* compiler ensures that foo's implementation maintains that promise
Attempt to modify string literals leads to undefined behaviour.
1 - yes i did a class variant.. for accept strings and numbers;
2 - 2 constructors twice was a mistake(i did some copys... now heres all);
3 - "You don't properly initialize the 'strValue' in your constructors."
i don't use that method... and not all compilers aren't ok with it;
4 - "Your operators are not "const correct"."
i belive you speak about:
the literal string: "hello world"
it's considered a const char*?
nobody seems to have pointed it out explicitly, but it's not any kind of pointer. The type of "hello world" is constchar[12], an array of 12 constant chars:
1 2 3 4 5 6 7
#include <iostream>
int main()
{
std::cout << sizeof"hello world" << '\n'; // prints 12
for(char c : "hello world") // cannot do that with a pointer
std::cout << c;
}
it doesn't have a lot of bearing on your program, but it may come up elsewhere one day.
if i miss something, i accept been corrected ;)
but the compiler see the difference between const char* and char*... and for the literal string i must use the const char* or i will get an error
using const char* or const char[], on functions parameters, it's the same
True, passing an array as a function parameter uses a different syntax entirely.
as I said, it doesn't affect your program since you force array to pointer conversions by presenting constructors that take pointers to char. Just for variety, here's how it would look if you took arrays as function parameters:
3 - "You don't properly initialize the 'strValue' in your constructors."
i don't use that method... and not all compilers aren't ok with it;
Cubbi's code does demonstrate the initializer list syntax:
1 2 3
variant::variant( const std::string& strvalue )
: strValue(strvalue) // member initialization
{} // nothing has to be done in the body
"not all compilers"?
Yes, it is possible that there still exists compilers that do not support what has been in the C++ Standard for 20 years. Back away from them very slowly. You may call a paleontologist, but do not touch such compilers yourself.
4 - "Your operators are not "const correct"."
i belive you speak about:
1 2 3 4 5 6 7 8 9 10
struct Foo {
void bar();
void gaz() const;
};
int main() {
const Foo foo;
foo.bar(); // ERROR: foo is const
foo.gaz(); // ok
}
If a member function does not modify the object, then it should be const so that you can call that member on const objects. None of your operators can be called on const objects, because none of them is const.