473,395 Members | 1,653 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Is this the right way to redirect output from a class to another?

Hello,

I have a class, transCore, that does certain work, and by default, prints
its progress to the stand output.

Later, I will to write a GUI class that encapsulate the transCore class. I
would like to redirect/intercept the output of transCore class to this GUI
class. I wrote a sample program that demonstrate how I did it, but I am not
sure if it's any good. Can anyone critic my code? If there's any
non-standard conforming code or a better way to redirect output, please let
me know. Thank you.

/* program start */

#include<iostream>
#include<string>
using namespace std;
class BaseDisplayFunctor
{
private:
public:
virtual void print(const string &)=0;
};

template<class T>
class MyDisplayFunctor:public BaseDisplayFunctor
{
private:
//object pointer
T *object_ptr;
//function pointer to display
void (T::*func_ptr)(const string &);
public:
MyDisplayFunctor(T* obj, void (T::*Display)(const string&))
{
object_ptr = obj;
func_ptr = Display;
}
virtual void print(const string &s)
{
(*object_ptr.*func_ptr)(s);
}
~MyDisplayFunctor(){}
};
/*
Class: TRANSCORE

PURPOSE: this class will does all works and print progress to
the screen
*/
class TransCore
{
private:
BaseDisplayFunctor *BDF;
void print(const string& s)
{
if(BDF != NULL)
BDF->print(s);
else
cout<< s;
}
public:
TransCore(BaseDisplayFunctor *display){BDF = display;}
void dosomething()
{
print("does some work#1");
print("does some work#2");
}
~TransCore(){};
};
/*
Class: TEST

PURPOSE: Demonstrate how I would like to redirect simple output
*/
class Test
{
private:
TransCore *transCore;
MyDisplayFunctor<Test> *myDF;
void redirected_output(const string &s)
{
/*
Supposed to intercept whatever ouput from TRANSCORE to
a GUI display
*/
cout<<"Redirected: " <<s<<endl;
}

public:
Test()
{
myDF = new MyDisplayFunctor<Test>(this, &Test::redirected_output);
transCore = new TransCore(myDF);
transCore->dosomething();
}
~Test(){delete transCore; delete myDF;}
};

int main(int argc, char **argv)
{
Test wow;
system("pause"); //prevent windowXP from closing my console
return 0;
}

/* program end */
Jul 22 '05 #1
2 1873

"Jason" <JC********@hotmail.com> wrote in message
news:eluWc.211$2F.144@trnddc05...
Hello,

I have a class, transCore, that does certain work, and by default, prints
its progress to the stand output.

Later, I will to write a GUI class that encapsulate the transCore class. I
would like to redirect/intercept the output of transCore class to this GUI
class. I wrote a sample program that demonstrate how I did it, but I am not sure if it's any good. Can anyone critic my code? If there's any
non-standard conforming code or a better way to redirect output, please let me know. Thank you.

/* program start */

#include<iostream>
#include<string>
using namespace std;
class BaseDisplayFunctor
{
private:
public:
virtual void print(const string &)=0;
};

template<class T>
class MyDisplayFunctor:public BaseDisplayFunctor
{
private:
//object pointer
T *object_ptr;
//function pointer to display
void (T::*func_ptr)(const string &);
public:
MyDisplayFunctor(T* obj, void (T::*Display)(const string&))
{
object_ptr = obj;
func_ptr = Display;
}
virtual void print(const string &s)
{
(*object_ptr.*func_ptr)(s);
}
~MyDisplayFunctor(){}
};
/*
Class: TRANSCORE

PURPOSE: this class will does all works and print progress to
the screen
*/
class TransCore
{
private:
BaseDisplayFunctor *BDF;
void print(const string& s)
{
if(BDF != NULL)
BDF->print(s);
else
cout<< s;
}
public:
TransCore(BaseDisplayFunctor *display){BDF = display;}
void dosomething()
{
print("does some work#1");
print("does some work#2");
}
~TransCore(){};
};
/*
Class: TEST

PURPOSE: Demonstrate how I would like to redirect simple output
*/
class Test
{
private:
TransCore *transCore;
MyDisplayFunctor<Test> *myDF;
void redirected_output(const string &s)
{
/*
Supposed to intercept whatever ouput from TRANSCORE to
a GUI display
*/
cout<<"Redirected: " <<s<<endl;
}

public:
Test()
{
myDF = new MyDisplayFunctor<Test>(this, &Test::redirected_output);
transCore = new TransCore(myDF);
transCore->dosomething();
}
~Test(){delete transCore; delete myDF;}
};

int main(int argc, char **argv)
{
Test wow;
system("pause"); //prevent windowXP from closing my console
return 0;
}

/* program end */


#include <fstream>
#include <iostream>
#include <ostream>
#include <string>

