473,387 Members | 1,798 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.

help understand source code in charpter 14.4 of accelerated C++

hi all,

i couldn't understand why code 1) can solve the problem of code 2) has.

code 1)
template<class T> T* clone(const T* tp)
{
return tp->clone();
}
and change our make_unique member to call it

template<class T>
void Ptr<T>::make_unique()
{
if (*refptr != 1) {
--*refptr;
refptr = new size_t(1);
p = p ? clone(p): 0; // call the global (not member) version
of clone
}
}




code 2)

template<class T>
void Ptr<T>::make_unique()
{
if (*refptr != 1) {
--*refptr;
refptr = new size_t(1);
p = p ? p->clone() : 0; // here is the problem
}
}
Look at the call to p->clone. Because we are using a Ptr< vector<char>
, this call will try to call the clone function that is a member of

vector<char>. Unfortunately, no such function exists!


template<class T> T* clone(const T* tp)
{
return tp->clone();
}

i think tp-clone() would has the same sematics of p->clone() since both
p and tp are of same type.
so I couldn't understand the difference. how it can solve the problem
of calls undefined member function?
thanks in advance.

baumann@pan

Jul 23 '05 #1
6 1363
* baumann@pan:

i couldn't understand why code 1) can solve the problem of code 2) has.

code 1)
template<class T> T* clone(const T* tp)
{
return tp->clone();
}
and change our make_unique member to call it

template<class T>
void Ptr<T>::make_unique()
{
if (*refptr != 1) {
--*refptr;
refptr = new size_t(1);
p = p ? clone(p): 0; // call the global (not member) version
of clone
}
} code 2)

template<class T>
void Ptr<T>::make_unique()
{
if (*refptr != 1) {
--*refptr;
refptr = new size_t(1);
p = p ? p->clone() : 0; // here is the problem
}
}
Look at the call to p->clone. Because we are using a Ptr< vector<char>
, this call will try to call the clone function that is a member of vector<char>. Unfortunately, no such function exists!

template<class T> T* clone(const T* tp)
{
return tp->clone();
}

i think tp-clone() would has the same sematics of p->clone() since both
p and tp are of same type.


Correct.

so I couldn't understand the difference. how it can solve the problem
of calls undefined member function?


The global template function can be specialized for the type T in
question, e.g.

typedef std::vector<char> CharVector;

template<> CharVector* clone( CharVector const* p )
{
return new CharVector( *p );
}

But I don't have the book and haven't read it, so it's impossible for
me to know whether I've understood correctly what the "problem" is, and
if so, whether the above was the authors' intention.

I think of such things as design: what mechanism, if any, should be used to
support non-intrusive cloning? The "problem" is then to choose between e.g.
a template function, as above, or an interface, or whatever. That's more of
a real "problem" because the solution space is infinite, and experience
says that your chances of first choosing an UnGood solution are 99.5%...

Another real "problem" is to know when to stop generalizing a solution and
adapting it for future reuse that maybe never will occur.

Much of that work _will_ be for nothing, and some of it may even be counter-
productive (e.g. higher complexity, lower flexibility) and the "problem" is
to strike a balance so that the work that does pay off pays enough to cover
also for the work that doesn't.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #2

baumann@pan wrote:
hi all,

i couldn't understand why code 1) can solve the problem of code 2) has.
code 1)
template<class T> T* clone(const T* tp)
{
return tp->clone();
}
and change our make_unique member to call it

template<class T>
void Ptr<T>::make_unique()
{
if (*refptr != 1) {
--*refptr;
refptr = new size_t(1);
p = p ? clone(p): 0; // call the global (not member) version of clone
}
}




code 2)

template<class T>
void Ptr<T>::make_unique()
{
if (*refptr != 1) {
--*refptr;
refptr = new size_t(1);
p = p ? p->clone() : 0; // here is the problem
}
}
Look at the call to p->clone. Because we are using a Ptr< vector<char>
, this call will try to call the clone function that is a member of

vector<char>. Unfortunately, no such function exists!


template<class T> T* clone(const T* tp)
{
return tp->clone();
}

i think tp-clone() would has the same sematics of p->clone() since

both p and tp are of same type.
so I couldn't understand the difference. how it can solve the problem
of calls undefined member function?
thanks in advance.

baumann@pan

my question is the global template clone also depends on object tp of
const T*, which means T should have member function clone.

is it right?
if right, i don't see any difference between the 2 codes .

Jul 23 '05 #3
baumann@pan wrote:

