如何平等两个对象? [英] how to equal two objects?

查看:65
本文介绍了如何平等两个对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何将一个指针指向另一个。在我的方法中获取(指针a,指针b)i指针b等于指针a。但是当我调试它时,我的变量不会改变。我该怎么办? ?



i want to know how can i equal one pointer to another.in my method get(pointer a,pointer b) i equal pointer b to pointer a.but when i debug it my variable do not change.what can i do?

#include<iostream>
using namespace std;
class state{
	public:
		int num;
		 state* start;
		 state* q1;
		 state* q2;
		 state* q3;
		 state* q4;
		 state* q5;
		 state* now;
		void getTheInput(char);
		bool equal(state*,state*);
		void get(state*,state*);
};
void state::get(state *a,state*b){
	a=b;
}
bool state::equal (state *a,state* b){
	return a==b; 
}
void state::getTheInput(char a)
{
	if(a=='a' || a=='b')
	{
		if(equal(now,start) && a=='a'){
			cout<<"1";
			get(now,q1);
			
		}
		else if(equal(now,start) && a=='b'){
			cout<<"2";
			get(now,start);
		}
		else if(equal(now,q1) && a=='a'){
			cout<<"3";
			get(now,q1);
		}
		else if(equal(now,q1) && a=='b'){
			cout<<"4";
			get(now,q2);
		}
		else if(equal(now,q2) && a=='a')
			get(now,q3);
		else if(equal(now,q2) && a=='b')
			get(now,start);
		else if(equal(now,q3) && a=='a')
			get(now,q1);
		else if(equal(now,q3) && a=='b')
			get(now,q4);
		else if(equal(now,q4) && a=='a')
			get(now,q3);
		else if(equal(now,q4)&& a=='b'){
			get(now,q5);
			cout<<"Got it!!!";
		}
		else if(equal(now,q5) && a=='a')
			get(now,q1);
		else if(equal(now,q5) && a=='b')
			get(now,start);
	}
	else {
		cout<<"Wrong input!! just 'a' or 'b'";
	}
}

int main()
{
	char a;
	state *test=new state();
	test->q1=new state();
	test->q2=new state();
	test->q3=new state();
	test->q4=new state();
	test->q5=new state();
	test->now=new state();
	test->start=new state();
	test->now=test->start;

	for(;;)
	{
		cin>>a;
		test->getTheInput(a);
	}
}

推荐答案

以下代码是无稽之谈。



The following code is nonsense.

void state::get(state *a,state*b){
a=b;
}





它没有用处。



如果你想让调用者的指针 a 指向 b ,你需要声明指针 a 作为参考。





It serves no purpose.

If you want the caller's pointer a to point to b, you need to declare pointer a as a reference.

void state::get(state* &a,state*b){
a=b;
}





如果你想复制数据,你需要取消引用它。





If you want to copy the data, you'll need to de-reference it.

void state::get(state*a, state*b){
*a=*b;
}





这会导致内存泄漏,所以可能不是你想要的。



如果你只想指定指针,你可以取消get函数并直接进行赋值。



而不是...





This would cause a memory leak so probably not what you want.

If you only want to assign pointers, you can eliminate the get function and do the assignment directly.

Instead of ...

get(now, start);





...你可以写...





... you can write ...

now = start;





对我来说,这会更容易阅读。



除此之外,我无法发表评论,因为我可以告诉你要做什么。



To me, that would be slightly easier to read.

Beyond that, I can't comment since I can't tell what you're trying to do.


这篇关于如何平等两个对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