指向类的指针上的按位运算符

Bitwise operator on pointer to class?

本文关键字:运算符 指针      更新时间:2023-10-16

我花了一天时间挖掘项目中以前一个人的遗留代码,并且我还搜索了按位运算符,我仍然无法清楚地理解按位操作器的代码行:

input >> *graph;

我可以编译和运行,但是,我为您的评论放置了 2 个 printf 函数:它可以打印"11111",之后是"4",但永远无法打印"2222",所以一定有问题按位运算符行。如何解决这个问题?

*graph是指向类 GGraph 对象的指针:

class GGraph{
public:
GGraph();
~GGraph();
void addNode ( GNodeData nodedata, GNodeOrGroup orgroup = GNOGROUP );
void delNode (); //code...........
};

仅供参考:这是返回图形数据集(图形挖掘)中频繁模式的程序的一部分。我在这里问的代码块只是一个从文件中打开和读取图形数据信息的过程。图形数据是这样的:

t # 0
v 0 0
v 1 0
...
(all the vertices with their labels)
e 0 1 3
e 1 2 3
...
(all the edges with the vetices they connect and their labels)
t # 1 (graph No.2)
....

这是程序在运行时无法传递的代码块:

void GDatabase::read ( string filename )
    {
        char header[100];
        ifstream input ( filename.c_str () );
        GGraph *graph = new GGraph ();
        input.getline ( header, 100 ); // we assume a header before each graph
        printf("%s", header);
    //    char c;
    //    c = input.get();
    //    while (input) {
    //        std::cout << c;
    //        c = input.get();}
        getchar();
        printf("11111111111");
        printf("n%d",sizeof(graph));
        input >> *graph;
        printf("2222222222");
        while ( !input.eof () ) {
            process ( graph );
            graphs.push_back ( graph );
            graph = new GGraph ();
            input.getline ( header, 100 );
            input >> *graph;
        }
        delete graph;     
        input.close ();
    }

编辑:正如建议的那样,这个">>"实际上是流提取器,我发现了运算符的这个定义:

istream& operator>>(istream& stream, GGraph &graph )
{
  char m;
  GNodeData nd;
  GEdgeData ed;
  GNodeOrGroup og;
  GNodeID ni, ni1, ni2;
  m = stream.get ();
  if ( stream.eof () )
    return stream;
  while ( !stream.eof () && m == 'v' ) {
    stream >> ni;
    stream >> nd;
    stream >> og;
    graph.addNode ( nd, og );
    do {
      m = stream.get (); // get end of line
    } 
    while ( m != 'n' ); 
    m = stream.get ();
  }
  while ( !stream.eof () && m == 'e' ) {
    stream >> ni1;
    stream >> ni2;
    stream >> ed;
    graph.addEdge ( ni1, ni2, ed );
    do {
      m = stream.get (); // get end of line
    }
    while ( m != 'n' ); 
    m = stream.get ();
  }
  stream.unget (); 
  stream.clear (); // also unput eof if we read 
  return stream;
}

输入>>*图表;

这不是按位运算符。 它是一个流提取运算符。 在代码中的某个地方,必须定义一个operator>>,它将流和 GGraph 作为输入,例如:

template<class CharT, class Traits = std::char_traits<CharT> >
std::basic_istream<CharT, Traits>& operator>>(std::basic_istream<CharT, Traits> &in, GGraph &graph)
{
    // read values from in and store them in graph as needed...
    return in;
}

所以这行:

input >> *graph;

实际上是在调用:

operator>>(input, *graph);