473,412 Members | 2,304 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,412 software developers and data experts.

Simulating polymorphism without the overhead of virtual methods?

jpr
hello ...

I'm translating a compressed graph class (for large graphs) from Java
to C++. Currently I'm writing a function that should return an iterator
over the successors of a given node. These successors are represented
by int's.

Depending on the way the graph is compressed, I might have to extract
the successors from the bitstream holding the graph in several ways.
Because of this, the Java version had four different iterator classes,
all extending a common base class; it would construct and return the
appropriate one.

I'd like to avoid having to declare my iterator members virtual because
of the overhead of a virtual function call (and the missed chance to
inline). However, I want to be able to return a single type to hide any
detail of how I come up with these types from the client.

Is there a trick to do this sort of thing, or am I chasing a hopeless
dream?

thanks!
-jacob

Mar 25 '06 #1
2 1759
jpr wrote:
Is there a trick to do this sort of thing, or am I chasing a hopeless
dream?


Get your C++ code working, with unit tests. Then profile it to see where the
real bottlenecks are; not the ones you guess will be. They might surprise
you. And there also might not be any.

Use the unit tests to time things, and to preserve behavior while you adjust
the bottlenecks and tune things.

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Mar 25 '06 #2
Phlip wrote:
jpr wrote:
Is there a trick to do this sort of thing, or am I chasing a hopeless
dream?


Get your C++ code working, with unit tests. Then profile it to see where the
real bottlenecks are; not the ones you guess will be. They might surprise
you. And there also might not be any.

Use the unit tests to time things, and to preserve behavior while you adjust
the bottlenecks and tune things.


Thank you, thank you, THANK YOU!!!!!!!!!!

I get so sick of seeing posts here where people are so overly concerned
about "the most efficient" way to do something, without having
benchmarked it. They seem to forget Hoare's/Knuth's law: "Premature
optimization is the root of all evil."

Your answer was the best response to this that I have seen. Mine tend
to be snarky, alas.

Thank you again!
Mar 26 '06 #3

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

Similar topics

37
by: Mike Meng | last post by:
hi all, I'm a newbie Python programmer with a C++ brain inside. I have a lightweight framework in which I design a base class and expect user to extend. In other part of the framework, I heavily...
6
by: john sun | last post by:
Hello, I am not newbie C++ developer infact :). But till recently I would like dig up more about C++. I know those OO details. But when people talking OO they focused on the polymorphism, and...
4
by: Leslaw Bieniasz | last post by:
Cracow, 20.09.2004 Hello, I need to implement a library containing a hierarchy of classes together with some binary operations on objects. To fix attention, let me assume that it is a...
4
by: Leslaw Bieniasz | last post by:
Cracow, 20.10.2004 Hello, As far as I understand, the generic programming basically consists in using templates for achieving a static polymorphism of the various code fragments, and their...
12
by: Steve Jorgensen | last post by:
The classing Visual Basic and VBA support for polymorphism, let's face it, is a bit on the weak side, and built-in support for inheritance is non-existent. This little essay is about some patterns...
4
by: LP | last post by:
Hi, I understand the concept/definition of polymorphism. But what does the term "runtime polymorphism" mean? I was asked to define it during a technical interview. I gave a guy vanilla definition...
18
by: Seigfried | last post by:
I have to write a paper about object oriented programming and I'm doing some reading to make sure I understand it. In a book I'm reading, however, polymorphism is defined as: "the ability of two...
11
by: chsalvia | last post by:
I've been programming in C++ for a little over 2 years, and I still find myself wondering when I should use polymorphism. Some people claim that polymorphism is such an integral part of C++,...
1
weaknessforcats
by: weaknessforcats | last post by:
Introduction Polymorphism is the official term for Object-Oriented Programming (OOP). Polymorphism is implemented in C++ by virtual functions. This article uses a simple example hierarchy which...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
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,...
0
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...

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.