PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > How to copy a vector if pointers to base class
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
How to copy a vector if pointers to base class

Réponse
 
LinkBack Outils de la discussion
Vieux 08/06/2008, 19h27   #1
Alex Snast
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut How to copy a vector if pointers to base class

hello

I'm trying to implement a copy constructor for a vector of pointers to
a base class (which is abstract)

My question is how would i know what kind of new type should i
allocate since the base poiner can have multipull meanings.

i know i can use dynamic_cast however i really don't like this
solution and i was wondering is i can do something better

thanks
  Réponse avec citation
Vieux 08/06/2008, 19h45   #2
Kai-Uwe Bux
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to copy a vector if pointers to base class

Alex Snast wrote:

> hello
>
> I'm trying to implement a copy constructor for a vector of pointers to
> a base class (which is abstract)
>
> My question is how would i know what kind of new type should i
> allocate since the base poiner can have multipull meanings.
>
> i know i can use dynamic_cast however i really don't like this
> solution and i was wondering is i can do something better


The standard trick is to provide a virtual clone() member in the base class.
You can then copy the vector by using std::transform with a back_inserter
and a functor like this

Base* deep_copy ( Base * ptr ) {
return( ptr->clone() );
}

or some of these unreadable pointer to member function binders.

You can also google the archives for copy_ptr or clone_ptr to find smart
pointers with deep copy semantics. Those are non-standard and might make
your code harder to maintain for others since they might not be familiar
with those libraries. On the plus side, you don't need to do anything to
copy a vector of those since the copy constructor of the smart pointer will
to the job.


Best

Kai-Uwe Bux
  Réponse avec citation
Vieux 08/06/2008, 22h58   #3
Alex Snast
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to copy a vector if pointers to base class



Kai-Uwe Bux כתב:
> Alex Snast wrote:
>
> > hello
> >
> > I'm trying to implement a copy constructor for a vector of pointers to
> > a base class (which is abstract)
> >
> > My question is how would i know what kind of new type should i
> > allocate since the base poiner can have multipull meanings.
> >
> > i know i can use dynamic_cast however i really don't like this
> > solution and i was wondering is i can do something better

>
> The standard trick is to provide a virtual clone() member in the base class.
> You can then copy the vector by using std::transform with a back_inserter
> and a functor like this
>
> Base* deep_copy ( Base * ptr ) {
> return( ptr->clone() );
> }
>
> or some of these unreadable pointer to member function binders.
>
> You can also google the archives for copy_ptr or clone_ptr to find smart
> pointers with deep copy semantics. Those are non-standard and might make
> your code harder to maintain for others since they might not be familiar
> with those libraries. On the plus side, you don't need to do anything to
> copy a vector of those since the copy constructor of the smart pointer will
> to the job.
>
>
> Best
>
> Kai-Uwe Bux


Thanks for the . Worked just find even though i haven't used
std::transform but rather wrote my own method
Storerepository::Storerepository(const Storerepository& rhs)
throw(OutOfMemory)
{
try {
for (const_repertory_iterator cit = rhs.repertory_.begin(); cit !=
rhs.repertory_.end(); ++cit) {
this->repertory_.push_back((*cit)->clone());
}
}
catch(std::bad_alloc){
throw OutOfMemory();
}
}

thanks again'
  Réponse avec citation
Vieux 09/06/2008, 12h02   #4
Frank Birbacher
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to copy a vector if pointers to base class

Hi!

Daniel T. schrieb:
> Or:
>
> transform(rhs.repertory_.begin(), rhs.repertory_.end(),
> back_inserter(repertory_), mem_fun(&Base::clone));


The mem_fun will not do virtual dispatch here! But you can always do it
like the "C++ Conding Standards" say: have a public non-virtual function
which calls the virtual protected function. Then the above would work.

class Base {
public:
Base* clone() const { return doClone(); }
protected:
virtual Base* doClone()=0;
};

Frank
  Réponse avec citation
Vieux 09/06/2008, 13h15   #5
James Kanze
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to copy a vector if pointers to base class

On Jun 9, 1:02 pm, Frank Birbacher <bloodymir.c...@gmx.net> wrote:
> Daniel T. schrieb:


> > Or:


> > transform(rhs.repertory_.begin(), rhs.repertory_.end(),
> > back_inserter(repertory_), mem_fun(&Base::clone));


> The mem_fun will not do virtual dispatch here!


The mem_fun has exactly the same semantics of calling the
function directly; if the function is declared virtual in Base,
and you call it correctly with a pointer to the base, then it
will use virtual dispatch.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
  Réponse avec citation
Vieux 09/06/2008, 14h31   #6
Frank Birbacher
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to copy a vector if pointers to base class

Hi!

James Kanze schrieb:
>> The mem_fun will not do virtual dispatch here!

>
> The mem_fun has exactly the same semantics of calling the
> function directly;


This is nothing a boost::bind could do, right? The mem_fun has extra
from the compiler here, right?

> if the function is declared virtual in Base,
> and you call it correctly with a pointer to the base, then it
> will use virtual dispatch.


So, the following won't do the same?

struct Base {
virtual void foo() {}
};

struct Dev : Base {
void foo() {}
void test();
};

void Dev::test() {
using boost::bind;

this->Base::foo(); //calls Base::foo
this->*(&Base::foo)(); //calls Base::foo ??
mem_fun(&Base::foo)(this); //calls Dev::foo ?? WTF?
bind(&Base::foo, this)(); //calls Base::foo ??
}

I don't get it. How does mem_fun work?

Frank
  Réponse avec citation
Vieux 10/06/2008, 10h33   #7
James Kanze
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to copy a vector if pointers to base class

On Jun 9, 3:31 pm, Frank Birbacher <bloodymir.c...@gmx.net> wrote:
> James Kanze schrieb:


> >> The mem_fun will not do virtual dispatch here!


> > The mem_fun has exactly the same semantics of calling the
> > function directly;


> This is nothing a boost::bind could do, right? The mem_fun has
> extra from the compiler here, right?


No extra . It uses a pointer to member function.

> > if the function is declared virtual in Base, and you call it
> > correctly with a pointer to the base, then it will use
> > virtual dispatch.


> So, the following won't do the same?


> struct Base {
> virtual void foo() {}
> };


> struct Dev : Base {
> void foo() {}
> void test();
> };


> void Dev::test() {
> using boost::bind;


> this->Base::foo(); //calls Base::foo
> this->*(&Base::foo)(); //calls Base::foo ??


Doesn't compile.
(this->*(&Base::foo))() ;
calls Dev::foo.

> mem_fun(&Base::foo)(this); //calls Dev::foo ?? WTF?


Calls Dev::foo.

> bind(&Base::foo, this)(); //calls Base::foo ??


Calls Dev::foo.
> }


> I don't get it. How does mem_fun work?


It uses a pointer to member function.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
  Réponse avec citation
Vieux 10/06/2008, 11h46   #8
Frank Birbacher
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to copy a vector if pointers to base class

Hi!

Daniel T. schrieb:
> The output I get is:
>
> Base::foo()
> Dev::foo()
> Dev::foo()
> Dev::foo()
>
> IE: in every case except the first, Dev::foo() is called.


o_O I'm sorry, I have a complete misunderstanding of this issue.

Thanks to both of you, Daniel and James, for clearing things up.

Frank
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 12h32.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,19590 seconds with 16 queries