473,387 Members | 1,722 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Initialising private base members in copy constructor

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
7 1879
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

"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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Tim Clacy | last post by:
Can this be done... or is there a better way to achieve the same objective? If an interface class contains only references (to yet more interface classes), then can those references be...
5
by: Christian Meier | last post by:
Hi dear programmers I looked for the difference between private and protected inheritance, but couldn't find anything. Here is my sample code: #include <iostream> using std::cout; using...
6
by: Fred | last post by:
Hi I have a class defined in a library that I'd like to add some extra functionality to. This will involve adding a few member variables and a few related methods. As I understand it I can...
10
by: Ioannis Vranos | last post by:
May someone explain why does this compile? class HiddenSealBaseClass { public: HiddenSealBaseClass() { } }; class Sealed: virtual HiddenSealBaseClass
4
by: Jamie Hankins | last post by:
I'm probably being dense here. In the following situation: class Base { int x; int y; } class Decendant : Base { int z; }
23
by: Ben Voigt | last post by:
I have a POD type with a private destructor. There are a whole hierarchy of derived POD types, all meant to be freed using a public member function Destroy in the base class. I get warning C4624....
4
by: ali | last post by:
Hi, I am new to C++ and trying to understand how to work on Inheritance and Operator overloading. I understand that the derived class can pass the base class constructor in its constructor...
2
by: mark4asp | last post by:
Q: Initialising and updating a class with only static members & database dependency I have a class with the following members: public static List<ACISACIS_List; static AssetClass() { //...
19
by: jan.loucka | last post by:
Hi, We're building a mapping application and inside we're using open source dll called MapServer. This dll uses object model that has quite a few classes. In our app we however need to little bit...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.