K
key9
Hi All
I defined a class "RootElement"
And want the class can be inherit tree's root.
The main idea is using std::string as the "config" of the instance
the module pass the compile with the help of kindness guys here,but when I
put it in main() , it can not work
Here's the code
*********************************************//root_element.hpp
#ifndef _ROOTELEMENT_
#define _ROOTELEMENT_
#include <string>
class RootElement{
public:
/* just for operate overload */
virtual
RootElement& operator<< (std::string& str);
public:
/* overloadable function open to user*/
// PS: & means reference , change this will dieectly change to obj
/* in */
//accept input string
int acceptiStr(const std::string& istr);
// check input string is legal
int checkiStr(const std::string& istr);
/* out */
virtual std::string outputoStr();
protected:
/*me and my son can use it ,user can not!*/
// any string , just check it! 0 - OK , 1 - error
virtual
int chkiStr(const std::string& istr);
// str must be correct ,must be error check before using
virtual
int hndValidiStr(const std::string& istr);
protected:
// check single pos , inherit that we can simply define one way in our son
virtual
int chkEachiStr(const std::string& istr);
// give out an way can simply handler string's 1 char
virtual
int hndEachiStr(const std::string& istr);
};
#endif /* _ROOTELEMENT_ */
**************************************//root_element.cpp
#include <iostream>
#include "root_element.hpp"
#include <sstream>
RootElement&
RootElement::
operator<< (std::string& str)
{
std::stringstream Temp;
Temp << str;
acceptiStr(Temp.str ());
}
int
RootElement::
acceptiStr(const std::string& istr)
{
if (chkiStr(istr)){
hndValidiStr(istr);
return 0;
}
else{
return 1;
}
}
int
RootElement::
chkiStr(const std::string& istr)
{
std::string::size_type sz_;
std::string::size_type sz_E;
std::string temp_str;
sz_E = istr.length();
sz_ = 0;
while( sz_ != sz_E ){
temp_str = istr.at(sz_);
if (chkEachiStr(temp_str)){
sz_ ++;
}
else
return 1;
}
return 0;
}
int
RootElement::
hndValidiStr(const std::string& istr)
{
std::string::size_type sz_;
std::string::size_type sz_E;
std::string temp_str;
sz_E = istr.length();
for (sz_ = 0; sz_ != sz_E ; sz_++){
temp_str = istr.at(sz_);
hndEachiStr(temp_str);
}
return 0;
}
int
RootElement::
hndEachiStr(const std::string& istr){
std::cout <<" RootElement: hndEachiStr is running! " << std::endl;
}
//testmain.cpp***************************************************
#include<iostream>
#include<string>
#include "root_element.hpp"
using namespace std;
RootElement re_;
int main(int argc, char* argv[])
{
string str = " This is test sample";
re_ << str;
return 0;
}
Thank you very much!
key9
I defined a class "RootElement"
And want the class can be inherit tree's root.
The main idea is using std::string as the "config" of the instance
the module pass the compile with the help of kindness guys here,but when I
put it in main() , it can not work
Here's the code
*********************************************//root_element.hpp
#ifndef _ROOTELEMENT_
#define _ROOTELEMENT_
#include <string>
class RootElement{
public:
/* just for operate overload */
virtual
RootElement& operator<< (std::string& str);
public:
/* overloadable function open to user*/
// PS: & means reference , change this will dieectly change to obj
/* in */
//accept input string
int acceptiStr(const std::string& istr);
// check input string is legal
int checkiStr(const std::string& istr);
/* out */
virtual std::string outputoStr();
protected:
/*me and my son can use it ,user can not!*/
// any string , just check it! 0 - OK , 1 - error
virtual
int chkiStr(const std::string& istr);
// str must be correct ,must be error check before using
virtual
int hndValidiStr(const std::string& istr);
protected:
// check single pos , inherit that we can simply define one way in our son
virtual
int chkEachiStr(const std::string& istr);
// give out an way can simply handler string's 1 char
virtual
int hndEachiStr(const std::string& istr);
};
#endif /* _ROOTELEMENT_ */
**************************************//root_element.cpp
#include <iostream>
#include "root_element.hpp"
#include <sstream>
RootElement&
RootElement::
operator<< (std::string& str)
{
std::stringstream Temp;
Temp << str;
acceptiStr(Temp.str ());
}
int
RootElement::
acceptiStr(const std::string& istr)
{
if (chkiStr(istr)){
hndValidiStr(istr);
return 0;
}
else{
return 1;
}
}
int
RootElement::
chkiStr(const std::string& istr)
{
std::string::size_type sz_;
std::string::size_type sz_E;
std::string temp_str;
sz_E = istr.length();
sz_ = 0;
while( sz_ != sz_E ){
temp_str = istr.at(sz_);
if (chkEachiStr(temp_str)){
sz_ ++;
}
else
return 1;
}
return 0;
}
int
RootElement::
hndValidiStr(const std::string& istr)
{
std::string::size_type sz_;
std::string::size_type sz_E;
std::string temp_str;
sz_E = istr.length();
for (sz_ = 0; sz_ != sz_E ; sz_++){
temp_str = istr.at(sz_);
hndEachiStr(temp_str);
}
return 0;
}
int
RootElement::
hndEachiStr(const std::string& istr){
std::cout <<" RootElement: hndEachiStr is running! " << std::endl;
}
//testmain.cpp***************************************************
#include<iostream>
#include<string>
#include "root_element.hpp"
using namespace std;
RootElement re_;
int main(int argc, char* argv[])
{
string str = " This is test sample";
re_ << str;
return 0;
}
Thank you very much!
key9