473,379 Members | 1,216 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,379 software developers and data experts.

questions from strategies and tactics C++


I have recently decided to go over the questions in the classical book
by Murray and few of them admittedly befuddled me.

page 140:

1. Under what circumstances should a smart compiler be able to use
static binding for a virtual function call? (is it when base class
pointer refers to base class object?)

2.Does it ever make sense to declare an inline virtual function? Under
what circumstances (is in it a contradiction unless it relates to my
answer to above question)?

3. Under what circumstances might be very costly to make a large
function virtual?

Jul 23 '05 #1
6 1528
* puzzlecracker:

I have recently decided to go over the questions in the classical book
by Murray and few of them admittedly befuddled me.

page 140:

1. Under what circumstances should a smart compiler be able to use
static binding for a virtual function call? (is it when base class
pointer refers to base class object?)
When the compiler can determine which function will be executed. A dumb
compiler can do that for

A o;
o.f();

A smart compiler can do it also for a range of other cases.

The time spent in analysis go quickly up, however.

2.Does it ever make sense to declare an inline virtual function?
Yes.

Under
what circumstances (is in it a contradiction unless it relates to my
answer to above question)?
Any circumstances, it's mostly a personal preference. Circumstances
where it isn't a good idea: when the project/firm's coding guidelines
say otherwise. No technical reasons.

3. Under what circumstances might be very costly to make a large
function virtual?


The direct (relative) cost of virtualness has nothing to do with the size of
a function but with the execution time and frequency of calls. So this one
has me befuddled, too. I'm guessing that the authors earlier have given
some inane advice like "prefentially don't make small functions virtual",
and here are trying to make the earlier advice seem sort of rational.

--
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
#include <iostream>

class Base {
public:
Base(const Base &) { std::cout << "copy is made" << std::endl; }
virtual ~Base() {}
virtual void foo() { std::cout << "Base" << std::endl; }
};

class Derived : public Base {
public:
virtual void foo() { std::cout << "Derived" << std::endl; }
};

void f(Base *p, Base obj, Base &ref) {
p->foo();
obj.foo();
ref.foo();
}

int main() {
Derived d;
f(&d, d, d);
return 0;
}

f accepts a pointer, an object, and a reference to Base. However, one
of these guarentees that Base::foo is called eventhough d, a Derived
object, is passed, and so the compiler can optimize it. perhaps this
example program will help you answer things correctly.

Jul 23 '05 #3
> 2.Does it ever make sense to declare an inline virtual function?
Yes.

I don't think a compiler can inline a virtual function (unless it is an
object -not a pointer to- or a pointer to a class instance of the same
class (A a=new A; a.foo();) ), so declaring it as inline doesn't make
sense from efficiency point of view (much less from aesthetic one) -
the function simply won't be inlined. aka. static vs. dynamic
binding... .

On the same note: can you have or inline a virtual template *member*
function?

Jul 23 '05 #4
* puzzlecracker:
[responding to my posting but referring to another posting, and not
stating who he's quoting etc. -- fixed:]
* Alf P. Steinbach:
* puzzlecracker:

Does it ever make sense to declare an inline virtual function?
Yes.


Your quoting and article references are inconsistent; please sharpen up.

I don't think a compiler can inline a virtual function (unless it is an
object -not a pointer to- or a pointer to a class instance of the same
class (A a=new A; a.foo();) ), so declaring it as inline doesn't make
sense from efficiency point of view (much less from aesthetic one) -
the function simply won't be inlined. aka. static vs. dynamic
binding... .
Sorry, that's incorrect: a compiler can and will inline a virtual function
in the same way as other functions, when it knows which function will be
executed.

Also note that the keyword 'inline', or placing a member function definition
inside the class definition, has very little to do with execution efficiency
or machine-code level inlining.

You should not rely on 'inline' as an optimization feature; that's not what
it's for.

On the same note: can you have or inline a virtual template *member*
function?


In C++ you cannot templatize a virtual function.

That's because a template function defines an infinite set of potential
functions, and dealing with the unknown set was and largely still is beyond
the compiler/linker technology C++ rests on in practice.

--
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 #5
In C++ you cannot templatize a virtual function.

That's because a template function defines an infinite set of potential

functions, and dealing with the unknown set was and largely still is
beyond
the compiler/linker technology C++ rests on in practice.

will (or should) a compiler complain or ignore the virtual aspects
(not vtable to be created if this the only v-function defined) if it
see virtual templ. mem. function?

Jul 23 '05 #6
* puzzlecracker:
In C++ you cannot templatize a virtual function.

That's because a template function defines an infinite set of potential

functions, and dealing with the unknown set was and largely still is
beyond
the compiler/linker technology C++ rests on in practice.

will (or should) a compiler complain or ignore the virtual aspects
(not vtable to be created if this the only v-function defined) if it
see virtual templ. mem. function?


Could you please fix your quoting? It's really not acceptable. One easy
way is to download a news client such as (if you're running Windows) Forté
Free Agent, set up an account with a free news-server provider, and voilà.

To repeat myself: in C++ you cannot templatize a virtual function.

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

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

Similar topics

0
by: Guyon Morée | last post by:
Hi, I am planning on creating a system using a xml rpc webservice. Simple XML RPC examples are easy to find on the web, but I am actually looking for some strategies or 'best practices'...
49
by: Paul Rubin | last post by:
I've started a few threads before on object persistence in medium to high end server apps. This one is about low end apps, for example, a simple cgi on a personal web site that might get a dozen...
13
by: Randy Webb | last post by:
Just for my own curiosity, what ever happend to the Java Applet that was referenced in the FAQ along with the HTTPRequestObject? http://jibbering.com/faq/#FAQ4_34 And in...
2
by: robin | last post by:
The Oblique Strategies were originally a set of one-hundred cards, each bearing a short phrase. They were devised by Brian Eno and Peter Schmidt as ways of working through creative problems. When a...
0
by: Net Virtual Mailing Lists | last post by:
I've been spending the last few days converting many databases into a single schema and have completed the process, but now I'm at somewhat of an impasse as to the best way to proceed forward.... ...
30
by: James Conrad StJohn Foreman | last post by:
After 3 years of using DB2 on Linux, I'm leaving my current employers to go work for a SQL Server shop instead. In order to find my replacement, they're trying to put together a set of questions...
13
by: CoreyWhite | last post by:
When playing games, perhaps the most simple is tic-tac-toe. The game has two simple strategies, one is defensive and the other offensive. It is not hard at first to learn how to tie games when...
0
by: kesterson.al | last post by:
fallout tactics patch http://cracks.00bp.com F R E E
0
by: origami.takarana | last post by:
Intrusion Detection Strategies ----------------------------------- Until now, we’ve primarily discussed monitoring in how it relates to intrusion detection, but there’s more to an overall...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: 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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.