Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Some misc C++ questions (multimap, derived class function argument,virtual static)

Reply
Thread Tools

Some misc C++ questions (multimap, derived class function argument,virtual static)

 
 
Digital Puer
Guest
Posts: n/a
 
      09-13-2009
I have several C++ questions:

1. Why are the interfaces for multiset and multimap so complicated for
getting multiple values?

Instead of
multimap<Key, Value>

I usually prefer
map<Key, vector<Value> >

Isn't that much easier to use?


2. Suppose I have a base class that looks like:

class Base
{
public:
virtual void combine(const Base &other, Base &result) = 0;
}

class Derived : public Base
{
private:
string _name;
public:
void combine(const Derived &other, Derived &other) {}
}

When I try to instantiate Derived, g++ is telling me that the
pure virtual method Base.combine() is not implemented.
Why does Derived.combine() above not override Base.combine()?

Instead, I have to do some casting, like:

void combine(const Base &other, Base &result)
{
Derived &a = (Derived &)other;
string combination = _name + a._name;

((Derived &)result)._name = combination;
}

Is there a better approach?


3. For my abstract Base class, I want to specify a static "Factory"
method that produces a Base*:

class Base
{
virtual static Base* createRandomInstance();
}

I want to force all my Derived classes to provide such a factory
method that produces a Derived *.

However, apparently I cannot have a virtual static method
in C++ (or in Java). What is the best way to force that all
derived classes have such a factory method?

 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      09-13-2009
* Digital Puer:
> I have several C++ questions:
>
> 1. Why are the interfaces for multiset and multimap so complicated for
> getting multiple values?
>
> Instead of
> multimap<Key, Value>
>
> I usually prefer
> map<Key, vector<Value> >
>
> Isn't that much easier to use?


No opinion.


> 2. Suppose I have a base class that looks like:
>
> class Base
> {
> public:
> virtual void combine(const Base &other, Base &result) = 0;
> }
>
> class Derived : public Base
> {
> private:
> string _name;
> public:
> void combine(const Derived &other, Derived &other) {}
> }
>
> When I try to instantiate Derived, g++ is telling me that the
> pure virtual method Base.combine() is not implemented.
> Why does Derived.combine() above not override Base.combine()?


Read up on the Liskov Substitution Principle.

Essentially if you could override that way you could then do

Derived d;
Base b;
Base& r = d;

r.combine( b, b );

where the Derived object gets Base objects in as arguments, but expects Derived
ones.


> Instead, I have to do some casting, like:
>
> void combine(const Base &other, Base &result)
> {
> Derived &a = (Derived &)other;
> string combination = _name + a._name;
>
> ((Derived &)result)._name = combination;
> }
>
> Is there a better approach?


Don't cast.

E.g. you might add virtual member routines that give access to the name.


> 3. For my abstract Base class, I want to specify a static "Factory"
> method that produces a Base*:
>
> class Base
> {
> virtual static Base* createRandomInstance();
> }
>
> I want to force all my Derived classes to provide such a factory
> method that produces a Derived *.
>
> However, apparently I cannot have a virtual static method
> in C++ (or in Java). What is the best way to force that all
> derived classes have such a factory method?


Why not implement a generic factory that works for nearly all derived classes?

Like

template< class T >
T* createRandomInstance() { return new T(); }


class Derived: public Base
{
public:
Derived( char const [] ) {}
};

template<>
Derived* createRandomInstance<Derived>() { return new Derived(""); }


Base* p = createRandomInstance<Base>();

If the generic version works for a derived class, fine. If not, then using it
for a derived class probably won't compile. So then you've "forced" the derived
class to provide a specialization.


Cheers & hth.,

- Alf
 
Reply With Quote
 
 
 
 
Francesco
Guest
Posts: n/a
 
      09-13-2009
On 13 Set, 21:10, Digital Puer <(E-Mail Removed)> wrote:
> I have several C++ questions:
>
> 1. Why are the interfaces for multiset and multimap so complicated for
> getting multiple values?


