|
-
October 21st, 2009, 03:48 AM
#1
Strange code in C++. Please help
Hi All,
I was writing a code. Accidently I came across a situation where I assigned string variable to a user- defined class. And strange thing is that string was filled with proper value.
Please see the code below:-
#include<iostream>
using namespace std;
class JStringToCharConstStar
{
public:
// constructor / destructor
JStringToCharConstStar (int a,
std::string j_string);
~JStringToCharConstStar (void);
// accessor
operator char const * (void) const
{
return(m_string);
}
private:
// intentionally unimplemented
JStringToCharConstStar (JStringToCharConstStar const &);
JStringToCharConstStar & operator = (JStringToCharConstStar const &);
// private data members
int abc;
std::string m_j_string;
char const * m_string;
}; // class JStringToCharConstStar
JStringToCharConstStar::
JStringToCharConstStar (int a,
std::string j_string)
:abc(a),
m_j_string(j_string),
m_string(NULL)
{
m_string = m_j_string.c_str();
if (NULL == m_string)
{
cout<<"GetStringUTFChars failed.\n";
}
}
JStringToCharConstStar::
~JStringToCharConstStar (void)
{
if (NULL != m_string)
{
cout<<"Destructor called";
}
}
int main()
{
std::string a;
char const *k = "vinit";
a = k;
cout<<"a="<<a<<"\n";
JStringToCharConstStar jstring(5,a);
std::string b;
b = jstring;
cout<<"b="<<b;
return 0;
}
output is
a=vinit
b=vinit
Question :- How come this is possible. I have assigned string b to object of class JStringToCharConstStar .
I did overloaded char const * to use it but it seems that I donot need that.
Can someone please explain me that is the issue here and if possible point me to some tutorial.
Thanks in advance for your help.
Thanks
Vinit
-
October 21st, 2009, 04:09 AM
#2
Re: Strange code in C++. Please help
you mean you assigned a string variable to your class and it accepted the string? I don't see that happening because you didn't define the operator = to do so and I don't see
jstring = b
anywhere in your code.
I see the opposite
b = jstring
which makes string class be assigned from your class and that is possible with the cast operator you have in your class
unless I am not understanding you. also please learn how to use the code tag. aka
0100 0111 0110 1111 0110 0100 0010 0000 0110 1001 0111 0011 0010 0000 0110 0110 0110 1111 0111 0010
0110 0101 0111 0110 0110 0101 0111 0010 0010 0001 0010 0001 0000 0000 0000 0000 0000 0000 0000 0000
-
October 21st, 2009, 06:28 AM
#3
Re: Strange code in C++. Please help
Hi Joeman, First of all thanks for your quick reply.
I have used b= jstring, where jstring is a object of JStringToCharConstStar class.
It means ctor of string class will be called to assign the value to b (using '=' assignment operator of string class).
how can we initialize a string variable using object of some other class. String class doesnot take any user defined class/datatype to assign any value to its object.
Here b is assigned to value which is set in JStringToCharConstStar class object.
I have just declared a assignment operator for class JStringToCharConstStar but not defined it.
And for assigning the value to string variable b I had overloaded char const * but it seems to be meaning less. Like I would have implemented like
b= (char const *) jstring; --------(1)
instead of
b = jstring. ----------------(2)
According to my understanding (2) should give some type conversion error. And (1) should have been its solution.
But while implementing I found no difference in (1) and (2) in terms of results.
This is a complete running code and you can try running in your system as well.
Thanks again for considering my post.
Thanks
Vinit
-
October 21st, 2009, 06:53 AM
#4
Re: Strange code in C++. Please help
sorry for this message. just truying why my reply is not posted earlier.
-
October 21st, 2009, 06:54 AM
#5
Re: Strange code in C++. Please help
Please post your code in [ code ] tags. Also it would help if you'd be more clear about what you are trying to do/expecting to happen.
 Originally Posted by VinitChanduka
