By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,035 Members | 1,555 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,035 IT Pros & Developers. It's quick & easy.

Initialising private base members in copy constructor

P: n/a
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,OPconst& 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

Aug 6 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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,OPconst& 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
Aug 6 '07 #2

P: n/a

"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

Aug 6 '07 #3

P: n/a
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

Aug 6 '07 #4

P: n/a
On Aug 6, 10:13 pm, "Fraser Ross" <a...@b.comwrote:
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

Aug 6 '07 #5

P: n/a
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
Aug 6 '07 #6

P: n/a
On Aug 6, 8:18 pm, "Fraser Ross" <a...@b.comwrote:
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,OPconst& 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:ja*********@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

Aug 7 '07 #7

P: n/a
On Aug 6, 9:13 pm, "Fraser Ross" <a...@b.comwrote:
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:ja*********@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

Aug 7 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.