I don't find them that convolute... what is the part of the interface
that you don't like?

> Instead of
> multimap<Key, Value>
>
> I usually prefer
> map<Key, vector<Value> >
>
> Isn't that much easier to use?


The two containers above are very similar but not interchangeable.

A multimap has its advantages on map, in this specific case. It allows
you to store separately two physically different keys which are just
logically equal to each other. If you don't need this peculiarity and
map fulfills your needs, you're perfectly fine using it.

Just my two cents, sorry for the other points but I should understand
them better before posting anything about them.

Cheers,
Francesco
 
Reply With Quote
 
Digital Puer
Guest
Posts: n/a
 
      09-13-2009
On Sep 13, 12:37*pm, Pete Becker <(E-Mail Removed)> wrote:
> Digital Puer wrote:
> > However, apparently I cannot have a virtual static method
> > in C++ (or in Java). What is the best way to force that all
> > derived classes have such a factory method?

>
> Specification and testing. Beyond that, why do you care? If someone
> wants to derived from your class but not provide a factory, what harm
> does it do?
>



Because I want to enforce the existence of a factory method
in all derived classes. Every derived class will have its own
data, and I want the class to be able to produce a Base *
though the factory. I would like this factory method to be
usable by a consumer of the Base class:

void consumer()
{
Base *randomItem = Derived::createInstance(); // factory
doSomething(randomItem);
}

You are right that I could write an English specification that
says that all derived classes must have a factory method,
but I want to have that enforced in the Base class.

I guess a better approach would be to move the factory method
into another class that knows about the Derived class.

void consumer(Factory &factory)
{
Base *randomItem = factory.createInstanceOfDerived();
doSomething(randomItem);
}
 
Reply With Quote
 
Paul N
Guest
Posts: n/a
 
      09-14-2009
On 13 Sep, 20:10, Digital Puer <(E-Mail Removed)> wrote:

> However, apparently I cannot have a virtual static method
> in C++ (or in Java).


I asked a similar question on 21 Mar 2008 - you can find it if you put
"gw7rib virtual static" into Google Groups and click "Search Groups".
Marcel Müller provided a solution to the problem I was having.

Hope this helps.
Paul.

 
Reply With Quote
 
Joshua Maurice
Guest
Posts: n/a
 
      09-14-2009
On Sep 13, 12:10*pm, Digital Puer <(E-Mail Removed)> wrote:
> However, apparently I cannot have a virtual static method
> in C++ (or in Java).


What would a "virtual static method" do?

Ex:

struct base { virtual void foo(); };
struct derived : base { virtual void foo(); };
int main()
{
derived d;
d.foo(); //calls derived::foo
derived* pd = &d;
pd->foo(); //calls derived::foo
base* pb = pd;
pb->foo(); //calls derived::foo

base b;
b.foo(); //calls base::foo
base* pb2 = &b;
pb2->foo(); //calls base::foo
}

In this example, which foo function is called depends upon the most-
derived type of the "this" object. If the most most-derived type of
the "this" object is "derived", then "derived::foo" is called. If the
most-derived type of the "this" object is "base", then "base::foo" is
called. Virtual functions require a "this" object. Static functions
forbid a "this" object. Thus it's not obvious what the semantics would
be for a "virtual static" function.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
call base class function or derived class function George2 C++ 0 03-17-2008 02:03 AM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 01:44 PM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 12:07 AM
comp.os.ms-windows.networking.misc,misc.invest.mutual-funds,comp.lang.python,comp.os.magic-cap,comp.sources.games.bugs . Python 0 06-19-2004 08:23 AM
comp.sys.mac.games.misc,comp.sys.ibm.pc.hardware.misc,comp.mail.pegasus-mail.ms-windows,comp.databases.paradox,comp.dcom.sys.cisco . Cisco 0 06-19-2004 07:48 AM



Advertisments