Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Initialising private base members in copy constructor

Reply
Thread Tools

Initialising private base members in copy constructor

 
 
Fraser Ross
Guest
Posts: n/a
 
      08-06-2007
template<typename T,
typename CounterPolicy = SimpleReferenceCount,
typename ObjectPolicy = StandardObjectPolicy>
class CountingPtr : private CounterPolicy, private ObjectPolicy {
private:
// shortcuts:
typedef CounterPolicy CP;
typedef ObjectPolicy OP;

T* object_pointed_to;
public:

// copy constructor:
CountingPtr (CountingPtr<T,CP,OP> const& cp)
: CP((CP const&)cp), // copy policies
OP((OP const&)cp) {
this->attach(cp); // copy pointer and increment counter
}

I'm not sure why the casts work. Why wasn't static_cast used instead?
Since the inheritances are private I'm surprised that casts can be done.
Some compilers don't appear to even require a cast at all.

Fraser.



--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      08-06-2007
Fraser Ross wrote:
> template<typename T,
> typename CounterPolicy = SimpleReferenceCount,
> typename ObjectPolicy = StandardObjectPolicy>
> class CountingPtr : private CounterPolicy, private ObjectPolicy {
> private:
> // shortcuts:
> typedef CounterPolicy CP;
> typedef ObjectPolicy OP;
>
> T* object_pointed_to;
> public:
>
> // copy constructor:
> CountingPtr (CountingPtr<T,CP,OP> const& cp)
> : CP((CP const&)cp), // copy policies
> OP((OP const&)cp) {
> this->attach(cp); // copy pointer and increment counter
> }
>
> I'm not sure why the casts work. Why wasn't static_cast used instead?


How are the copy constructors defined in the 'CounterPolicy' and the
'ObjectPolicy' classes used to instantiate this template? IOW, you did
not post enough code to answer your question.

> Since the inheritances are private I'm surprised that casts can be
> done. Some compilers don't appear to even require a cast at all.


V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
 
 
 
Fraser Ross
Guest
Posts: n/a
 
      08-06-2007

"Victor Bazarov"
> How are the copy constructors defined in the 'CounterPolicy' and the
> 'ObjectPolicy' classes used to instantiate this template? IOW, you

did
> not post enough code to answer your question.


They are usually small classes that don't define copy constructors.
There is enough code pasted.

The subject should have been "Initialising private base classes in copy
constructor".

Fraser.



--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
Fraser Ross
Guest
Posts: n/a
 
      08-06-2007
Are pointers/references to derived objects convertible to
pointers/references of private base classes that are directly inherited
as it says in this article:
http://msdn2.microsoft.com/en-us/lib...4d(VS.80).aspx ?

Fraser.



--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
hurcan solter
Guest
Posts: n/a
 
      08-06-2007
On Aug 6, 10:13 pm, "Fraser Ross" <(E-Mail Removed)> wrote:
> Are pointers/references to derived objects convertible to
> pointers/references of private base classes that are directly inherited
> as it says in this article:http://msdn2.microsoft.com/en-us/lib...4d(VS.80).aspx ?
>
> Fraser.
>
> --
> Posted via a free Usenet account fromhttp://www.teranews.com


yes they are convertible from inside the derived class, and the
compiler will provide a deafult copy constructor
if you didn't declare one. See http://www.parashift.com/c++-faq-lit...heritance.html

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      08-06-2007
Fraser Ross wrote:
> "Victor Bazarov"
>> How are the copy constructors defined in the 'CounterPolicy' and the
>> 'ObjectPolicy' classes used to instantiate this template? IOW, you
>> did not post enough code to answer your question.

>
> They are usually small classes that don't define copy constructors.


Ah. OK.

> There is enough code pasted.


I am not going to argue based on a technicality.

> The subject should have been "Initialising private base classes in
> copy constructor".


The conversion works regardless of where it occurs. What you have is
essentially

class DerivedClass : public BaseClass1, public BaseClass2 {};

DerivedClass object;
DerivedClass const& rderived = object;
BaseClass1 const & rb1 = (BaseClass1 const &) rderived;
BaseClass2 const & rb2 = (BaseClass2 const &) rderived;

in which the casts before the 'derived' in the initialisation of 'rb1'
and 'rb2' are _absolutely_ unnecessary. The conversion should work
without them. Have you tried removing them at all?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      08-07-2007
On Aug 6, 8:18 pm, "Fraser Ross" <(E-Mail Removed)> wrote:
> template<typename T,
> typename CounterPolicy = SimpleReferenceCount,
> typename ObjectPolicy = StandardObjectPolicy>
> class CountingPtr : private CounterPolicy, private ObjectPolicy {
> private:
> // shortcuts:
> typedef CounterPolicy CP;
> typedef ObjectPolicy OP;


> T* object_pointed_to;
> public:


> // copy constructor:
> CountingPtr (CountingPtr<T,CP,OP> const& cp)
> : CP((CP const&)cp), // copy policies
> OP((OP const&)cp) {
> this->attach(cp); // copy pointer and increment counter
> }


> I'm not sure why the casts work. Why wasn't static_cast used
> instead?


Because static_cast doesn't ignore the private. This is the one
thing that you can only do with a C style cast. Except that
here...

> Since the inheritances are private I'm surprised that casts can be done.
> Some compilers don't appear to even require a cast at all.


At this point, you're in the context of CountingPtr, so you have
access to the private bases. No cast is required, and IMHO,
it's very bad policy to use one. (If you have to, of course,
static_cast is to be preferred, but in general, the conversion
to base is one of the foundations of OO programming, to the
point of being an exception to the rule that implicit
conversions should be avoided.)

Outside of CountingPtr, of course, static_cast won't work, but a
C style cast will. (I don't know why the standard says this.
Some historical reason, probably.) In general, however, private
inheritance is (or should be) an implementation detail; outside
of the class, you should code as if it wasn't there.

--
James Kanze (GABI Software) email:(E-Mail Removed)
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

 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      08-07-2007
On Aug 6, 9:13 pm, "Fraser Ross" <(E-Mail Removed)> wrote:
> Are pointers/references to derived objects convertible to
> pointers/references of private base classes that are directly inherited
> as it says in this article:http://msdn2.microsoft.com/en-us/lib...4d(VS.80).aspx ?


I didn't read the article in detail, but it certainly started
out on the right foot. Access control is orthogonal to
conversions; the implicit conversion exists, regardless of the
type of heritage (private or public). Access control determines
whether you have a right to use it or not; if the heritage is
private, you only have a right to use the conversion within the
class itself.

C style casts ignore access control, so you can violate this
restriction at will. I can't think of a case where you'd want
to, however. (And I would never use a C style cast for a
pointer or reference conversion.)

--
James Kanze (GABI Software) email:(E-Mail Removed)
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

 
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
template copy constructor vs normal copy constructor cinsk C++ 35 10-10-2010 11:14 PM
Calling base class constructor from derived class Copy constructor ali C++ 4 03-05-2007 09:15 AM
Initialising base class reference members from a derived class Tim Clacy C++ 3 03-04-2004 08:54 PM
Can nested class members access private members of nesting class? CoolPint C++ 8 12-14-2003 02:30 PM
Re: Copy assignment for derived class when base class has private members John Harrison C++ 0 08-06-2003 08:50 AM



Advertisments