473,836 Members | 1,412 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

copy ctor question

Given this class heirarchy

class Base{};

class A : public Base
{};

class B : public Base
{};

etc

How do I go about coding a copy ctor for the following class:

class SomeClass
{
std::vector<Bas e*> m_BasePointers;
};

when m_BasePointers contains pointers to child classes of Base?


thanks
Jul 19 '05
39 2897
WW
lilburne wrote:
If the method is virtual then you will normally be calling
it via base some class, in other words you'll be using late
binding. In such cases the compiler can't inline the call
because it doesn't know which derived class version of the
method to inject. Therefore, it uses the virtual calling
mechanism so your virtual method is not inlined. Secondly
the compiler needs to have a method to call so it injects a
static version of the method into the object file created by
each source file that reads the header, you might also get a
copy of the classes vtable injected into the object file too.
Which might happen anyways, depending on the compiler you are using.
Using inlines in general is a bit like writing:

if (expression) some_statement;

Try putting a break point on some_statement. Try puting a
break point on an inline method. You might be able to step
into one but you wont be able to break when the method is
called.
Again: this is compiler/debugger dependent.
In some cases where a method is small and heavily used then
inline will give some performance benefit. But the cases
where that is so is pretty limited.
With you compiler, perhaps.0
Then there is the obsfucation of the inline methods
cluttering up the class header files.
For those who are unable to read C++, perhaps.
When inlining is
heavily used the classes API becomes obscured, and when an
inline method is altered the world has to recompile.


Yes. And when should a Clone method be altered? When the class name
changes.

If you hate inline, hate them. But starting a ranting when someone uses
inline notation in _example_ code is plain silly.

--
WW aka Attila
Jul 19 '05 #21

"lilburne" <li******@godzi lla.net> wrote in message news:bl******** ****@ID-203936.news.uni-berlin.de...
Big mistake. Huge mistake. Adding a copy constructor to a class having no
controlled resources is a mistake. It's copy constructor will be identical
to the one generated by the compiler - at best. However it introduces
maintenance risk and nightmare for no value.


Illustrate one!


In order to implement a copy constructor, you must inturn implement copy semantics
for each member and base class. The compiler generated copy constructor isn't
just empty like the default destructor. This means that everytime someone adds
an element they need to bash your copy constructor (and copy assignment operator).
Just more code to maintain, and totally unnecessary if the bases and members already
do proper copy semantics themselves. This is preferential, use strings rather than char*,
vector rather than managing the arrays yourself, etc.. then you don't have to worry about
destruction and copying semantics, nor initializing them.
Jul 19 '05 #22

"lilburne" <li******@godzi lla.net> wrote in message
news:bl******** ****@ID-203936.news.uni-berlin.de...
jeffc wrote:
"lilburne" <li******@godzi lla.net> wrote in message
news:bl******** ****@ID-203936.news.uni-berlin.de...
Kick out the inlines though, virtual inlines are definitely
a mistake...
Why do you say that?


If the method is virtual then you will normally be calling
it via base some class, in other words you'll be using late
binding. In such cases the compiler can't inline the call
because it doesn't know which derived class version of the
method to inject.


Your second statement is true, but your premise is false. In general you
can't say if you'll be using late binding or not. Virtual functions only
allow late binding in situations where you want it. It's quite feasible
that most code does not exploit late binding, even if the functions are
virtual for other situations. Virtual inline allows for both cases -
virtual when polymorphism is used, and efficiency when it is not.
Using inlines in general is a bit like writing:

if (expression) some_statement;

Try putting a break point on some_statement. Try puting a
break point on an inline method.
This is completely unrelated to virtual methods, true though it might be.
In some cases where a method is small and heavily used then
inline will give some performance benefit. But the cases
where that is so is pretty limited.
Again that is completely unrelated to virtual functions. That is merely an
inline function issue.
Then there is the obsfucation of the inline methods
cluttering up the class header files. When inlining is
heavily used the classes API becomes obscured, and when an
inline method is altered the world has to recompile.


That is not only a matter of opinion, but also a matter of style. Inline
functions don't have to go in the header files. Even if they do, they don't
have to go in the class definitions. Again, this is totally unrelated to
virtual functions.
Jul 19 '05 #23
jeffc wrote:
"lilburne" <li******@godzi lla.net> wrote in message
news:bl******** ****@ID-203936.news.uni-berlin.de...

Why do you say that?


If the method is virtual then you will normally be calling
it via base some class, in other words you'll be using late
binding. In such cases the compiler can't inline the call
because it doesn't know which derived class version of the
method to inject.