Question :- How come this is possible. I have assigned string b to object of class JStringToCharConstStar.
I did overloaded char const * to use it but it seems that I donot need that.
You do need the overloaded operator, because this is exactly what's happening. The compiler searches for a suitable assignment operator for std::string and finds
Code:
std::string& std::string::operator=(const char *)
as the only suitable candidate. As your class is implicitly convertable to a const char * via the operator you implemented, it will use that conversion and assign the returned value to the string. If you don't want this to happen, add the "explicit" keyword in front of your operator.
More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf
Premature optimization is the root of all evil --Donald E. Knuth
Please read Information on posting before posting, especially the info on using [code] tags.
-
October 21st, 2009, 06:56 AM
#6
Re: Strange code in C++. Please help
Joeman,Thanks for your quick reply.
Let me reframe my question once again.
In the statement b = jstring; I am assigning the value to string class "b" using user defined class JStringToCharConstStar.
According to me above code snippet is not valid because for initializing the string variable you should not use user defined datatype/ class because string class doesnot have proper constructor to handle this.
b=jstring will use string assignment operator. But since jstring is user-written class this should not be valid.(as no assignment operator has been defined for JStringToCharConstStar class object).
I was expecting
b=jstring --------------- (1)
should give a type conversion error and the proper solution for this is
b=(char const *) jstring -----------(2)
thats the reason why I had overloaded the operator char const * in the class JStringToCharConstStar
But while running the code I find no difference in (1) and (2) according to results. Both gives the same result.
it is true that I have declared the assignment operator for class JStringToCharConstStar but I didnot defined it.
My question is why staement (1) have not given me any error.
The above code are complete and can easily be run at any system.
Thanks again for considering my post.
Thanks
Vinit
-
October 21st, 2009, 07:04 AM
#7
Re: Strange code in C++. Please help
Hey Treuss, Thanks for your reply.
It will be great if you can explain in more details. I didnot understood your message completely.
I didnot wanted compiler to use any of its default assignment operator thats why in my code I declared a assignment operator.
Moreever what I feel that in this code
b = jstring;----------------(1)
since b is a std::string class object,so string class's assignment operator will be used not of class written by me.
Please correct me if I am wrong here.
Please elaborate on how compiler decides which default compiler to use and if possible point me to some tutorial.
Thanks again for replying to my message.
Thanks
Vinit
-
October 21st, 2009, 07:06 AM
#8
Re: Strange code in C++. Please help
sending the code once again
[#include<iostream>
using namespace std;
class JStringToCharConstStar
{
public:
// constructor / destructor
JStringToCharConstStar (int a,
std::string j_string);
~JStringToCharConstStar (void);
// accessor
operator char const * (void) const
{
return(m_string);
}
private:
// intentionally unimplemented
JStringToCharConstStar (JStringToCharConstStar const &);
JStringToCharConstStar & operator = (JStringToCharConstStar const &);
// private data members
int abc;
std::string m_j_string;
char const * m_string;
}; // class JStringToCharConstStar
JStringToCharConstStar::
JStringToCharConstStar (int a,
std::string j_string)
:abc(a),
m_j_string(j_string),
m_string(NULL)
{
m_string = m_j_string.c_str();
if (NULL == m_string)
{
cout<<"GetStringUTFChars failed.\n";
}
}
JStringToCharConstStar::
~JStringToCharConstStar (void)
{
if (NULL != m_string)
{
cout<<"Destructor called";
}
}
int main()
{
std::string a;
char const *k = "vinit";
a = k;
cout<<"a="<<a<<"\n";
JStringToCharConstStar jstring(5,a);
std::string b;
b = jstring;
cout<<"b="<<b;
return 0;
}
]
output:
a==vinit
b==vinit
-
October 21st, 2009, 07:13 AM
#9
Re: Strange code in C++. Please help
 Originally Posted by VinitChanduka
