问题 [英] questions

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

问题描述

1。在Java中有一个名为HashTable的类。在C ++中有类似的东西吗?

我的意思是:

-----

hash [" my_name"] =" ; paul" ;;

hash [" my_mail"] =" sp ** @ here.now";

-----


2.我们有这个代码:

-----------------

A类{。 ..};

B级:A {...};

无效GiveMeAClass(A& a){...}

-----------------

在函数GiveMeAClass中我们也可以传递一个A或B对象。

有没有GiveMeAClass中的方法在运行时确定传递的对象是来自A还是B?

类似Java的关键字''instanceof''

我不相信,但是希望会最后死去;-)


谢谢

解决方案



" - 变色龙 - >" < CH ****** @ hotmail.NOSPAM.com>在消息新闻中写道:bk ********** @ nic.grnet.gr ...

1.在Java中有一个名为HashTable的类。在C ++中有类似的东西吗?
我的意思是:
-----
hash [" my_name"] =" paul";
hash [" my_mail"] =" sp ** @ here.now" ;;


标准语言不支持散列,但标准地图类有你想要的

语义(它在内部使用树表示)。 br />

#include< map>

#include< string>

using namespace std;


map< string,string> hash;


hash [" my_name"] =" paul";


等等。

2.我们有这个代码:
-----------------
A类{...};
B类:A { ...};
void GiveMeAClass(A& a){...}
-----------------
在函数GiveMeAClass中我们也可以传递一个A或B对象。
在GiveMeAClass中是否有一个方法在运行时确定传递的对象是来自A还是B?
像Java'的关键字''instanceof' '
我不相信,但希望会永远消亡; - )




嗯,在上面的例子中,你不能把B传给GiveMeAClass (因为你并没有公开承诺'b $ b继承)。但是,如果你修复它,并且你制作了一个多态(即,使它至少有一个

虚拟函数),你可以:


void GiveMeAClass (A& a){

if(dynamic_cast< A *>(& a))

cout<< 给我一个A!\ n;

else if(dynamic_cast< B *>(& b))

cout<< 给我一个B!\ n;

其他

cout<< 这是什么?\ n;

}

}


或者你可以用typeinfo做类似的事情:


if(typeid(a)== typeid(A))

两者之间存在微小的差异,但你明白了。


> > 1.在Java中有一个名为HashTable的类。在C ++中有类似的东西吗?

我的意思是:
-----
hash [" my_name"] =" ; paul" ;;
hash [" my_mail"] =" sp ** @ here.now";



标准语言不支持散列,但是标准的map类有你想要的语义(它在内部使用树形表示)。

#include< map>
#include< string>
using namespace std;

map< string,string> hash;

hash [" my_name"] =" paul" ;;

等等。

2.我们有这个代码:
-----------------
A类{...};
B类:A {...} ;
void GiveMeAClass(A& a){...}
-----------------
在函数GiveMeAClass中我们可以通过A或B对象也是。
在GiveMeAClass中是否有一个方法在运行时确定传递的对象是来自A还是B?
类似Java的关键字''instanceof''
我不相信,但希望会最后消亡; - )



嗯,在上面的例子中,你不能把B传给GiveMeAClass(因为你没有公开
继承)。但是,如果你修复它,并且你制作了一个多态(即,使它至少有一个
虚函数),你可以:

void GiveMeAClass(A& a){
if(dynamic_cast< A *>(& a))
cout<< 给我一个A!\ n;
如果(dynamic_cast< B *>(& b))
cout<< 给我一个B!\ n;
其他
cout<< 这是什么?\ n;
}


或者你可以用typeinfo做类似的事情:

if(typeid (a)== typeid(A))

这两者之间存在细微差别,但你明白了。




非常感谢你很多!


1. In Java there is a class named HashTable. Is there something similar in C++?
I mean something like:
-----
hash["my_name"] = "paul";
hash["my_mail"] = "sp**@here.now";
-----

2. we have this code:
-----------------
class A { ... };
class B : A { ... };
void GiveMeAClass(A &a) { ... }
-----------------
In function GiveMeAClass we can pass an A or B object too.
Is there a method inside GiveMeAClass to determine in run-time if passed object is from A or B?
Something like Java''s keyword ''instanceof''
I believe not, but hope will die last ;-)

Thanks

解决方案


"<- Chameleon ->" <ch******@hotmail.NOSPAM.com> wrote in message news:bk**********@nic.grnet.gr...

1. In Java there is a class named HashTable. Is there something similar in C++?
I mean something like:
-----
hash["my_name"] = "paul";
hash["my_mail"] = "sp**@here.now";
The standard language doesn''t support hashing, but the standard map class has the
semantics you want (it internally uses a tree representation).

#include <map>
#include <string>
using namespace std;

map<string, string> hash;

hash["my_name"] = "paul";

and so forth.
2. we have this code:
-----------------
class A { ... };
class B : A { ... };
void GiveMeAClass(A &a) { ... }
-----------------
In function GiveMeAClass we can pass an A or B object too.
Is there a method inside GiveMeAClass to determine in run-time if passed object is from A or B?
Something like Java''s keyword ''instanceof''
I believe not, but hope will die last ;-)



Well, in your above example, you can''t pass a B to GiveMeAClass (since you didn''t make A publicly
inheritted). However if you fix that, and you make A polymorhphic (that is, make it have at least one
virtual function), you can:

void GiveMeAClass(A& a) {
if(dynamic_cast<A*>(&a))
cout << "Give me an A!\n";
else if(dynamic_cast<B*>(&b))
cout << "Give me a B!\n";
else
cout << "What is this?\n";
}
}

Alternatively you could do a similar thing with typeinfo:

if(typeid(a) == typeid (A))
There are small subtle differences between the two but you get the idea.


> > 1. In Java there is a class named HashTable. Is there something similar in C++?

I mean something like:
-----
hash["my_name"] = "paul";
hash["my_mail"] = "sp**@here.now";



The standard language doesn''t support hashing, but the standard map class has the
semantics you want (it internally uses a tree representation).

#include <map>
#include <string>
using namespace std;

map<string, string> hash;

hash["my_name"] = "paul";

and so forth.

2. we have this code:
-----------------
class A { ... };
class B : A { ... };
void GiveMeAClass(A &a) { ... }
-----------------
In function GiveMeAClass we can pass an A or B object too.
Is there a method inside GiveMeAClass to determine in run-time if passed object is from A or B?
Something like Java''s keyword ''instanceof''
I believe not, but hope will die last ;-)



Well, in your above example, you can''t pass a B to GiveMeAClass (since you didn''t make A publicly
inheritted). However if you fix that, and you make A polymorhphic (that is, make it have at least one
virtual function), you can:

void GiveMeAClass(A& a) {
if(dynamic_cast<A*>(&a))
cout << "Give me an A!\n";
else if(dynamic_cast<B*>(&b))
cout << "Give me a B!\n";
else
cout << "What is this?\n";
}
}

Alternatively you could do a similar thing with typeinfo:

if(typeid(a) == typeid (A))
There are small subtle differences between the two but you get the idea.



Thankyou very much!


这篇关于问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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