473,799 Members | 3,061 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Code specific to couples of classes

dl
I'll try to clarify the cryptic subject line.

Let's say I have a base class 'GeometricObjec t' with a virtual method
'double distance (const GeometricObject &) const'.

Among the derived classes I have eg 'Segment', representing a segment
of a line, and 'Arc', representing an arc of circle.

What is the right place to put the code for computing the distance
between a Segment and an Arc? It is not specific to the Segment nor to
the Arc, but to the coupling of the two objects.

Thank you,

Daniele

Mar 4 '07 #1
19 1835
dl wrote:
I'll try to clarify the cryptic subject line.

Let's say I have a base class 'GeometricObjec t' with a virtual method
'double distance (const GeometricObject &) const'.

Among the derived classes I have eg 'Segment', representing a segment
of a line, and 'Arc', representing an arc of circle.

What is the right place to put the code for computing the distance
between a Segment and an Arc? It is not specific to the Segment nor to
the Arc, but to the coupling of the two objects.
How about a global function, perhaps contained in the same
namespace your classes are in.

HTH,
- J.
Mar 4 '07 #2
dl
On 4 Mar, 18:05, Jacek Dziedzic <jacek.dziedzic .n.o.s.p....@gm ail.com>
wrote:
How about a global function, perhaps contained in the same
namespace your classes are in.
Thank you.

Yes it works. But is it "the right place"?

Mar 4 '07 #3
dl wrote:
On 4 Mar, 18:05, Jacek Dziedzic <jacek.dziedzic .n.o.s.p....@gm ail.com>
wrote:
> How about a global function, perhaps contained in the same
namespace your classes are in.

Thank you.

Yes it works. But is it "the right place"?
Absolutely.

If you want some authoritative word on that try Item 23 of Scott Meyer's
Effective C++: "Prefer non-member non-friend functions to member functions."

--
Alan Johnson
Mar 4 '07 #4
dl
On 4 Mar, 21:36, Alan Johnson <a...@yahoo.com wrote:
dl wrote:
On 4 Mar, 18:05, Jacek Dziedzic <jacek.dziedzic .n.o.s.p....@gm ail.com>
wrote:
How about a global function, perhaps contained in the same
namespace your classes are in.
Thank you.
Yes it works. But is it "the right place"?

Absolutely.

If you want some authoritative word on that try Item 23 of Scott Meyer's
Effective C++: "Prefer non-member non-friend functions to member functions."
Thank you.

I don't know this book. I will get a copy.

Mar 4 '07 #5
Alan Johnson wrote:
>
>>How about a global function, perhaps contained in the same
namespace your classes are in.

Yes it works. But is it "the right place"?

Absolutely.

If you want some authoritative word on that try Item 23 of Scott Meyer's
Effective C++: "Prefer non-member non-friend functions to member
functions."
I do not know what Scott Meyer meant, but think, it is not general rule.
Member function can have encapsulated data so member function can be better
than plain function.

OP note, that the function "distance" can be shared between several classes,
else more than one copy of code must be at least maintained.

With "copy of code" in fact we are speaking about implementation of the
function "distance". So we need to have one implementation of the function
for all possible declarations of the function, in other words we need
abstract function declaration (interface) to be separated from the function
implementation.

In our case we have interface completely separated from its implementation.
In general case we can have only part of implementation of the interface
shared between several interfaces.

Let we will make the shared implementation as plain function. The plane
function (even placed into own namespace) does not allow us
- to use inheritance as design way to make new improved version of the plain
function,
- to have runtime template with the help of virtual function,
- to hide some data with the function
- and so on.

There are two design patterns existing to help to implement similar
separations: "bridge" and "strategy". The "strategy" can be useful design
pattern in our case.

Also free-standing class with inline members can be good solution instead of
"plane function placed into own namespace". There are no overhead here.

You also can declare inline member function in each class (if you want) to
compare with other class and make forward to its implementation. Consider
the following example:

-- cut here --

namespace Ndecl
{

class type{};

//
template<class First, class Second>
class Dist_implementa tion
{
public:
inline type distance( const First&, const Second& );
inline type distance( const Second&, const First& );
};

//
template<class First, class Second>
inline type distance( const First& f, const Second& s )
{
Dist_implementa tion<First,Seco nd di;
di.distance(f,s );
}

template<class First, class Second>
inline type distance( const Second& s, const First& f )
{
Dist_implementa tion<First,Seco nd di;
di.distance(s,f );
}

//namespace Ndecl
}

/*

//*************** *************** *
-= C++ limitation =-
"define" does not exist as template parameter,
so can not do like this:

//declaration
template<define Dist_implementa tion>
class GeometricObject
{
protected:
template<class First, class Second>
type distance(const Second& obj)
{

//with "define" we avoid here
//"Dist_implement ation is not template" problem
Dist_implementa tion<First,Seco nd di;

return
di.distance
(
static_cast<con st First&>(*this),
obj
);
}
};

//inheritance
template<define Dist_implementa tion>
class Segment:
public GeometricObject <Dist_implement ation>
{
typedef GeometricObject <Dist_implement ationTparent;
using Tparent::distan ce;

public:
template<class Second>
type distance(const Second& obj)
{
return
//-= probably g++ limitation =- : Tparent::distan ce<Segment,Seco nd>
static_cast<Tpa rent&>(*this).t emplate
distance<Segmen t,Second>
(
obj
);
}
};

//instantiating
Segment<Ndecl:: Dist_implementa tion seg;

//implementation Ndecl::Dist_imp lementation

-=C++ limitation=-
can not point to itself in template parameter like this

template<>type
Dist_implementa tion<
class Segment<Dist_im plementation>,
class Arc<Dist_implem entation>
>::
distance(
const Segment<Dist_im plementation>&,
const Arc<Dist_implem entation>&
)
{
}

so we are forced to use other namespace and
inherit only to make link to itself

namespace Nimpl
{
using Ndecl::Dist_imp lementation;

class GeometricObject :
public Ndecl::Geometri cObject
<
Ndecl::Dist_imp lementation
>
{
....
};

class Segment:
public Ndecl::Segment
<
Ndecl::Dist_imp lementation
>
{
....
};

class Arc:
public Ndecl::Arc
<
Ndecl::Dist_imp lementation
>
{
....
};

//namespace Nimpl
}

// *************** *************** *************

so in order to separate "Dist_implement ation" and
"GeometricObjec t" we must use other namespace and
"using" directive or must use "#define" directive
of preprocessor

*/

namespace Ndecl2
{
using Ndecl::Dist_imp lementation;
using Ndecl::type;

//
class GeometricObject
{
//can not declare here and it is correct
//Dist_implementa tion di;

protected:
template<class First, class Second>
type distance(const Second& obj)
{
//can be worse than global data due to ctor/dtor calls
Dist_implementa tion<First,Seco nd di;

return
di.distance(
static_cast<con st First&>(*this),
obj
);
}
};

// ***
//
class Segment:
public GeometricObject
{
public:
template<class Second>
type distance(const Second& obj)
{
return
GeometricObject ::distance<Segm ent,Second>
(
obj
);
}
};

//
class Arc:
public GeometricObject
{
public:
template<class Second>
type distance(const Second& obj)
{
return
GeometricObject ::distance<Arc, Second>
(
obj
);
}
};

//namespace Ndecl2
}

