questions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Guest's Avatar

    questions

    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"] = "spam@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


  • Ron Natalie

    #2
    Re: questions


    "<- Chameleon ->" <cham_gss@hotma il.NOSPAM.com> wrote in message news:bknlc6$p54 $1@nic.grnet.gr ...[color=blue]
    > 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"] = "spam@here.now" ;[/color]

    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.
    [color=blue]
    > 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 ;-)[/color]

    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.


    Comment

    • Guest's Avatar

      #3
      Re: questions

      > > 1. In Java there is a class named HashTable. Is there something similar in C++?[color=blue][color=green]
      > > I mean something like:
      > > -----
      > > hash["my_name"] = "paul";
      > > hash["my_mail"] = "spam@here.now" ;[/color]
      >
      > 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.
      >[color=green]
      > > 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 ;-)[/color]
      >
      > 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.[/color]

      Thankyou very much!


      Comment

      Working...