Your second statement is true, but your premise is false. In general you
can't say if you'll be using late binding or not. Virtual functions only
allow late binding in situations where you want it. It's quite feasible
that most code does not exploit late binding, even if the functions are
virtual for other situations. Virtual inline allows for both cases -
virtual when polymorphism is used, and efficiency when it is not.


What! You reckon that polymorphic systems are written and
the bulk of the execution involves calling virtual methods
on variables whose static type is known at compile time?

Why not use C?
Using inlines in general is a bit like writing:

if (expression) some_statement;

Try putting a break point on some_statement. Try puting a
break point on an inline method.

This is completely unrelated to virtual methods, true though it might be.

What part of 'Using inlines in general' was hard for you.

In some cases where a method is small and heavily used then
inline will give some performance benefit. But the cases
where that is so is pretty limited.

Again that is completely unrelated to virtual functions. That is merely an
inline function issue.


Didn't you say earlier:

"Virtual inline allows for both cases - virtual when
polymorphism is used, and efficiency when it is not."

you appear very confused.

Then there is the obsfucation of the inline methods
cluttering up the class header files. When inlining is
heavily used the classes API becomes obscured, and when an
inline method is altered the world has to recompile.

That is not only a matter of opinion, but also a matter of style. Inline
functions don't have to go in the header files. Even if they do, they don't
have to go in the class definitions. Again, this is totally unrelated to
virtual functions.


True, but where shall they can go? Obviously not in the
source file that would be daft, perhaps a ... inline file.
So now to avoid cluttering the header we have two places to
go look for code for the class, and doubled the number of
include directives and make dependencies.

Jul 19 '05 #24
Ron Natalie wrote:
"lilburne" <li******@godzi lla.net> wrote in message news:bl******** ****@ID-203936.news.uni-berlin.de...

Big mistake. Huge mistake. Adding a copy constructor to a class having no
controlled resources is a mistake. It's copy constructor will be identical
to the one generated by the compiler - at best. However it introduces
maintenanc e risk and nightmare for no value.


Illustrate one!

In order to implement a copy constructor, you must inturn implement copy semantics
for each member and base class. The compiler generated copy constructor isn't
just empty like the default destructor. This means that everytime someone adds
an element they need to bash your copy constructor (and copy assignment operator).
Just more code to maintain, and totally unnecessary if the bases and members already
do proper copy semantics themselves. This is preferential, use strings rather than char*,
vector rather than managing the arrays yourself, etc.. then you don't have to worry about
destruction and copying semantics, nor initializing them.


Firstly you'll want to do some sanity checking on the
argument, some mutator on the class might have left you with
an empty string or vector, or some other violation of the
state invariant, you might as well catch it early.

Secondly you can't put a breakpoint on a compiler generated
method, and there *will* be times when you'll want to track
all ocassions where an object of your class is created. You
can't even #if the code out to see where the linker complains.

But this is all academic because you probably don't want
copy constructors for most classes anyway.

As an aside: WW's comment above that 'copy constructor will
be identical to the one generated by the compiler - at best'
isn't quite true. A profiling tool we use noticed that one
of the few compiler generated copy constructors in our
system was actually 7% slower than a hand rolled version,
*shrug*, you just never know where the bottlenecks are until
you go looking.
Jul 19 '05 #25
WW wrote:

If you hate inline, hate them. But starting a ranting when someone uses
inline notation in _example_ code is plain silly.


"Kick out the inlines though, virtual inlines are
definitely a mistake, and the rest are a waste of time,
and only serve to clutter your headers with
implementation details."

is hardly a rant.

Jul 19 '05 #26

"lilburne" <li******@godzi lla.net> wrote in message
news:bm******** ****@ID-203936.news.uni-berlin.de...

If the method is virtual then you will normally be calling
it via base some class, in other words you'll be using late
binding. In such cases the compiler can't inline the call
because it doesn't know which derived class version of the
method to inject.
Your second statement is true, but your premise is false. In general you
can't say if you'll be using late binding or not. Virtual functions only allow late binding in situations where you want it. It's quite feasible
that most code does not exploit late binding, even if the functions are
virtual for other situations. Virtual inline allows for both cases -
virtual when polymorphism is used, and efficiency when it is not.


What! You reckon that polymorphic systems are written and
the bulk of the execution involves calling virtual methods
on variables whose static type is known at compile time?


Certainly some have been. But whether or not the "bulk" of or "most" of the
code is like that isn't the point. The point is that most applications mix
both.
class A;
class B : public A {};
A a; // I know I have an A
B b; // I know I have a B
void f(&A); // I don't care which I have
void f(&B); // I know I have a B

