Jump to content
  • Advertisement
Sign in to follow this  
The C modest god

string into MyClass

This topic is 4414 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 want to convert a string into MyClass, something like this:
class MyClass {
};

void foo (MyClass Arg);

foo ("Text");



How should I do this? Should I use friend in MyClass? should I write some convertor? How is it done? Thanks in advance.

Share this post


Link to post
Share on other sites
Advertisement
A constructor:


class MyClass
{
MyClass(); // normal constructor
MyClass(const std::string & s) // conversion constructor
{
// use s.
};
};




Note:
You could also write a cast operator in std::string


class string
{
operator MyClass() const
{
MyClass mc;
mc.InitSomeMembers(*this); // or whatever.
return mc;
};
};


Of course - you don't have access to std::string class (which is a template, not a class btw) - but if you did, it would be a valid alternative.

So there's a choice beetween writing a cast operator in one class and a construtor in the other class - but in real life you often can't modify the code of both classes - thus giving you no choice.

Share this post


Link to post
Share on other sites
Quote:
Original post by deffer
A constructor:


class MyClass
{
MyClass(); // normal constructor
MyClass(const std::string & s) // conversion constructor
{
// use s.
};
};




Note:
You could also write a cast operator in std::string


class string
{
operator MyClass() const
{
MyClass mc;
mc.InitSomeMembers(*this); // or whatever.
return mc;
};
};


Of course - you don't have access to std::string class (which is a template, not a class btw) - but if you did, it would be a valid alternative.

So there's a choice beetween writing a cast operator in one class and a construtor in the other class - but in real life you often can't modify the code of both classes - thus giving you no choice.


Ok, I got confused.
I actually need to convert from const char[6], for instance.
Because "test" is not a string but rather a char array.
So how would I do this?
Should I simply do such a ctr:

MyClass (const char * Source);

Or should I do something like this:

template <T>
MyClass (const char[T] Source);


What do you say?

Share this post


Link to post
Share on other sites
const char[]
is convertible to
const char*.

So wheater you use:
foo("blah");
or
const char* p = give_me_thy_name();
foo(p);
it will be safely "typecasted" to "const char*".

Also:
string s;
foo(s.c_str());

Share this post


Link to post
Share on other sites
Quote:
Original post by The C modest god
Should I simply do such a ctr:

MyClass (const char * Source);



Yes.

Quote:

Or should I do something like this:

template <T>
MyClass (const char[T] Source);



You could do that too, but only if you take a reference parameter to prevent the array-to-pointer decay. That way you have knowledge of the size of the array (which may or may not be the size of the string, depending on how you do things).

template<size_t N> MyClass(const char (&Source)[N]) ...

That function will obviously not work if you pass a pointer rather than an array.

Share this post


Link to post
Share on other sites
the idea of a templated class based on a number of chars sounds completely crazy - unless you have a very esoteric use for this. Just remember that each template instantiation is the creation of another class. To answer these questions in the future think of the template as a code generation wizard and ask yourself - would I want to generate 3 classes for these cases or write 1 unified class. If you can write the algorithm unified, no need for templates. Templates are for cases where the unified version cannot be efficiently or correctly written (IE taking either int, short, char, double, float, or MyFavoriteClass).

Share this post


Link to post
Share on other sites
Quote:

Just remember that each template instantiation is the creation of another class.


When the class itself is the template, yes. Not when a member function is templated.

Although I agree it's better to not bother with templates in this case.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!