conversion from string to char*

Pages: 12
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.
why not use c_str()?
1
2
3
4
char* operator/*the sign of your operator*/(string a)
{
return a.c_str();
}
That looks useful, can you really make your own operators for basical types? I mean, can you then compile something like this?
1
2
3
4
5
6
7
8
9
10
class FileLoader {
	private:
		char * Filename;
	public:
		char * GetFileName() { ... }
}
char * operator=(FileLoader f)
{
	return f.GetFileName();
}

If it is going to work, that is going to be really useful.
Last edited on
You cannot overload the assignment operators in the global scope, and additionally globally defined operators must take two parameters.

Here is an example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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);
    }

    operator int() //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
}
See: http://www.cplusplus.com/doc/tutorial/classes2/
Last edited on
@viliml - I said I dont wanna use string library, i want to implement such a function myself, the thing is i dont know which way i should go
Last edited on
It depends on how you have your class set up. If you have a dynamic array for your custom string class, you can just return a const char* to it.

I am confused as to what you want exactly, could you be more specific? Where do you need help?
You can do this. I make no claims to its safety.

1
2
string x = "BeansOnToast";
char* a = &(x[0]);
@ 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.
You mean, convert std::string to const char *?

How will this function be used?
Declare it in your class (which I'm calling mowiczSpecialStringClass) along the lines of:

char* convertToCharPointer();

and define it along the lines of

1
2
3
4
char* mowiczSpecialStringClass::convertToCharPointer()
{
// your code here
}


As for how to actually do it; well, that really depends on how you've actually implemented your mowiczSpecialStringClass class.
OK, it's been a long time, but i have been visiting old topics and trying to revive them
sou, you could try this:
1
2
3
4
5
6
7
8
9
10
char* string_to_char_p(string a)
{
      char x[a.size()+1];
      for (int i=0; i<a.size(); i++)
      {
            x[i]=a[i];
            }
      x[a.size()]='\0';
      return x;
      }

or, if this actualy works on global scope:
1
2
3
4
5
6
7
8
9
10
operator char*(string a)
{
      char x[a.size()+1];
      for (int i=0; i<a.size(); i++)
      {
            x[i]=a[i];
            }
      x[a.size()]='\0';
      return x;
      }
Last edited on
@viliml - That will not work because the array x will only exist until the function ends.
why not? If you assign the return vale of the function(or operator) to a char* variable, it should work fine!
Not only that, but this is illegal syntax in C++. Arrays have to have fixed, compile-time sizes.
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=new char[a.size()+1];
With that, it works perfectly.
Last edited on
@Moschops

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.
Last edited on
@viliml - Now the caller of the function has to be careful not to forget to delete the array when done with it.
char* x=new char[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.)
Last edited on
OK, I've realised tat the operator is impossible, but isn't the function completly fine?
1
2
3
4
5
6
7
8
9
10
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 *.
Last edited on
Pages: 12