//specialization
namespace Ndecl
{
using Ndecl2::Arc;
using Ndecl2::Segment ;

//
template<>type
Dist_implementa tion<Arc,Segmen t>::
distance
(
const Arc&,
const Segment&
)
{
//your "distance" for Arc& and Segment& here
return type();
}

//
template<>type
Dist_implementa tion<Arc,Segmen t>::
distance
(
const Segment& s,
const Arc& a
)
{
return distance(a,s);
}

/*
-=C++ limitation=-
can not declare comutative template parameters like this

template<>
class Dist_implementa tion<Segment,Ar c>
is alias Dist_implementa tion<Arc,Segmen t>;

It is maybe correct, I think C++ must not have complicated
template design stuff integrated to itself, but C++ must
have only integrated bidirectional bridge to the external
stuffs like this.

It is due to the possible integrated design stuff can turn C++
into "langauge of exceptions from self rules", but
simultaneosly the integrated stuff always will be limited
by ordinary C++ syntax and that is why can be unsuitable
for concrete application domain.

*/

template<>
class Dist_implementa tion<Segment,Ar c>
{
Dist_implementa tion<Arc,Segmen t di;

public:
type distance( const Segment& s, const Arc& a )
{
return di.distance(s,a );
}

type distance( const Arc& a, const Segment& s )
{
return di.distance(a,s );
}

};

//namespace Ndecl
}
// *************** *************** ***************
//
int main()
{
using namespace Ndecl2;

Segment seg;
Arc arc;

//the following are equal

seg.distance<Ar c>(arc);
arc.distance<Se gment>(seg);

Ndecl::Dist_imp lementation<Arc ,Segment di;
di.distance(arc ,seg);

Ndecl::distance <Arc,Segment>(a rc,seg);
}

-- cut here --
--
Maksim A. Polyanin
http://grizlyk1.narod.ru/cpp_new

"In thi world of fairy tales rolls are liked olso"
/Gnume/
Mar 5 '07 #6
On Mar 4, 11:32 am, "dl" <daniele.lu...@ general-logic.itwrote:
Let's say I have a base class 'GeometricObjec t' with a virtual method
'double distance (const GeometricObject &) const'.

Among the derived classes I have eg 'Segment', representing a segment
of a line, and 'Arc', representing an arc of circle.

What is the right place to put the code for computing the distance
between a Segment and an Arc? It is not specific to the Segment nor to
the Arc, but to the coupling of the two objects.
Seems that to compute the distance between any two GeometricObject s
you would need to know the center point of each, which could be stored
and fetched at the base class level. If that's the case, the distance
function could be defined there as well.

Mar 5 '07 #7
On 5 Mar, 13:41, "Grizlyk" <grizl...@yande x.ruwrote:
Alan Johnson wrote:
If you want some authoritative word on that try Item 23 of Scott Meyer's
Effective C++: "Prefer non-member non-friend functions to member
functions."

I do not know what Scott Meyer meant
He meant this
http://www.ddj.com/dept/cpp/184401197

Gavin Deane

Mar 5 '07 #8
dl
On 5 Mar, 14:59, dave_mikes...@f astmail.fm wrote:
On Mar 4, 11:32 am, "dl" <daniele.lu...@ general-logic.itwrote:
Let's say I have a base class 'GeometricObjec t' with a virtual method
'double distance (const GeometricObject &) const'.
Among the derived classes I have eg 'Segment', representing a segment
of a line, and 'Arc', representing an arc of circle.
What is the right place to put the code for computing the distance
between a Segment and an Arc? It is not specific to the Segment nor to
the Arc, but to the coupling of the two objects.

Seems that to compute the distance between any two GeometricObject s
you would need to know the center point of each, which could be stored
and fetched at the base class level. If that's the case, the distance
function could be defined there as well.
Not so if for distance you mean 'the shortest distance between any
point on the arc and any point on the segment'. Anyway this is
geometry, not c++. There are plenty of different examples, not
geometric, where you need to write code specific to the coupling of
class A with class B.

Anyway I think this is a limitation of the OO approach, not of c++.
Or maybe 'limitation' is too strong; just something not fitting well
in OO.

Thank you all,

Daniele

