Jump to content
  • Advertisement
Sign in to follow this  
kernkraft

Operator Overriding Question

This topic is 5407 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am currently working on my own String class in C++ (almost done too!) and I want to be able to have code like this: LPWSTR = MyStringClassInstance; or MyStringClassInstance = Int + LPWSTR + Boolean; So I overrode operators for assignment where I'm converting TO my string class. These work okay but I also want to override the equality operator so that I can convert FROM my string class. However, when I do this: inline bool operator=(const MyString& sInput); inline char* operator=(const MyString& sInput); inline wchar_t* operator=(const MyString& sInput); inline int operator=(const MyString& sInput); The compiler gives me an error that I cannot have two methods with the same sig but different return types. So how would I actually code it? Is it even doable when overriding the equality operator? Maybe casts...

Share this post


Link to post
Share on other sites
Advertisement
The compiler cannot tell the difference between function overloads just based on the return type.

Overriding == Replacing inherited virtual method implementation with your own.
Overloading == Providing multiple implementation of a function/method with different parameter signatures.

Share this post


Link to post
Share on other sites
Quote:
Original post by kernkraft
inline bool operator=(const MyString& sInput);


this is not an equality operator, an equality operator as a member function would be of the form:


bool operator==(const MyString&) const;


and as a free function:


bool operator==(const MyString&, const MyString&);


Quote:
Original post by kernkraft
inline char* operator=(const MyString& sInput);
inline wchar_t* operator=(const MyString& sInput);
inline int operator=(const MyString& sInput);



These don't even make any sense, to do something like this you would overload the cast operator but thats not a good idea because there can be subtle problems with it.

Share this post


Link to post
Share on other sites
Quote:
Original post by kernkraft
I am currently working on my own String class in C++ (almost done too!) and I want to be able to have code like this:

LPWSTR = MyStringClassInstance;
or
MyStringClassInstance = Int + LPWSTR + Boolean;


I beleive you can't do that because none of them casts to MyStringClassInstance. You can however use
MyStringClassInstance = MyStringClassInstance + any_type
or
MyStringClassInstance = any_type + MyStringClassInstance

Also, use snpritf or boost::format for doing that.

Quote:
inline bool operator=(const MyString& sInput);
inline char* operator=(const MyString& sInput);
inline wchar_t* operator=(const MyString& sInput);
inline int operator=(const MyString& sInput);

Yes, casts. std::string has c_str() function which returns pointer to char array.

Casting to/from string can be done via boost::lexical_cast or std::stringstream.

Casting from string can be done via atol, atoi, fcvt, etc.

Share this post


Link to post
Share on other sites
I probably shouldn't tell you this, considering that what you're doing seems pretty dodgy to begin with, but here goes anyway:

operator TYPE();

No return type - its implied. This will let you define a cast from your class to TYPE.

Eg:

class Hack {
...
};

class Crap {
public:
operator Hack() const {
Hack h;
...
return h;
}
};

Share this post


Link to post
Share on other sites
All right, I guess the only way to do it (as close as my original plan) is to overload cast operators. Oh well. How does CString do it?

Share this post


Link to post
Share on other sites


Class Sting
{
private:
char buf[SIZE],buf1[SIZE]; //Should be dynamic actually [grin]

public:
//bla,bla...

operator = (char *);
operator = (int);
friend operator + (Sting *);
}

Sting::operator = (char *s)
{
strcpy(buf,s);
}

Sting::operator = (int x)
{
itoa(buf1,x,9);
strcpy(buf,buf1);
}

Sting::operator + (Sting *s)
{
strcat(buf,s.buf);
}


Make sense?

Share this post


Link to post
Share on other sites
My suggestion would be to overload operator<<, then you could write:

MyStringClassInstance << Int << LPWSTR << Boolean;

Using this method, you can take advantage of std::stringstream to do all the conversion for you, e.g.


#include <iostream>
#include <sstream>

class MyString
{
public:
MyString() {}
~MyString() {}

template <class T>
MyString & operator<<(const T & object)
{
if (!(_buffer << object))
throw std::invalid_argument("Error converting object");
return *this;
}

void Reset() {_buffer.rdbuf()->str("");}

friend std::ostream & operator<<(std::ostream & os, const MyString & str) //Allows use in cout
{
os << str._buffer.str();
return os;
}
private:
std::ostringstream _buffer;
};

int main()
{
int myInt = 20;
float myFloat = 356.2f;

MyString myString;
myString << "Int: " << myInt << "\t\tFloat: " << myFloat;
std::cout << myString << std::endl;

return 0;
}



You can then use _buffer.str() to get access to the std::string, or _buffer.str().c_str() to get a "const char *".

Hope that helps!

Share this post


Link to post
Share on other sites
Quote:
Original post by kernkraft
All right, I guess the only way to do it (as close as my original plan) is to overload cast operators. Oh well. How does CString do it?

I think that CString's source is provided with MFC's source (and therefore with any Visual C++).
In MFC6 CString was defining the method "operator LPCSTR();", but in MFC7 I couldn't find it.
"operator LPCSTR();" is very convenient (allows "const char* str = myCString;"), but you should remember to explicitly call it where it won't be called automatically (like in "printf("%s",(LPCSTR)myCString);").

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!