Hi there.
I am making my own pseudo string class and I need an overloaded operator for converting string into char*. Dont wanna use cstring /c_str() function.
Could you help me with it?
Greetings.
class IntWrapper
{
int x;
public:
IntWrapper() : x(0) {}
IntWrapper(const IntWrapper &from) : x(from.x) {}
IntWrapper(int from) : x(from) {}
//The assignment operator is provided by default unless you give your own definition:
IntWrapper &operator=(const IntWrapper &from)
{
x = from.x; //this just replicates the default behavior the compiler would give
return *this;
}
IntWrapper operator+(const IntWrapper &with) //custom operator in class scope
{
return IntWrapper(x + with.x);
}
operatorint() //use when casting an IntWrapper to an int
{
return x;
}
friend IntWrapper operator*(const InWrapper &left, const IntWrapper &right); //makes this global operator able to access private data
};
IntWrapper operator-(const IntWrapper &left, IntWrapper &right) //custom operator in global scope
{
return IntWrapper(int(left)+int(right));
}
IntWrapper operator*(const InWrapper &left, const IntWrapper &right) //custom operator in global scope, friend of IntWrapper
{
return IntWrapper(left.x * right.x); //can access private members
}
@ L B - it is a school project. I have to make a pseudo string class and among all the other functions I need to make an operator which is to convert a string into char*. I need help on how to declare this function inside a class and how to write the function itself.
ok, I tested it and I see it is wrong, but it can be ficed by replacing char x[a.size()+1];
with char* x=newchar[a.size()+1];
With that, it works perfectly.
char* a = &(x[0]); is indeed safe and guaranteed by the C++ standard as of C++11 because a string (just like vector as of C++03) is a contiguous array and it has an additional '\0' at x[x.size()] (although it may hold a bunch of other '\0's)
Of course, pointer invalidation rules apply for string resizing etc.
char* x=newchar[a.size()+1];
With that, it works perfectly.
Allocating new memory with a cast is a nasty thing to do. For one, if you cast a thing you still expect to be working with the original, not a copy. Generally such an operator would be implemented to make it easier to work with functions expecting a char*. Every time you fed this to such a function, it would be implicitly cast to char* and you would leak memory (as well as any changes made by the function being lost.)
char* string_to_char_p(string a) //here you make an unnecessary copy of the string parameter
{
char x[a.size()+1]; //illegal syntax: array sizes must be constant at compile time
for (int i=0; i<a.size(); i++) //possible signed/unsigned mismatch
{
x[i]=a[i];
}
x[a.size()]='\0';
return x; //when the function returns, memory allocated for x goes away
}
According to the OPer, the function needs to be a member function of his pseudo string class. I'm not sure why he wants his member function to convert a std::string to a char * instead of his own class to a const char *.