Click here to Skip to main content
15,913,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I have make a class derived from QString class and i want to overload typecast myString to char* conversion. i did as following.
// mystring.h
#ifndef MYSTRING_H
#define MYSTRING_H

#include <QString>

class MyString : public QString
{
public:
    MyString();
    operator char*()
    {
        QByteArray ba = toLocal8Bit();
        return ba.data();
    }
    void operator =(const QString& str){ clear();append(str);}
};

#endif // MYSTRING_H


// main.cpp file

#include <mystring.h>
int main(int argv,char* argc[])
{
   char* strChar = NULL;
   MyString strMy;
   strMy = "Hello";
   strChar = strMy;
   int i = 0;
}



but i can't get "Hello" in strChar.
How do i overload operator char* ?
I can't get whats wrong in my code.


Thanks in advance.
Posted

1 solution

You are returning the buffer held by a temporary object.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900