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

Class methods operating on arrays of objects of the same type as itself.

Should a class be allowed to operate on an array of classes of the same type
as itself?

Say,

class X {
.....
void func( X* x);
}

void X::func(X* x) { ...}

....

X a = X();
X b[3] = { X(), X(), X() };

a.func(b);
Or is this a bad idea, and the function should better be moved to the class
that will need to call this method?

Thanks,
Bora
Jul 22 '05 #1
1 1195
Bora wrote:

Should a class be allowed to operate on an array of classes of the same type
as itself?

Say,

class X {
....
void func( X* x);
}

void X::func(X* x) { ...}

...

X a = X();
X b[3] = { X(), X(), X() };

a.func(b);

Or is this a bad idea,
That depends on the situation.

Eg.
X is a 'book'and func represents the operation search.

a.func(b)

would then be: search if book 'a' is rerpesented in the collection
of books b.

In this case I would opt for a seperate class 'library'. The library
manages all the books, thus the operation turns around:

library.search( a );

Here introducing a new class solved the problem nicely.

If on the other hand X eg. represents say a geometric line
and foo represents the operation 'intersect', the operation

a.func( b )

would read in plain english: dear line 'a', please intersect
yourself with all the lines I give to you and tell me if
there is an intersection.

IMHO nothing wrong with that.

In the first case I managed to come up with a meaningful
entitiy for the collection of X, the library. The library
has some functionality on its own, and can be viewed as
such a single entity on its own.
For the second example I can't come up with something like that.
Or, well I could come up with something equivalent: A line
usually is part of a drawing, thus a drawing consists of lines
So i could do:
line.intersect( drawing )
or
drawing.intersect( line )

on the other hand I just want to know the intersection of 2 lines
that may be overkill and the original would be better.

You see. It all depends on the current situation.
and the function should better be moved to the class
that will need to call this method?


That could also be a choice. As said it depends on the situation.
In programming there is seldom a 'one size fits all' situation.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #2

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

Similar topics

2
by: Fernando Rodriguez | last post by:
Hi, I need to traverse the methods defined in a class and its superclasses. This is the code I'm using: # An instance of class B should be able to check all the methods defined in B #and A,...
5
by: Irmen de Jong | last post by:
Hi, I've developed the Metaclass below, because I needed a way to make a bunch of classes thread-safe. I didn't want to change every method of the class by adding lock.aqcuire()..lock.release()...
7
by: Bob Rock | last post by:
Hello, this may seem a strange question, but is there a way of being able to call methods of a class through an array of that class when not referencing a specific object in the array. In other...
42
by: Dan | last post by:
Hello, I have trouble with class calling. I am calling getvolume() with succes in the function CreateCircle but it do not want to call it in ShowCircle() function. I am staying in the same...
6
by: gustav04 | last post by:
hi all i have a question: what is the difference between a c-function and an c++ class method (both do exactly the same thing). lets say, i have a function called print2std() and a class...
21
by: Jon Slaughter | last post by:
I have a class that is basicaly duplicated throughout several files with only members names changing according to the class name yet with virtually the exact same coding going on. e.g. class...
9
by: Brian Henry | last post by:
If i inherite a queue class into my class, and do an override of the enqueue member, how would i then go about actually doing an enqueue of an item? I am a little confused on this one... does over...
3
by: Brett | last post by:
I have several classes that create arrays of data and have certain properties. Call them A thru D classes, which means there are four. I can call certain methods in each class and get back an...
14
by: Dennis | last post by:
If I have a structure like; Public Structure myStructureDef Public b() as Byte Public t as String End Structure If I pass this structure, will the values in the array b be stored on the...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
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
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.