I was expecting
b=jstring --------------- (1)
should give a type conversion error and the proper solution for this is
b=(char const *) jstring -----------(2)
thats the reason why I had overloaded the operator char const * in the class JStringToCharConstStar
But while running the code I find no difference in (1) and (2) according to results. Both gives the same result.
That's because the compiler performs an implicit cast from JStringToCharConstStar to const char*. Since std::string has an operator =(const char*) and JStringToCharConstStar is convertible to const char*, the compiler knows how to do the assignment.
it is true that I have declared the assignment operator for class JStringToCharConstStar but I didnot defined it.
Nothing is assigned to a JStringToCharConstStar in your code.
Edit: you're still not using the code tags correctly. It's really difficult to read your code this way. Please read the FAQ about it.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
October 21st, 2009, 07:26 AM
#10
Re: Strange code in C++. Please help
b=jstring will use string assignment operator. But since jstring is user-written class this should not be valid.(as no assignment operator has been defined for JStringToCharConstStar class object).
I will try to clear most of your confusion up.
std::string knows const char* and your class can cast to const char*, so this is done automatically for you.
it is true that I have declared the assignment operator for class JStringToCharConstStar but I didnot defined it.
Yep
0100 0111 0110 1111 0110 0100 0010 0000 0110 1001 0111 0011 0010 0000 0110 0110 0110 1111 0111 0010
0110 0101 0111 0110 0110 0101 0111 0010 0010 0001 0010 0001 0000 0000 0000 0000 0000 0000 0000 0000
-
October 21st, 2009, 07:32 AM
#11
Re: Strange code in C++. Please help
I have joined this site today. I hope I got right this time.
Code:
#include<iostream>
using namespace std;
class JStringToCharConstStar
{
public:
// constructor / destructor
JStringToCharConstStar (int a,
std::string j_string);
~JStringToCharConstStar (void);
// accessor
operator char const * (void) const
{
return(m_string);
}
private:
// intentionally unimplemented
JStringToCharConstStar (JStringToCharConstStar const &);
JStringToCharConstStar & operator = (JStringToCharConstStar const &);
// private data members
int abc;
std::string m_j_string;
char const * m_string;
}; // class JStringToCharConstStar
JStringToCharConstStar::
JStringToCharConstStar (int a,
std::string j_string)
:abc(a),
m_j_string(j_string),
m_string(NULL)
{
m_string = m_j_string.c_str();
if (NULL == m_string)
{
cout<<"GetStringUTFChars failed.\n";
}
}
JStringToCharConstStar::
~JStringToCharConstStar (void)
{
if (NULL != m_string)
{
cout<<"Destructor called";
}
}
int main()
{
std::string a;
char const *k = "vinit";
a = k;
cout<<"a="<<a<<"\n";
JStringToCharConstStar jstring(5,a);
std::string b;
b = jstring;
cout<<"b="<<b;
return 0;
}
My question is that auto conversion is compiler dependent? Is there any better way to implemet this?
because the above code compiled successfully in windows compiler, my local Linux compiler( Centos machine)
But it failed my build in Linux. I am sorry I dont know the compiler name as we have different build team out here.
Thanks for your reply.
-
October 21st, 2009, 02:20 PM
#12
Re: Strange code in C++. Please help
 Originally Posted by VinitChanduka
My question is that auto conversion is compiler dependent?
No. All ANSI compatible C++ compilers must follow the C++ specification as to how to choose the correct function.
because the above code compiled successfully in windows compiler, my local Linux compiler( Centos machine)
Windows and Linux are operating systems -- they are not compilers.
You need to specify the C++ compiler name and version that you used, not the OS you're running on.
I am sorry I dont know the compiler name as we have different build team out here.
I would think it would be a requirement for you to know exactly what other people on your team are using.
Regards,
Paul McKenzie
-
October 21st, 2009, 03:34 PM
#13
Re: Strange code in C++. Please help
 Originally Posted by VinitChanduka
I have joined this site today. I hope I got right this time.
Well, half way. You should paste your properly indented code between the tags.
 Originally Posted by VinitChanduka
My question is that auto conversion is compiler dependent?
No, it's not. At least not in theory. The compiler does up to one implizit conversion (in your case from class JStringToCharConstStar to const char *) if that is needed to make the code valid.
The problem with your example is probably, that std::string is not normal class, but a template instantiation (of std::basic_string). There's been a discussion on Codeguru a couple years back (which I don't remember in detail), but there was something that g++ does not always automatically use implicit conversions (even though I am unable to reproduce it now).
 Originally Posted by VinitChanduka
Is there any better way to implemet this?
The best way to deal with implicit conversions is to avoid them. Add a member function toString() to your class that returns the desired std::string object.
More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf
Premature optimization is the root of all evil --Donald E. Knuth
Please read Information on posting before posting, especially the info on using [code] tags.
-
October 21st, 2009, 04:21 PM
#14
Re: Strange code in C++. Please help
 Originally Posted by VinitChanduka
IMy question is that auto conversion is compiler dependent? Is there any better way to implemet this?
The better way is what treuss suggested, and it's very simple -- don't code implicit conversions.
Have a function called (again as treuss mentioned), toString(), toInt(), toWhatever() when wanting to do a conversion. For example, note that std::string doesn't have a const char* conversion operator, instead you call c_str().
Implicit conversions can lead to programs that are
1) Hard to debug
2) Hard to understand
3) Undesirable side-effects
4) Could cause code to execute slower due to calls to convert.
5) Code that you thought gets executed doesn't, and code you thought that didn't execute gets executed, all due to the conversion being done.
6) Code sometimes will not compile due to "ambiguous function call" errors. Then it takes you hours to sort out that mess, and who knows what other side-effects you've introduced by trying to get the code to compile.
Yes, conversion operators are available in C++, but I highly recommend not to use them -- use "real" functions, where the caller knows exactly what is going to be done.
Regards,
Paul McKenzie
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|