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

Java-like interfaces in c++

I'm trying to make something like a Java interface. I have a class, that
needs a distance measure. The distance measure is itself a class passed
to the constructor:

MyClass::MyClass(vector v, DistanceMeasure dm)...

Then I have different specific distance measures inheriting from
DistanceMeasure (fx. CosDistance), and I would like to be able to do fx.

CosDisance cs;
Vector v;
MyClass ms(v, cs);

If I make the methods in DistanceMeasure virtual, I'm told by the
linker, that it's an undefined reference. If I make them pure virtual,
I'm told that the constructor declaration above is illegal, because dm
is abstract. If I provide a dummy implementation in DistanceMeasure it's
the DistanceMeasure methods that are called from MyClass and not the
overriding methods from CosDistance.

How can I specify an interface for a distance measure, and make MyClass
indifferent to the class implementing the interface, and use the methods
from the class provided in the constructor???

I'm not that familiar with c++, so any help or ideas would be highly
appreciated.

Morten
Mar 30 '07 #1
4 4022
On Mar 30, 8:31 am, Morten Lemvigh <lemv...@studentergaarden.dk>
wrote:
I'm trying to make something like a Java interface. I have a class, that
needs a distance measure. The distance measure is itself a class passed
to the constructor:

MyClass::MyClass(vector v, DistanceMeasure dm)...

Then I have different specific distance measures inheriting from
DistanceMeasure (fx. CosDistance), and I would like to be able to do fx.

CosDisance cs;
Vector v;
MyClass ms(v, cs);

If I make the methods in DistanceMeasure virtual, I'm told by the
linker, that it's an undefined reference. If I make them pure virtual,
I'm told that the constructor declaration above is illegal, because dm
is abstract. If I provide a dummy implementation in DistanceMeasure it's
the DistanceMeasure methods that are called from MyClass and not the
overriding methods from CosDistance.

How can I specify an interface for a distance measure, and make MyClass
indifferent to the class implementing the interface, and use the methods
from the class provided in the constructor???
Use pointers.

class DistanceMeasure {
public:
virtual long getDistance() = 0;
};

class CosDistance : public DistanceMeasure {
public:
virtual long getDistance() { return 1; }
};

class MyClass {
public:
MyClass::MyClass(DistanceMeasure * dm) {}
};

int main()
{
DistanceMeasure * cs = new CosDistance();
MyClass myClass(cs);

return 0;
}

Mar 30 '07 #2
Morten Lemvigh wrote:
I'm trying to make something like a Java interface. I have a class,
that needs a distance measure. The distance measure is itself a class
passed to the constructor:

MyClass::MyClass(vector v, DistanceMeasure dm)...
Pass by reference (to const if no changes are made).
[..]

I'm not that familiar with c++, so any help or ideas would be highly
appreciated.
*Get* familiar. C++ is not something you can do "by the way".

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Mar 30 '07 #3
* Morten Lemvigh:
I'm trying to make something like a Java interface. I have a class, that
needs a distance measure. The distance measure is itself a class passed
to the constructor:

MyClass::MyClass(vector v, DistanceMeasure dm)...

Then I have different specific distance measures inheriting from
DistanceMeasure (fx. CosDistance), and I would like to be able to do fx.

CosDisance cs;
Vector v;
MyClass ms(v, cs);

If I make the methods in DistanceMeasure virtual, I'm told by the
linker, that it's an undefined reference.
You forgot to link in a definition of the function in question.

If I make them pure virtual,
I'm told that the constructor declaration above is illegal, because dm
is abstract.
You forgot to make the constructor argument a reference. What you have
is a copy of the DistanceMeasure part, of type DistanceMeasure. You
can't make an instance of an abstract class.

If I provide a dummy implementation in DistanceMeasure it's
the DistanceMeasure methods that are called from MyClass and not the
overriding methods from CosDistance.
You forgot to make the constructor argument a reference. What you have
is a copy of the DistanceMeasure part, of type DistanceMeasure. For a
non-abstract DistanceMeasure that results in a slice operation.
How can I specify an interface for a distance measure, and make MyClass
indifferent to the class implementing the interface, and use the methods
from the class provided in the constructor???

