Jump to content
  • Advertisement
Sign in to follow this  
mdias

Assignment operators

This topic is 5410 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

Hi all, I'm having trouble assigning a char* to a custom CString class I made, here's what I do:
class CString
{
public:
    operator = (WCHAR* pwszString){}
};

void main()
{
   CString aaa;
   aaa = L"blah";
}
This doesn't work, the compiler gives me this error: error C2440: '=' : cannot convert from 'wchar_t *' to 'CString *' I guess this is some mistake I'm making because I'm not very familiar with operators. Can anyone help me? Thank you for your time

Share this post


Link to post
Share on other sites
Advertisement
Also, you need a return type on operator = (), which should be a reference to the object, thus CString&

CString& CString::operator = (const WCHAR* pwszString)
{
//Whatever
return *this;
}

Share this post


Link to post
Share on other sites
Why do you use WCHAR instead of the C++ standard wide-character type wchar_t?

Show the implementation of your operator=.

Share this post


Link to post
Share on other sites
Quote:
Original post by Fruny
Why do you use WCHAR instead of the C++ standard wide-character type wchar_t?

Show the implementation of your operator=.


what's the difference between WCHAR and wchar_t?

the code that generates the error is the code that I posted, I did nothing else than what I've shown you guys.

Thanks to everyone who replied so far.
And again, thank you for your time.

Share this post


Link to post
Share on other sites
Quote:
Original post by Kamikaze15
what's the difference between WCHAR and wchar_t?
WCHAR is a typedef for wchar_t particular to the Win32 API.

The code you've shown has an empty definition for operator=. Show the actual code.

Share this post


Link to post
Share on other sites
Quote:
Original post by Kamikaze15
Quote:
Original post by Fruny
Why do you use WCHAR instead of the C++ standard wide-character type wchar_t?

Show the implementation of your operator=.


what's the difference between WCHAR and wchar_t?


not much except that WCHAR is a win32 typedef of wchar_t, you don't wont to make a depandance on on win32 header files just for a custom string class.

anyways change it to:


class CString {

public:
CString& operator=(const wchar_t* const ls) {
/* ..... */
return *this;
}
};


Your probably wondering why a constant pointer to constant data.

What ever you do you don't wont to just assign pointers, especially from string literals because they are statically allocated, if your destructor calls delete[] and the pointer points to statically allocated memory bang your dead.

Also modifying a string literals through a pointer has undefined behaviour, so make sure you do a deep copy.

You may wont to consider making CString's representation with an intrusive reference count with copy-on-write schemtics to avoid alot of redundancy.

Share this post


Link to post
Share on other sites
Quote:
Original post by Oluseyi
Quote:
Original post by Kamikaze15
what's the difference between WCHAR and wchar_t?
WCHAR is a typedef for wchar_t particular to the Win32 API.

The code you've shown has an empty definition for operator=. Show the actual code.


Thank you for clarifying me, the error ocurred even with the empty body.
Now I solved the error and it was MY mistake. I was doing this:
CString* aaa = L"blah";

instead of this:
CString aaa = L"blah";


I apologise for having wasted the time you could've used to help others.
Anyways, thanks to everyone who posted.

Sorry for any spelling or gramatic errors. :)

Share this post


Link to post
Share on other sites
snk_kid: yup, I'm aware of the problems with deleting staticaly allocated strings and stuff but thanks for warning me anyway :)

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!