about output, from a class, char* to string

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

48 Replies - 1617 Views - Last Post: 23 December 2013 - 02:11 PM Rate Topic: -----

#1 Cambalinho  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 95
  • Joined: 04-March 12

about output, from a class, char* to string

Posted 22 December 2013 - 11:23 AM

heres the code that i use for output the char*:
operator const char*() const
    {
        return (char*)result.c_str();
    }

the question: why, for string, i must casting it for string?
what make me confuse is these:
string c;
char *b = "oi";
c=b;

if these code works, why i must casting the char* for string?

heres the entire class:
#include <iostream>
#include <string>
#include <typeinfo.h>

using namespace std;

class Variant
{
private:

    string result;

public:

    Variant()
    {
        result="";
    }

    template <typename T>
    Variant(T val)
    {
        result=to_string(val);
    }

    Variant(const string val)
    {
        result=val;
    }

    Variant(const char* val)
    {
        result=val;
    }

    Variant(char* val)
    {
        result=val;
    }

    Variant(const char val)
    {
        result=val;
    }

    Variant(bool val)
    {
        if (val==false)
        {
            result="false";
        }
        else
        {
            result="truedfds";
        }
    }

    friend ostream& operator<< (ostream &out, Variant &VResult)
    {
        out <<VResult.result;
        return out;
    }

    friend istream& operator >> (istream &in, Variant &VResult)
    {
        getline(in,VResult.result);
        return in;
    }

    operator const char*() const
    {
        return (char*)result.c_str();
    }

    operator double()
    {
        return atof(result.c_str());
    }

    operator unsigned int()
    {
        return atoi(result.c_str());
    }

    operator int()
    {
        return atoi(result.c_str());
    }

    operator long()
    {
        return atol(result.c_str());
    }

    operator unsigned long()
    {
        return atol(result.c_str());
    }



    operator bool()
    {
        if (result=="0" || result=="false" || result!="true")
        {
            return false;
        }
        else
        {
            return true;
        }
    }
};


int main()
{
    string c;
    Variant b="oi";
    c=(string)b;//works but i want without casting :(/>
    cout << c;
    return 0;
}


