Convert custom String to C-Style string

May 7, 2016 at 10:23pm
Hi.

I have my own GString class, built using a char* called mainString

I have these methods to allow conversion from my GString class to char*

1
2
3
4
5
6
7
8
9
10

GString::operator const char*() const
{
	return mainString;
}

GString::operator char*() const
{
	return mainString;
}


The problem is, whenever I try to call my static function:

1
2
3
4
5
6
7
8
9
(static) bool GString::StrStr(GString source, GString substr)
{ 
   ... 
}

int main()
{
   bool check = GString::StrStr("hello", "byebye");
}


The compiler claims:


Error C2664 'bool GString::StrStr(GString,GString)': cannot convert argument 1 from 'const char [14]' to 'GString'


I have an explicit constructor to construct a GString starting from a char*

 
explicit GString(const char* CstyleString = "");
Last edited on May 7, 2016 at 10:24pm
May 8, 2016 at 1:16pm
I have an explicit constructor to construct a GString starting from a char*

Yes, but you are asking the compiler to use an implicit constructor to make GStrings from "hello" and "byebye".
May 8, 2016 at 5:26pm
explicit means that your constructor will not be considered for an implicit conversion. It must be used explicitly, and since you aren't using your constructor explicitly here, the compiler cannot use it implicitly.

Yes, but you are asking the compiler to use an implicit constructor to make GStrings from "hello" and "byebye".

Usually when one refers to an implicit constructor, one refers to one implicitly generated by the compiler: not one that is not designated explicit.

May 8, 2016 at 5:54pm
A constructor declared without the function-specifier explicit specifies a conversion from the types of its parameters to the type of its class. Such a constructor is called a converting constructor. - IS
Topic archived. No new replies allowed.