 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
blrmaani Guest
|
Posted: Tue Jul 20, 2004 7:55 am Post subject: Reusing the algorithm in >> without modifying the class |
|
|
I have a class ( part of a library which is not modifiable ).
class myclass
{
public:
myclass() { }
friend istream& operator>>(istream&, myclass&);
private:
int mydata_;
};
istream& operator>>(istream& is, myclass& myobj)
{
// some algorithm to parse the input string and
// store it in mydata_
}
Question:
I want to use 'myclass' class but I want to test it with a string as
a input so that the algorithm in >> is executed. I know that I can
do this:
main()
{
myclass myobj;
cin >> myobj;
}
But if I want to give a input in the form of a string, what changes
are required? I do not have control to modify the original class and
I do not want to repeat the algorithm in >>.
thanks in advance
blrmaani.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Carl Barron Guest
|
Posted: Tue Jul 20, 2004 6:24 pm Post subject: Re: Reusing the algorithm in >> without modifying the class |
|
|
In article <15dfd9f3.0407191355.7db46450 (AT) posting (DOT) google.com>, blrmaani
<blrmaani (AT) yahoo (DOT) com> wrote:
| Quote: | Question:
I want to use 'myclass' class but I want to test it with a string as
a input so that the algorithm in >> is executed. I know that I can
do this:
main()
{
myclass myobj;
cin >> myobj;
}
But if I want to give a input in the form of a string, what changes
are required? I do not have control to modify the original class and
I do not want to repeat the algorithm in >>.
use an std::istringstream, like this: |
#include <sstream>
#include <string>
using namespace std;
int main()
{
myclass mydata;
string a(...);
istringstream in(a);
in >> mydata;
// ...
}
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ulrich Eckhardt Guest
|
Posted: Tue Jul 20, 2004 6:24 pm Post subject: Re: Reusing the algorithm in >> without modifying the class |
|
|
blrmaani wrote:
| Quote: | class myclass
{
public:
myclass() { }
friend istream& operator>>(istream&, myclass&);
private:
int mydata_;
};
istream& operator>>(istream& is, myclass& myobj)
{
// some algorithm to parse the input string and
// store it in mydata_
}
Question:
I want to use 'myclass' class but I want to test it with a string as
a input so that the algorithm in >> is executed.
|
You are looking for stringstreams, see <sstream>.
Uli
--
FAQ: http://parashift.com/c++-faq-lite/
/* bittersweet C++ */
default: break;
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Maciej Sobczak Guest
|
Posted: Tue Jul 20, 2004 9:25 pm Post subject: Re: Reusing the algorithm in >> without modifying the class |
|
|
Hi,
blrmaani wrote:
| Quote: | I want to use 'myclass' class but I want to test it with a string as
a input so that the algorithm in >> is executed. I know that I can
do this:
main()
{
myclass myobj;
cin >> myobj;
}
But if I want to give a input in the form of a string, what changes
are required? I do not have control to modify the original class and
I do not want to repeat the algorithm in >>.
|
Luckily, you do not have to perform any changes in the original code.
This is a Good Thing and is a result of the IOStream library design.
You can substitute just about anything that provides the istream
interface; istringstream is a good candidate when in-memory strings are
needed:
#include <sstream>
int main()
{
std::istringstream ss("some string that you want to test");
myclass myobj;
ss >> myobj;
}
That's all, but look into your docs to see what other members
istringstream has - they may be useful.
This way, you can not only reuse the algorithm, but you can use this
technique to write *automatic* tests for your own algorithms.
Substituting istringstream with prepared input, and possibly retrieving
the results with ostringstream (if your algorithm produces results to
ostream&) can be a very good idea in such tests.
--
Maciej Sobczak : http://www.msobczak.com/
Programming : http://www.msobczak.com/prog/
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Sven Rosiers Guest
|
Posted: Tue Jul 20, 2004 9:27 pm Post subject: Re: Reusing the algorithm in >> without modifying the class |
|
|
blrmaani wrote:
| Quote: | I have a class ( part of a library which is not modifiable ).
class myclass
[SNIP]
Question:
I want to use 'myclass' class but I want to test it with a string as
a input so that the algorithm in >> is executed. I know that I can
do this:
main()
{
myclass myobj;
cin >> myobj;
}
But if I want to give a input in the form of a string, what changes
are required? I do not have control to modify the original class and
I do not want to repeat the algorithm in >>.
|
stringstream to the rescue!
#include <sstream>
int main() {
myclass myobj;
istringstream input("blablabla");
input >> myobj;
}
Regards,
Sven R.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
dietmar_kuehl@yahoo.com Guest
|
Posted: Wed Jul 21, 2004 11:51 am Post subject: Re: Reusing the algorithm in >> without modifying the class |
|
|
Hi,
blrmaani wrote:
BTW, there is no "implict int"-rule in C++, i.e. the above line
shall read
int main()
instead.
| Quote: | {
myclass myobj;
cin >> myobj;
}
But if I want to give a input in the form of a string, what changes
are required?
|
You need to change the source of input to be a stream reading from
a string. The standard C++ library supports such a class in the
header <sstream>:
#include <sstream>
int main() {
myclass myobj;
std::istringstream stream("whatever");
stream >> myobj;
}
--
<mailto:dietmar_kuehl (AT) yahoo (DOT) com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|