Virtual functions are only used in one of those cases.
Why not use C?
Because C doesn't support classes or inheritance, not to mention
polymorphism when I want to use it.
Using inlines in general is a bit like writing:

if (expression) some_statement;

Try putting a break point on some_statement. Try puting a
break point on an inline method.


This is completely unrelated to virtual methods, true though it might be.
What part of 'Using inlines in general' was hard for you.


What part of sticking to the subject of virtual inlines was hard for you?
In some cases where a method is small and heavily used then
inline will give some performance benefit. But the cases
where that is so is pretty limited.


Again that is completely unrelated to virtual functions. That is merely an
inline function issue.


Didn't you say earlier:

"Virtual inline allows for both cases - virtual when
polymorphism is used, and efficiency when it is not."

you appear very confused.


Only to you. The point is, and always has been, that virtual inline
functions make sense.
Then there is the obsfucation of the inline methods
cluttering up the class header files. When inlining is
heavily used the classes API becomes obscured, and when an
inline method is altered the world has to recompile.


That is not only a matter of opinion, but also a matter of style. Inline functions don't have to go in the header files. Even if they do, they don't have to go in the class definitions. Again, this is totally unrelated to virtual functions.


True, but where shall they can go? Obviously not in the
source file that would be daft, perhaps a ... inline file.
So now to avoid cluttering the header we have two places to
go look for code for the class, and doubled the number of
include directives and make dependencies.


Again, it's a matte of opinion and style. You don't like it, some people
do. But the relevant point is that it's not a reason to avoid VIRTUAL
inline functions per se.
Jul 19 '05 #27
jeffc wrote:
"lilburne" <li******@godzi lla.net> wrote in message
news:bm******** ****@ID-203936.news.uni-berlin.de...

What! You reckon that polymorphic systems are written and
the bulk of the execution involves calling virtual methods
on variables whose static type is known at compile time?

Certainly some have been. But whether or not the "bulk" of or "most" of the
code is like that isn't the point. The point is that most applications mix
both.
class A;
class B : public A {};
A a; // I know I have an A
B b; // I know I have a B
void f(&A); // I don't care which I have
void f(&B); // I know I have a B

Virtual functions are only used in one of those cases.


The point of inlining is to gain some performance advantage,
and in order to do so you have to either make reading the
classes API more difficult by peppering it with
implementation details, or you have to contrive some other
scheme (seperate include file) to divorce the inline methods
from the class definition.

Is the complication worth the pain?
http://pcroot.cern.ch/TaligentDocs/T...WM/WM_120.html
http://pcroot.cern.ch/TaligentDocs/T...WM/WM_123.html

Didn't you say earlier:

"Virtual inline allows for both cases - virtual when
polymorphism is used, and efficiency when it is not."

you appear very confused.

Only to you. The point is, and always has been, that virtual inline
functions make sense.

They make sense syntactically but the compiler is only able
to inline them in special circumstances. So you have an
inline method with its associated pain, and even less chance
of the compiler being able to give you a performance boost,
then it might with a normal inline method.

Again, it's a matte of opinion and style. You don't like it, some people
do. But the relevant point is that it's not a reason to avoid VIRTUAL
inline functions per se.


Damn I never knew that where you put a brace could cause
such problems. Style indeed.

If you want a relevant point "Don't inline unless you can
show that the calling sequence is a bottleneck".
Jul 19 '05 #28

"lilburne" <li******@godzi lla.net> wrote in message
news:bm******** ****@ID-203936.news.uni-berlin.de...

Only to you. The point is, and always has been, that virtual inline
functions make sense.


They make sense syntactically but the compiler is only able
to inline them in special circumstances. So you have an
inline method with its associated pain, and even less chance
of the compiler being able to give you a performance boost,
then it might with a normal inline method.


It's not a question of "chance". When it is used polymorphically , it is not
inlined, and that is exactly what you want. You obviously have a distaste
for inline functions and have constantly steered this discussion toward
inline functions in general, which was never the issue. Regardless, you
said "virtual inlines are definitely a mistake", and you're wrong. They
make sense more than just syntactically. If inline functions make sense
(and of course they do), then virtual inline functions also make sense. If
you don't like them, then don't use them.
Jul 19 '05 #29
jeffc wrote:
"lilburne" <li******@godzi lla.net> wrote in message
news:bm******** ****@ID-203936.news.uni-berlin.de...
Only to you. The point is, and always has been, that virtual inline
functions make sense.


They make sense syntactically but the compiler is only able
to inline them in special circumstances. So you have an
inline method with its associated pain, and even less chance
of the compiler being able to give you a performance boost,
then it might with a normal inline method.

