C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Reusing the algorithm in >> without modifying the class

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
blrmaani
Guest





PostPosted: Tue Jul 20, 2004 7:55 am    Post subject: Reusing the algorithm in >> without modifying the class Reply with quote



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





PostPosted: Tue Jul 20, 2004 6:24 pm    Post subject: Re: Reusing the algorithm in >> without modifying the class Reply with quote



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





PostPosted: Tue Jul 20, 2004 6:24 pm    Post subject: Re: Reusing the algorithm in >> without modifying the class Reply with quote



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





PostPosted: Tue Jul 20, 2004 9:25 pm    Post subject: Re: Reusing the algorithm in >> without modifying the class Reply with quote

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





PostPosted: Tue Jul 20, 2004 9:27 pm    Post subject: Re: Reusing the algorithm in >> without modifying the class Reply with quote

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





PostPosted: Wed Jul 21, 2004 11:51 am    Post subject: Re: Reusing the algorithm in >> without modifying the class Reply with quote

Hi,
blrmaani wrote:
Quote:
main()

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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.