code 1)
template<class T> T* clone(const T* tp)
{
return tp->clone();
}


BTW, using clone() functions is bad style anyway because it makes the
caller responsible to release (delete) the resource (object).

R.C.

Jul 23 '05 #4
baumann@pan <ba*********@gmail.com> wrote:
so I couldn't understand the difference. how it can solve the problem
of calls undefined member function?


through overloading or specialization (former should be preferred) of
non-member function named "clone". Code presented in 2) does not have
any area for extension (e.g. you won't be able to provide clone
semantics for vector), while call to separate function allows you to
overload it for selected classes (e.g. vector), providing desired effect
for classes that do not have "clone" member function (or that have
"Clone" or something else instead).
B.

Jul 23 '05 #5

Bronek Kozicki wrote:
baumann@pan <ba*********@gmail.com> wrote:
so I couldn't understand the difference. how it can solve the problem of calls undefined member function?
through overloading or specialization (former should be preferred) of

non-member function named "clone". Code presented in 2) does not have any area for extension (e.g. you won't be able to provide clone
semantics for vector), while call to separate function allows you to
overload it for selected classes (e.g. vector), providing desired effect for classes that do not have "clone" member function (or that have
"Clone" or something else instead).

how to overload it for the classes which don't have clone member
function?


B.


Jul 23 '05 #6
baumann@pan <ba*********@gmail.com> wrote:
Bronek Kozicki wrote:
baumann@pan <ba*********@gmail.com> wrote:
so I couldn't understand the difference. how it can solve the
problem of calls undefined member function?


through overloading or specialization (former should be preferred) of

non-member function named "clone". Code presented in 2) does not have

any area for extension (e.g. you won't be able to provide clone
semantics for vector), while call to separate function allows you to
overload it for selected classes (e.g. vector), providing desired
effect for classes that do not have "clone" member function (or that
have "Clone" or something else instead).

how to overload it for the classes which don't have clone member
function?


// class has some other function providing similar functionality
Baz* clone(const Baz* tp)
{
std::auto_ptr<Baz> p = tp->MakeCopy();
return p.release();
}

// no clone functionality at all, class is not polymorphic
template <typename T>
std::vector<T>* clone(const std::vector<T>* tp)
{
return new std::vector<T>(*tp);
}

If class is polymorphic and does not provide anything similar to clone,
then such class is "not cloneable". Although one could write some
"clone" overload using copy constructor (as demonstrated above for
std::vector), it would not do the right thing - it would just slice.
Thus it is best not to define such overload for class that do not
provide this functionality and is polymorphic (i.e. cctor is not
applicable).
B.

Jul 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: abcd | last post by:
kutthaense Secretary Djetvedehald H. Rumsfeld legai predicted eventual vicmadhlary in Iraq mariyu Afghmadhlaistmadhla, kaani jetvedehly after "a ljetvedehg, hard slog," mariyu vede legai pressed...
31
by: da Vinci | last post by:
OK, this has got to be a simple one and yet I cannot find the answer in my textbook. How can I get a simple pause after an output line, that simply waits for any key to be pressed to move on? ...
6
by: TuPLaD | last post by:
Hi, i just started learning how to code OpenGL with the syntax of C++. But i got the following problem(YES IT IS C++ BUT ITS A OPENGL PROGRAM): ------------------ Quote From A Site...
17
by: Phil McKraken | last post by:
I am having a problem putting together a shopping cart with the below script. Everything displays fine, adds totals fine, and works perfect EXCEPT if you choose the 9.95 item #5 BY ITSELF the total...
21
by: Corey Dyke | last post by:
k here's the deal. im in desperate need of help with C#. i'm taking a course at DeVry now and we're doing C#. i've done C++ last semester, so i know i should be able to catch on to this stuff. ...
5
by: Y2J | last post by:
I am working through this book on C++ programming, the author is speaking of using linked lists. He gave and example which I found confusing to say the least. So I rewrote the example in a way that...
1
by: cakeathon | last post by:
I'm working my way through the accelerated C++ book, and exercise 10-5, & 10-6 have me stuck I have the follwing class in a header file: class String_list { public: String_list(); void...
7
by: DJ Dharme | last post by:
Hi, I really like to use stl as much as possible in my code. But I found it really hard to understand by looking into there source code. I have no idea about what iterator traits, heaps and...
7
by: sara | last post by:
I have a friend doing some pro-bono work for a non-profit that does job training for distressed kids under DCSS care. He asked me for code to do the following (he's using A2003). I can't find...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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.