please tell me good answer:(

Is This A Good Question/Topic? 0
  • +

Replies To: about output, from a class, char* to string

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3105
  • View blog
  • Posts: 10,906
  • Joined: 08-August 08

Re: about output, from a class, char* to string

Posted 22 December 2013 - 11:40 AM

You're mixing languages: string c is a C++ string and char *b is an array of characters. Completely different types designed for different languages.
Was This Post Helpful? 0
  • +
  • -

#3 Cambalinho  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 95
  • Joined: 04-March 12

Re: about output, from a class, char* to string

Posted 22 December 2013 - 11:43 AM

View PostCTphpnwb, on 22 December 2013 - 11:40 AM, said:

You're mixing languages: string c is a C++ string and char *b is an array of characters. Completely different types designed for different languages.

so tell me, why the string operator is ignored?
Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3105
  • View blog
  • Posts: 10,906
  • Joined: 08-August 08

Re: about output, from a class, char* to string

Posted 22 December 2013 - 11:51 AM

It isn't. When I change your output line:
	string c;
	Variant b="oi";
	c=(string)b;//works but i want without casting :(/>/>
	cout << c << " b: "<<b << endl;


I get this:
oi b: oi


Was This Post Helpful? 0
  • +
  • -

#5 Cambalinho  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 95
  • Joined: 04-March 12

Re: about output, from a class, char* to string

Posted 22 December 2013 - 11:54 AM

View PostCTphpnwb, on 22 December 2013 - 11:51 AM, said:

It isn't. When I change your output line:
	string c;
	Variant b="oi";
	c=(string)b;//works but i want without casting :(/>/>/>
	cout << c << " b: "<<b << endl;


I get this:
oi b: oi


i used the operator string before and was ignored :(
that line works because i'm casting it, and i don't want casting it
Was This Post Helpful? 0
  • +
  • -

#6 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 687
  • View blog
  • Posts: 2,377
  • Joined: 31-December 10

Re: about output, from a class, char* to string

Posted 22 December 2013 - 11:55 AM

Just so you know, there are new functions for converting strings to integral types in C++11.

You might also want to consider adding some overloaded assignment operators, such as:
Variant& operator=(const string&);

Variant& operator=(const char*);



And, your constructor that takes a std::string simply takes a const string by value, you should really be passing it by const reference:
Variant(const string&);


Was This Post Helpful? 0
  • +
  • -

#7 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3105
  • View blog
  • Posts: 10,906
  • Joined: 08-August 08

Re: about output, from a class, char* to string

Posted 22 December 2013 - 11:58 AM

Quote

that line works because i'm casting it, and i don't want casting it
The first "oi" is from your variable c, which is cast as a string from b. The second is from b without casting. If you don't want to cast then don't cast:
cout << b << endl;


Was This Post Helpful? 0
  • +
  • -

#8 Cambalinho  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 95
  • Joined: 04-March 12

Re: about output, from a class, char* to string

Posted 22 December 2013 - 12:04 PM

View Postvividexstance, on 22 December 2013 - 11:55 AM, said:

Just so you know, there are new functions for converting strings to integral types in C++11.

You might also want to consider adding some overloaded assignment operators, such as:
Variant& operator=(const string&);

Variant& operator=(const char*);



And, your constructor that takes a std::string simply takes a const string by value, you should really be passing it by const reference:
Variant(const string&);


is these correct:
#include <iostream>
#include <string>
#include <typeinfo.h>

using namespace std;

class Variant
{
private:

    string result;

public:

    Variant()
    {
        result="";
    }

    template <typename T>
    Variant(T val)
    {
        result=to_string(val);
    }

    Variant(const string val)
    {
        result=val;
    }

    Variant(const char* val)
    {
        result=val;
    }

    Variant(char* val)
    {
        result=val;
    }

    Variant(const char val)
    {
        result=val;
    }

    Variant(bool val)
    {
        if (val==false)
        {
            result="false";
        }
        else
        {
            result="true";
        }
    }

    friend ostream& operator<< (ostream &out, Variant &VResult)
    {
        out <<VResult.result;
        return out;
    }

    friend istream& operator >> (istream &in, Variant &VResult)
    {
        getline(in,VResult.result);
        return in;
    }

    operator const char*() const
    {
        return (char*)result.c_str();
    }

    operator double()
    {
        return atof(result.c_str());
    }

    operator unsigned int()
    {
        return atoi(result.c_str());
    }

    operator int()
    {
        return atoi(result.c_str());
    }

    operator long()
    {
        return atol(result.c_str());
    }

    operator unsigned long()
    {
        return atol(result.c_str());
    }

    operator bool()
    {
        if (result=="0" || result=="false")
        {
            return false;
        }
        else
        {
            return true;
        }
    }

    Variant& operator=(const string&)
    {
        return *this;
    }
};


int main()
{
    string c;
    Variant b="oi";
    c=b;
    cout << c;
    return 0;
}


????
i continue with same error message:
"ambiguous overload for 'operator=' (operand types are 'std::string {aka std::basic_string<char>}' and 'Variant')"
Was This Post Helpful? 0
  • +
  • -

#9 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3105
  • View blog
  • Posts: 10,906
  • Joined: 08-August 08

Re: about output, from a class, char* to string

Posted 22 December 2013 - 12:10 PM

What are you trying to do with the = operator? Normally it would be an assignment operator so you'd set result to a value. Why are you trying to return a reference to the object?

This post has been edited by CTphpnwb: 22 December 2013 - 12:11 PM

Was This Post Helpful? 0
  • +
  • -

#10 Cambalinho  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 95
  • Joined: 04-March 12

Re: about output, from a class, char* to string

Posted 22 December 2013 - 12:14 PM

View PostCTphpnwb, on 22 December 2013 - 12:10 PM, said:

What are you trying to do with the = operator? Normally it would be an assignment operator so you'd set result to a value. Why are you trying to return a reference to the object?

you have seen, that i don't know correctly... but now i did these:
Variant& operator=(const string&)
    {
        return *this->result;
    }

but i get errors :(/>
please correct me for learn more

This post has been edited by Cambalinho: 22 December 2013 - 12:15 PM

Was This Post Helpful? 0
  • +
  • -

#11 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3105
  • View blog
  • Posts: 10,906
  • Joined: 08-August 08

Re: about output, from a class, char* to string

Posted 22 December 2013 - 12:19 PM

Explain why you think that method should return anything. If I were trying to over load the assignment operator I would probably have a single line in that method and it would start with result =.
Was This Post Helpful? 0
  • +
  • -

#12 Cambalinho  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 95
  • Joined: 04-March 12

Re: about output, from a class, char* to string

Posted 22 December 2013 - 12:27 PM

View PostCTphpnwb, on 22 December 2013 - 12:19 PM, said:

Explain why you think that method should return anything. If I were trying to over load the assignment operator I would probably have a single line in that method and it would start with result =.

sorry.. but i need output the result for a string :(
sorry i'm trying these by several weeks, but i continue with these problem :(
what i need is the string recive my Variant result without the casting.
Was This Post Helpful? 0
  • +
  • -

#13 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 687
  • View blog
  • Posts: 2,377
  • Joined: 31-December 10

Re: about output, from a class, char* to string

Posted 22 December 2013 - 12:33 PM

You haven't changed the constructor that takes a const string by-value to be a constructor that takes a string by const-reference.

Then the assignment operator you wrote does absolutely NOTHING! When you assign a value to something, shouldn't it atleast hold the value that you assigned?

Lastly, passing any of the POD (Plain-Old-Data) types (i.e., char, bool, int, float) as constants and by value is just wrong. When passing by value, each time the function is called, its own copy of the variable is pushed on the stack for it work on, so the const is just unnecessary, unless you meant to pass it by const reference. In that case, you're missing a '&' symbol.
Was This Post Helpful? 0
  • +
  • -

#14 Cambalinho  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 95
  • Joined: 04-March 12

Re: about output, from a class, char* to string

Posted 22 December 2013 - 12:40 PM

View Postvividexstance, on 22 December 2013 - 12:33 PM, said:

You haven't changed the constructor that takes a const string by-value to be a constructor that takes a string by const-reference.

Then the assignment operator you wrote does absolutely NOTHING! When you assign a value to something, shouldn't it atleast hold the value that you assigned?

Lastly, passing any of the POD (Plain-Old-Data) types (i.e., char, bool, int, float) as constants and by value is just wrong. When passing by value, each time the function is called, its own copy of the variable is pushed on the stack for it work on, so the const is just unnecessary, unless you meant to pass it by const reference. In that case, you're missing a '&' symbol.

sorry i'm trying follow you...
#include <iostream>
#include <string>
#include <typeinfo.h>

using namespace std;

class Variant
{
private:

    string result;

public:

    Variant()
    {
        result="";
    }

    template <typename T>
    Variant(T val)
    {
        result=to_string(val);
    }

    Variant(const string &val)
    {
        result=val;
    }

    Variant(const char* val)
    {
        result=val;
    }

    Variant(char* val)
    {
        result=val;
    }

    Variant(const char val)
    {
        result=val;
    }

    Variant(bool val)
    {
        if (val==false)
        {
            result="false";
        }
        else
        {
            result="true";
        }
    }

    friend ostream& operator<< (ostream &out, Variant &VResult)
    {
        out <<VResult.result;
        return out;
    }

    friend istream& operator >> (istream &in, Variant &VResult)
    {
        getline(in,VResult.result);
        return in;
    }

    operator double()
    {
        return atof(result.c_str());
    }

    operator unsigned int()
    {
        return atoi(result.c_str());
    }

    operator int()
    {
        return atoi(result.c_str());
    }

    operator long()
    {
        return atol(result.c_str());
    }

    operator unsigned long()
    {
        return atol(result.c_str());
    }

    operator bool()
    {
        if (result=="0" || result=="false")
        {
            return false;
        }
        else
        {
            return true;
        }
    }

    Variant& operator=(const char* &value)
    {
        result=value;
        return *this;
    }

    Variant& operator=(const string &value)
    {
        result=value;
        return *this;
    }
};

int main()
{
    string c;
    Variant b="oi";
    c=b;
    cout << c;
    return 0;
}

but i continue with errors :(
Was This Post Helpful? 0
  • +
  • -

#15 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 687
  • View blog
  • Posts: 2,377
  • Joined: 31-December 10

Re: about output, from a class, char* to string

Posted 22 December 2013 - 01:09 PM

Well it would help if you posted those errors.

*EDIT*:
This is part of line #23:
to_string(val);

Where is this to_string() function declared and defined?

This post has been edited by vividexstance: 22 December 2013 - 01:10 PM

Was This Post Helpful? 0
  • +
  • -

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »