指向类的指针上的按位运算符
Bitwise operator on pointer to class?
我花了一天时间挖掘项目中以前一个人的遗留代码,并且我还搜索了按位运算符,我仍然无法清楚地理解按位操作器的代码行:
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);
相关文章:
- C++ 带有函数指针的运算符优先级
- 是否允许编译器省略对指针的 &* 运算符的组合调用?
- 错误:表达式必须具有算术、无作用域枚举或带有运算符重载的指针类型
- 重载 ostream << 运算符,指针作为参数,导致输出上的内存地址
- 在一个指令中声明更多指针的运算符优先级
- 为什么在通过引用返回运算符分配时取消引用'this'指针?
- SFINAE - 检测类型 T 是指针、数组还是带有随机访问运算符的容器,以及给定的值类型
- c++:复制、删除和运算符=在原始指针映射中
- 除了调用全局删除运算符之外,删除一个void指针还能做什么呢
- 类重载运算符 '<' 插入指向该对象集的共享指针时不调用
- 使用继承的指针列表复制构造函数或重载运算符=
- delete运算符如何处理c中的指针
- 为指针重载运算符++
- 在分配指向数组内存地址的指针时,为什么不必使用地址运算符?
- 如何从模板类重载创建的指针对象上的运算符?
- 创建指针是否超过非数组指针的末尾,而不是从 C++17 中的一元运算符和未定义的行为派生?
- 为什么运算符"new"需要指针才能工作?
- C++运算符指针,乘法
- 运算符指针数组的新语法
- 重载运算符(指针)