 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
nomis80 Guest
|
Posted: Sat Dec 24, 2005 8:39 am Post subject: How to swap parent classes |
|
|
Hi,
I'm writing a swap method:
class Parent {
Stuff parentData;
};
class Derived : public Parent {
Stuff* dynamicallyAllocatedStuff;
public:
void swap( Derived& other ) {
// How to swap the parent parts using std::swap()?
std::swap( derivedData, other.derivedData );
}
}
How can I swap the parent parts using std::swap()? I don't want to add
a swap method to the parent class because it is obviously not needed.
The default copy constructor is acceptable.
All I'm asking for is the correct syntax.
Thanks!
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
John Potter Guest
|
Posted: Sun Dec 25, 2005 10:43 am Post subject: Re: How to swap parent classes |
|
|
On 24 Dec 2005 03:39:31 -0500, "nomis80" <simon.perreault (AT) gmail (DOT) com>
wrote:
| Quote: | class Parent {
Stuff parentData;
};
class Derived : public Parent {
Stuff* dynamicallyAllocatedStuff;
public:
void swap( Derived& other ) {
// How to swap the parent parts using std::swap()?
std::swap( derivedData, other.derivedData );
|
std::swap<Parent>( *this, other );
John
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
wizofaus@hotmail.com Guest
|
Posted: Wed Dec 28, 2005 10:02 am Post subject: Re: How to swap parent classes |
|
|
John Potter wrote:
| Quote: | On 24 Dec 2005 03:39:31 -0500, "nomis80" <simon.perreault (AT) gmail (DOT) com
wrote:
class Parent {
Stuff parentData;
};
class Derived : public Parent {
Stuff* dynamicallyAllocatedStuff;
public:
void swap( Derived& other ) {
// How to swap the parent parts using std::swap()?
std::swap( derivedData, other.derivedData );
std::swap
}
}
|
Which will only work if "Stuff" supports operator= of course. Related
question, that I'm sure has been asked before - are there any plans to
support compiler-generated memberwise swap? (I'd also like to see
operator==, but swap's the main thing).
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Michiel.Salters@tomtom.co Guest
|
Posted: Wed Dec 28, 2005 6:08 pm Post subject: Re: How to swap parent classes |
|
|
nomis80 wrote:
| Quote: | Hi,
I'm writing a swap method:
class Parent {
Stuff parentData;
};
class Derived : public Parent {
Stuff* dynamicallyAllocatedStuff;
public:
void swap( Derived& other ) {
// How to swap the parent parts using std::swap()?
|
using std::swap;
swap( (Parent&)*this, (Parent&)other);
Don't use std::swap directly, because Parent may have a namespace scope
swap. With an unqualified swap and a using std::swap, std::swap is used
as
a last resort.
HTH,
Michiel Salters
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Jeffrey Schwab Guest
|
Posted: Sat Dec 31, 2005 1:20 pm Post subject: Re: How to swap parent classes |
|
|
nomis80 wrote:
| Quote: | Hi,
I'm writing a swap method:
class Parent {
Stuff parentData;
};
class Derived : public Parent {
Stuff* dynamicallyAllocatedStuff;
public:
void swap( Derived& other ) {
// How to swap the parent parts using std::swap()?
std::swap( derivedData, other.derivedData );
}
}
How can I swap the parent parts using std::swap()? I don't want to add
a swap method to the parent class because it is obviously not needed.
The default copy constructor is acceptable.
|
parentData is a private member of Parent. The cleanest way for Derived
to manipulate parentData is for Parent to provide some protected
methods. Alternatively, refactor to avoid the inheritance relationship.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Carl Barron Guest
|
Posted: Sun Jan 01, 2006 11:47 am Post subject: Re: How to swap parent classes |
|
|
In article <sM_sf.166$lg.158 (AT) southeast (DOT) rr.com>, Jeffrey Schwab
<jeff (AT) schwabcenter (DOT) com> wrote:
| Quote: |
How can I swap the parent parts using std::swap()? I don't want to add
a swap method to the parent class because it is obviously not needed.
The default copy constructor is acceptable.
parentData is a private member of Parent. The cleanest way for Derived
to manipulate parentData is for Parent to provide some protected
methods. Alternatively, refactor to avoid the inheritance relationship.
|
The access rights of Parent, are not material, You don't access the
contents Parent anymore than you are accessing the contents of
std::vector<T,A> when swapping two std::vector<T,A>'s.
This idiom is useful if 1) std::swap<Parent> or std::swap<Derived> is
inefficient or invalid [Parent or Derived is not Assignable]
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Jeffrey Schwab Guest
|
Posted: Thu Jan 05, 2006 12:44 am Post subject: Re: How to swap parent classes |
|
|
Carl Barron wrote:
| Quote: | In article <sM_sf.166$lg.158 (AT) southeast (DOT) rr.com>, Jeffrey Schwab
[email]jeff (AT) schwabcenter (DOT) com[/email]> wrote:
How can I swap the parent parts using std::swap()? I don't want to add
a swap method to the parent class because it is obviously not needed.
The default copy constructor is acceptable.
parentData is a private member of Parent. The cleanest way for Derived
to manipulate parentData is for Parent to provide some protected
methods. Alternatively, refactor to avoid the inheritance relationship.
The access rights of Parent, are not material, You don't access the
contents Parent anymore than you are accessing the contents of
std::vector<T,A> when swapping two std::vector<T,A>'s.
This idiom is useful if 1) std::swap<Parent> or std::swap<Derived> is
inefficient or invalid [Parent or Derived is not Assignable]
|
I find that point of view absurd. std::vector provides methods for
swapping vector contents; if the contents of Parent classes are intended
to be swapped, there should be explicit provision for such behavior.
This does not imply that std::swap has to be used at all.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
John Potter Guest
|
Posted: Thu Jan 05, 2006 7:04 pm Post subject: Re: How to swap parent classes |
|
|
On 28 Dec 2005 13:08:11 -0500, [email]Michiel.Salters (AT) tomtom (DOT) com[/email] wrote:
| Quote: | nomis80 wrote:
class Parent {
Stuff parentData;
};
class Derived : public Parent {
Stuff* dynamicallyAllocatedStuff;
public:
void swap( Derived& other ) {
// How to swap the parent parts using std::swap()?
using std::swap;
swap( (Parent&)*this, (Parent&)other);
}
}
Don't use std::swap directly, because Parent may have a namespace scope
swap. With an unqualified swap and a using std::swap, std::swap is used
as a last resort.
|
Nope. Any namespace scope swap is hidden by the name of the function
containing the call. Without the using, it is a call of a non-existent
member function, and with the using, it is a call of std::swap. You
could add another using to bring in a namespace scope swap, but that
would depend upon it existing and remove the need for using std::swap.
Your code does solve the problem, but is no better than a direct call of
std::swap. The original question also states that there is no swap for
Parent because std::swap does the right thing. It is a simple syntax
question.
John
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
John Potter Guest
|
Posted: Thu Jan 05, 2006 7:04 pm Post subject: Re: How to swap parent classes |
|
|
On 4 Jan 2006 19:44:39 -0500, Jeffrey Schwab <jeff (AT) schwabcenter (DOT) com>
wrote:
| Quote: | Carl Barron wrote:
In article <sM_sf.166$lg.158 (AT) southeast (DOT) rr.com>, Jeffrey Schwab
[email]jeff (AT) schwabcenter (DOT) com[/email]> wrote:
|
Missing attribution to original questioner.
| Quote: | How can I swap the parent parts using std::swap()? I don't want to add
a swap method to the parent class because it is obviously not needed.
The default copy constructor is acceptable.
parentData is a private member of Parent. The cleanest way for Derived
to manipulate parentData is for Parent to provide some protected
methods. Alternatively, refactor to avoid the inheritance relationship.
|
The original article states that Parent contains only simple members and
trivial copy ctor and assignment. Maybe you were confused by use of
"parentData" to refer to the base class member of Derived of type
Parent?
| Quote: | The access rights of Parent, are not material, You don't access the
contents Parent anymore than you are accessing the contents of
std::vector<T,A> when swapping two std::vector<T,A>'s.
This idiom is useful if 1) std::swap<Parent> or std::swap<Derived> is
inefficient or invalid [Parent or Derived is not Assignable]
I find that point of view absurd. std::vector provides methods for
swapping vector contents; if the contents of Parent classes are intended
to be swapped, there should be explicit provision for such behavior.
This does not imply that std::swap has to be used at all.
|
I find that point of view absurd. std::vector provides methods for
swapping because it makes sense for efficiency. std::vector is not
designed to be used as a base (Parent) class. std::pair does not
provide methods for swapping pair contents because it would be silly.
The Parent class did provide the means to swap by providing copy
construction and assignment. It swaps just like int and std::swap is
appropriate. What was a simple syntax question got turned into a
nonsense debate over unrelated style opinions.
John
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|