It's not a question of "chance". When it is used polymorphically , it is not
inlined, and that is exactly what you want. You obviously have a distaste
for inline functions and have constantly steered this discussion toward
inline functions in general, which was never the issue. Regardless, you
said "virtual inlines are definitely a mistake", and you're wrong. They
make sense more than just syntactically. If inline functions make sense
(and of course they do), then virtual inline functions also make sense. If
you don't like them, then don't use them.


Foremost a virtual inline method is an inline method, though
it might not be compiled inline. So it is appropriate to
discuss the problems with using inline methods per se. They
aren't free, and I believe you acknowledge that. You can't
put a breakpoint on their call, you can't alter the methods
implementation without require all clients to recompile, you
can't change the internal data that the method uses
without requiring all clients to recompile. You pay this
price irrespective of whether the compiler is able to inline
the method or not (inline after all is only a hint). If the
method is compiled inlined you may suffer code bloat and
excessive paging. So you ought to be fairly sure that your
inlining is actually going to be cost effective. Now with
virtual inlines the situations where the compiler can inline
the method is restricted to those situations where it knows
the static type at compile time. That means the method from
which the call is being made has to have created the object.

You'll be hard pushed to provide a realistic example of
where a virtual inline demonstrates a measurable performance
gain, which can't be obtained by means other than using
inline. You can even choose what % performance gain you
think would be reasonable. Until then "virtual inlines are
definitely a mistake".

Note the Clone() example that started this off is a dreadful
example of an inline virtual, because if the compiler can
inline it, a simple copy construction could have been used.

Jul 19 '05 #30

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

Similar topics

11
2349
by: Sam Wilson [Bentley] | last post by:
If you pass a C++ object by value as an argument to a function which has a variable-length argument list (...), the MSVC7 C++ compiler does not call the object's copy constructor and will not complain if the copy constructor is private. 1) Is this part of the C++ language definition? What is the thinking behind it? 2) In any case, how can I catch at compile time any callers that try to
5
4248
by: kalita | last post by:
template<class T> class A { template<class Y> A(const A<Y> &) { // whatever } };
5
1700
by: nagrik | last post by:
Hello group, Last week I picked up a thread, which pointed out that if a copy constructor is created with pointers instead of reference, there is a danger of it going in infinite recursion. My observation: 1. Compiler does not complain.
1
2130
by: blangela | last post by:
3.0 Advanced Topic Addendum There are a few cases where the C++ compiler cannot provide an overloaded assignment operator for your class. If your class contains a const member or/and a reference member, the compiler will not be able to synthesize an assignment operator for your class. It actually helps to think of a reference member as a const member (since it cannot be made to reference any other object once it has been initialized). ...
10
4033
by: campos | last post by:
"Effective C++ 3rd Edition" Item 6, P39 ------------------------------------------------------- class Uncopyable { protected: // allow construction Uncopyable() {} // and destruction of ~Uncopyable() {} // derived objects... private: Uncopyable(const Uncopyable&); // ...but prevent copying
13
2486
by: Jeroen | last post by:
Hi all, I'm trying to implement a certain class but I have problems regarding the copy ctor. I'll try to explain this as good as possible and show what I tried thusfar. Because it's not about a certain code syntax but more a 'code architecture' thing , I'll use simple example classes (which are certainly not complete or working...) just to illustrate the idea (and I may make some mistakes because I'm not that experienced...). The...
6
1582
by: Richard Thompson | last post by:
Hi - I have a program which was previously working (but wasn't well tested). I've added a new function call, and it now doesn't compile. The call requires a copy constructor, but the compiler appears to think that it actually calls one of the *other* constructors. The copy ctor would work in this context, but the other ctor can't be used (it leads to a template instantiation error, which the compiler is reporting).
3
2060
by: subramanian100in | last post by:
If we provide any ctor for a class the compiler does not supply the default ctor. However if we do not provide the copy ctor but provide any other ctor, the compiler still supplies the copy ctor. Why doesn't the compiler supply the default ctor but still supplies the copy ctor when we have defined any other ctor ? Kindly explain
2
2169
by: subramanian100in | last post by:
If we do not provide any ctor for a class, the compiler provides the default ctor and copy ctor if needed. Consider a class Test. Suppose we provide some ctor in class Test but do not provide the default ctor. Suppose we try to create Test obj;
11
2294
by: Dijkstra | last post by:
Hi folks! First, this is the code I'm using to expose the problem: ------------------------------------------------------------------ #include <functional> #include <string> #include <iostream> using namespace std;
0
9671
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10845
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10592
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9376
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6979
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5828
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4456
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4019
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3116
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.