Mar 5 '07 #9
Gavin Deane wrote:
>
>Alan Johnson wrote:
If you want some authoritative word on that try Item 23 of Scott
Meyer's
Effective C++: "Prefer non-member non-friend functions to member
functions."

I do not know what Scott Meyer meant but think, it is not general rule.
Member function can have encapsulated data so member function can be
better than plain function.

He meant this
http://www.ddj.com/dept/cpp/184401197

1.
He has written: "It's important that we're trying to choose between member
functions and non-friend non-member functions".

As i can understand "non-friend non-member function" is a function, that can
use only public interface of the class. It is unsuitable rule for our
example, as i can guess.
2.
He has written: "... Wombat clients often need a number of convenience
functions related to eating, sleeping, and breeding. Such convenience
functions are by definition _not strictly necessary_."

From the first look on the page i think, that Scott Meyer can mix in
reader's mind interface and implementation.

Somebody could think, that if implementation of a member can be separated
from class, than interface _must_ be separated also (member removed as _not
strictly necessary_). It is not true in general.

For ordinary OO design we are jumping from interface, without taking
implementation into account. We must not try to find effective
implementation befor interface has been logically completed.

Probably he is speaking about the case, when we can define logically
completed interface in free manner, we have no fixed requirements.

Is GeometricObject ::distance<memb er strictly necessary or not? Not easy to
answer looking from GeometricObject ::distance<side .

Scott Meyer advice to remove the member (as _not strictly necessary_) if
"non-friend non-member function" exist. I can not agree or disagree with the
his rule, because have no enough expirence and other reliable information
for interface without fixed requirements.

Interfaces can be predefined. Concrete members of interfaces of high level
classes strictly defined by application domain, we can not reduce the
interface for the sake of "more effective implementation" . The interfaces of
low level classes (as list, vector) often defined by reusage and other
purposes, so we can not reduce it in free manner also.

Interface is declaration, so it gives no code and we must not try to reduce
logicaly correct interface only in order to get its implementation more
effective. There are some stuffs, as design patterns for example, with the
help of which we can get optimal separated implementation for most existed
in nature interfaces.

For most, but it is not easy. From my point of view it is always hard to
make interfaces in plactical work, it is always iterational process, often
with blind branches of development. I do not know any universal and fast
method of design, but I will not remove members from logically completed
interface of class to namespace scope, as maybe he advice based on "existing
non-friend non-member function" criterion.

