Hashmap copy constructor

D

David

Hi,

I have created the following HashMap class.
class HashMap: public hash_map<string, string, HashString,
HashStringCompare>
{
public:
HashMap(): hash_map<string, string, HashString, HashStringCompare>() {}
};

If I want to clone another HashMap, what should I do?


Thanks
D
 
D

David

David said:
Hi,

I have created the following HashMap class.
class HashMap: public hash_map<string, string, HashString,
HashStringCompare>
{
public:
HashMap(): hash_map<string, string, HashString, HashStringCompare>() {}
};
I also found that hash_map contains copy constructor, so if I write:
HashMap hash;
hash["key"] = "key";
hash["value"] = "value";

HashMap tmp;
tmp=hash;

tmp object should contains all items from hash?
 
K

Kai-Uwe Bux

I do not quite see the point of this definition. What is the advantage
compared to:

typedef hash_map said:
I also found that hash_map contains copy constructor, so if I write:
HashMap hash;
hash["key"] = "key";
hash["value"] = "value";

HashMap tmp;
tmp=hash;

tmp object should contains all items from hash?

No, since you defined your own class by inheriting, you would have to
provide your own copy constructor for HashMap. If you use the typedef from
above, things would work like you seem to expect.



Best

Kai-Uwe Bux
 
K

Kai-Uwe Bux

Kai-Uwe Bux said:
I do not quite see the point of this definition. What is the advantage
compared to:

I also found that hash_map contains copy constructor, so if I write:
HashMap hash;
hash["key"] = "key";
hash["value"] = "value";

HashMap tmp;
tmp=hash;

tmp object should contains all items from hash?

No, since you defined your own class by inheriting, you would have to
provide your own copy constructor for HashMap. If you use the typedef from
above, things would work like you seem to expect.

Oops, on second thought: the compiler will provide the default copy
constructor and assignment operator, which in your case should work as
expected. Yet, I still dont see why you want to inherit.


Best

Kai-Uwe Bux
 
R

Richard Herring

Hi,
I have created the following HashMap class.
class HashMap: public hash_map<string, string, HashString,
HashStringCompare>
{
public:
HashMap(): hash_map<string, string, HashString, HashStringCompare>() {}
};
I also found that hash_map contains copy constructor, so if I write:
HashMap hash;
hash["key"] = "key";
hash["value"] = "value";

HashMap tmp;
tmp=hash;[/QUOTE]

That's not copy construction. It's default construction followed by
assignment.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,668
Latest member
SamiraShac

Latest Threads

Top