void print(std::ostream& os, const std::string& s)
{
os << s;
}

int main()
{
std::ifstream ifs("filename");
std::string s1("Hello");
print(std::cout, s1); // to standard output
print(ifs, s1); // to file
return 0;
}

-Mike
Jul 22 '05 #2
"Jason" <JC********@hotmail.com> wrote in message news:<eluWc.211$2F.144@trnddc05>...
Hello,

I have a class, transCore, that does certain work, and by default, prints
its progress to the stand output.

Later, I will to write a GUI class that encapsulate the transCore class. I
would like to redirect/intercept the output of transCore class to this GUI
class.
Two easy ways to do this are 1) have a 'setStream' function which
allows you to change the output stream from 'std::cout' (the default),
to some other 'std::ostream', or 2) set the output stream in a
constructor.
I wrote a sample program that demonstrate how I did it, but I am not
sure if it's any good. Can anyone critic my code?
Sure.

[snip] #include<iostream>
#include<string>
using namespace std;
class BaseDisplayFunctor
{
private:
public:
virtual void print(const string &)=0;
};
This is not a functtor. AFAIK, "functor" is defined (largely) by the
presence of 'operator()'. In any case, inheritance is not the right
approach to solve your problem.
template<class T>
class MyDisplayFunctor:public BaseDisplayFunctor
{
private:
//object pointer
T *object_ptr;
//function pointer to display
void (T::*func_ptr)(const string &);
public:
MyDisplayFunctor(T* obj, void (T::*Display)(const string&))
This is just complicated. Pointers to functions are often helped by
typedefs.
{
object_ptr = obj;
func_ptr = Display;
Your naming scheme is wildly inconsistent. Typically, people name data
members with a leading or trailing underscore, or a mnemonic, such as
'd_'. So this would be written as

d_obj_p = obj; // d_obj_p is a data member of a pointer type
d_func = func; // d_func is a function pointer (note, no '_p')

These conventions obviate the need for coming up with funny argument
names, and make the code much more readable.
}
virtual void print(const string &s)
{
(*object_ptr.*func_ptr)(s);
You can take advantage of a language feature which allows you treat
pointers to functions as functions when making function calls:

object_ptr->func_ptr(s);
}
~MyDisplayFunctor(){}
};
/*
Class: TRANSCORE

PURPOSE: this class will does all works and print progress to
the screen
*/
class TransCore
{
private:
BaseDisplayFunctor *BDF;
void print(const string& s)
{
if(BDF != NULL)
BDF->print(s);
else
cout<< s;
You should flush the stream with 'std::flush' or 'std::endl'.
}
public:
TransCore(BaseDisplayFunctor *display){BDF = display;}
void dosomething()
{
print("does some work#1");
print("does some work#2");
}
~TransCore(){};
};
/*
Class: TEST

PURPOSE: Demonstrate how I would like to redirect simple output
*/


Why is this a class? Just put all these mechanics into 'main'.

HTH, /david
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Hal Vaughan | last post by:
I have a Java program, composed of a number of classes, that I've been running as a command line program. It logs everything it does and, as it logs each event, it also prints that line of info to...
3
by: Frank Rizzo | last post by:
Is there anyway to transparently redirect the output of the Debug.WriteLine statement from the Debug window to something else such as a console or a file? Thanks
8
by: Andreas Klemt | last post by:
Hello, I get this error Message "cannot redirect after http headers have been sent" when I do this response.redirect ("home.aspx") How can I find out with vb.net if already a http header has...
13
by: Tim | last post by:
Hello, Is there a way to "cancel" a response.Redirect? For example, in the code below, could I insert anything in the Catch statement that would cancel the redirect and resume flow after the...
2
by: Ivan Lam | last post by:
Hi all, Thanks for reading my post!!! I am facing a problem that I cannot redirect StandartOutput and StandardInput at the same time without closing the executive. Actually, I have a...
8
by: Mantorok | last post by:
Hi all When I start a new thread that tries to call: HttpContext.Current.Response.Redirect() It fails as Current returns null, is there anyway to access the current httpcontext from within...
1
by: Petterson Mikael | last post by:
Hi, I am using xalan. In my stylesheet I have the following snippet to write to std out when a specific error occurs. <xsl:template name="class_error"> <xsl:param name="class"/> <xsl:param...
1
by: nettleby | last post by:
The Microsoft site has confused me on this issue: http://www.microsoft.com/technet/prodtechnol/windows2000serv/reskit/iisbook/c06_redirection.mspx?mfr=true Says that: "ASP automatically discards...
2
by: JRough | last post by:
I have this code that switches templates depending on if the user fills in a form with a request. The request asks for the $mark & $number. If that request gets input then it displays a list...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.