By the way, using the accident, I can say about weak point of C++ again - it
is hard to work with already defined classes in C++, due to C++ has no
standard bidirectional bridge between external OO desing tools (as Smalltalk
workplace) and C++ source code. We can not add tools, can not switch from
one to other as we need. I will write (want to write) about the bridge (the
bridge can be developed with the help of extra information about "class
items") on my page maybe during nearest months.
3.
In general any the "non-friend non-member function" is more independent than
any other function using protected data of the class, but more independent
always only due to class interface, not due to place of the function
declaration.

So it does not matter for the function where the kind of function has
declared: as member or as non-member and in my previous example i have shown
that member can be at least not worse.

He has written: "it becomes clear that a class with n member functions is
more encapsulated than a class with n+1 member functions". Maybe he want to
say, that small interface is better than large one.

A class with interface separated into small parts can seem easyer than the
same class with large interface, but last can seem more "solid", joining
members together for search and work with the class.

In my example there is extra link between member (implemeted as forwarding
function) and its real implementation placed outside of class. But in
program the link always will exist at the point of the non-member function
call. It is impossible to hide the link with the help of non-member.

We can use parameter of the concrete implementation as parameter of
templated member, so can use multiple implementations for one class (as with
non-member):

//
class GeometricObject
{
protected:
template<
class First,
class Second,
class Dist_implementa tion=
Ndecl::Dist_imp lementation<Fir st,Second>
>
type distance(const Second& obj)
{
Dist_implementa tion di;

return
di.distance(
static_cast<con st First&>(*this),
obj
);
}
};

but it is often better to link concrete implementation with the class
GeometricObject (as i have done befor), rather than with its member
"distance".
--
Maksim A. Polyanin
http://grizlyk1.narod.ru/cpp_new

"In thi world of fairy tales rolls are liked olso"
/Gnume/
Mar 6 '07 #10

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

Similar topics

242
13465
by: James Cameron | last post by:
Hi I'm developing a program and the client is worried about future reuse of the code. Say 5, 10, 15 years down the road. This will be a major factor in selecting the development language. Any comments on past experience, research articles, comments on the matter would be much appreciated. I suspect something like C would be the best based on comments I received from the VB news group. Thanks for the help in advance James Cameron
45
3629
by: Steven T. Hatton | last post by:
This is a purely *hypothetical* question. That means, it's /pretend/, CP. ;-) If you were forced at gunpoint to put all your code in classes, rather than in namespace scope (obviously classes themselves are an exception to this), and 'bootstrap' your program by instantiating a single application object in main(), would that place any limitations on what you could accomplish with your program? Are there any benefits to doing things that...
2
3793
by: Bryan Olson | last post by:
The current Python standard library provides two cryptographic hash functions: MD5 and SHA-1 . The authors of MD5 originally stated: It is conjectured that it is computationally infeasible to produce two messages having the same message digest. That conjecture is false, as demonstrated by Wang, Feng, Lai and Yu in 2004 . Just recently, Wang, Yu, and Lin showed a short- cut solution for finding collisions in SHA-1 . Their result
8
3042
by: Paul Cochrane | last post by:
Hi all, I've got an application that I'm writing that autogenerates python code which I then execute with exec(). I know that this is not the best way to run things, and I'm not 100% sure as to what I really should do. I've had a look through Programming Python and the Python Cookbook, which have given me ideas, but nothing has gelled yet, so I thought I'd put the question to the community. But first, let me be a little more detailed...
20
23115
by: Steve Jorgensen | last post by:
A while back, I started boning up on Software Engineering best practices and learning about Agile programming. In the process, I've become much more committed to removing duplication in code at a much finer level. As such, it's very frustrating to be working in VBA which lacks inheritance, one of the more powerful tools for eliminating duplication at the level I'm talking about. I've recently come up with a technique to emulate one...
171
7801
by: tshad | last post by:
I am just trying to decide whether to split my code and uses code behind. I did it with one of my pages and found it was quite a bit of trouble. I know that most people (and books and articles) like it because you can split the code from the design. That is logical. But if you are the only one working on the code, it seem a little overkill. I use Dreamweaver to do my design and find it a bit of a hassle to have multiple files open...
10
2296
by: Noozer | last post by:
I'm writing an ASP application and have a noob question... I have a class that access an MS SQL database. I have another class also accesses an MS SQL database and this second class uses objects from the first class. I have a third class using the DB and objects of the second class. Each of these classes contain all the code needed to access the database and this means much duplicated code. What I'd like to know is if there is a way...
0
1009
by: Happy Married Couple | last post by:
You're about to discover the 'magic ingredients' that make some couples live happy and blissful marriages for DECADES and how you can too. Dear Friend, My name is Michael Webb. I DON'T have a doctorate in counseling, marriage studies and I DON'T host a radio call in show… but I DO have what most other relationship "experts” don’t have…a blissful marriage. Which is why hundreds of men and women ask for my relationship advice and have...
8
2984
by: Brad Walton | last post by:
Hello. First post, but been doing a bit of reading here. I am working on a project in Java, but decided to switch over to C# after seeing some of the additional features I can get from C#. One of the big changes I want to make is event-driven code (rather than the linear flow I had in Java). I have spent a week or so searching Google, talking to a couple of programming friends, and just chewing on it in my brain. I think I have an ok handle...
0
9688
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9546
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,...
1
10247
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
9079
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...
1
7571
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6809
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
5593
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4146
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
3
2941
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.