I'm not that familiar with c++, so any help or ideas would be highly
appreciated.
It's a very large design space:

* Run-time versus compile-time specification.

* Identification by type (infinite variation possible) versus values.

* Different measures per MyClass instance or per class.

For the case of run-time identification of distance measure per instance:

* Passing in identifying value.
* Passing in factory object.
* Passing in std::auto_ptr (ownership transfer), or other smart
pointer.
* Passing in ref to object managed by client code.
* Passing in a callback.

Whether any of these solutions are reasonable depends on what you want
to achieve.

"Java interface" is something else entirely, but given that you have a
Java background, you may find it easiest to do the smart pointer thing.

For that you should get yourself the Boost library's smart pointer
collection (can be used without compiling the library, all code in
header files) and use boost::shared_ptr.

--
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?
Mar 30 '07 #4
da***********@fastmail.fm wrote:
On Mar 30, 8:31 am, Morten Lemvigh <lemv...@studentergaarden.dk>
wrote:
>I'm trying to make something like a Java interface. I have a class, that
needs a distance measure. The distance measure is itself a class passed
to the constructor:

MyClass::MyClass(vector v, DistanceMeasure dm)...

Then I have different specific distance measures inheriting from
DistanceMeasure (fx. CosDistance), and I would like to be able to do fx.

CosDisance cs;
Vector v;
MyClass ms(v, cs);

If I make the methods in DistanceMeasure virtual, I'm told by the
linker, that it's an undefined reference. If I make them pure virtual,
I'm told that the constructor declaration above is illegal, because dm
is abstract. If I provide a dummy implementation in DistanceMeasure it's
the DistanceMeasure methods that are called from MyClass and not the
overriding methods from CosDistance.

How can I specify an interface for a distance measure, and make MyClass
indifferent to the class implementing the interface, and use the methods
from the class provided in the constructor???

Use pointers.

class DistanceMeasure {
public:
virtual long getDistance() = 0;
};

class CosDistance : public DistanceMeasure {
public:
virtual long getDistance() { return 1; }
};

class MyClass {
public:
MyClass::MyClass(DistanceMeasure * dm) {}
};

int main()
{
DistanceMeasure * cs = new CosDistance();
MyClass myClass(cs);

return 0;
}
Thanks! That was just, what I needed.

Morten
Mar 30 '07 #5

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

Similar topics

0
by: Ravi Tallury | last post by:
Hi We are having issues with our application, certain portions of it stop responding while the rest of the application is fine. I am attaching the Java Core dump. If someone can let me know what...
1
by: ptaz | last post by:
Hi I'm trying to run a web page but I get the following error. Ca anyone please tell me a solution to this. Thanks Ptaz HTTP Status 500 - type Exception report
0
by: Markus Wollny | last post by:
Hello! When I try to run ./configure --with-java, it complains that ant doesn't work. However ant is installed, as is the latest Java SDK 1.4.2 from sun, PATH and JAVA_HOME are set correctly; ...
0
by: mailkhurana | last post by:
Hii , I am trying to use a type 2 driver to connect to DB2 0n AIX 5 I have a small java test to class to establish a conneciton with the db .. I am NOT using WAS or any appserver When I try to...
12
by: Mark Fink | last post by:
I wrote a Jython class that inherits from a Java class and (thats the plan) overrides one method. Everything should stay the same. If I run this nothing happens whereas if I run the Java class it...
0
by: jaywak | last post by:
Just tried running some code on Linux (2.4.21-32.0.1.EL and Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_04-b05)) and Windows XPSP2 (with Java HotSpot(TM) Client VM (build...
0
oll3i
by: oll3i | last post by:
package library.common; import java.sql.ResultSet; public interface LibraryInterface { public ResultSet getBookByAuthor(String author); public ResultSet getBookByName(String name);
7
oll3i
by: oll3i | last post by:
i need to write publisher subscriber application on openjms and use db4o but i dont know how do i add the references to db4o when i compile the app with javac ? i added db4o-6.1-java5.jar to my...
1
by: henrymania | last post by:
Am writing a code for database backup....by backupservlet is as given below i get the following exception
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: 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
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: 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,...

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.