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

design question - member function argument or pointer member

Hi all,

I have a question on interface design: I have a set of objects that are
interlinked in the real world: object of class A needs for example for the
operator() an object of class B. On what arguments do you decide whether to
pass a reference to the object of class B to the member function like this
operator()(classB& objB) or to have in class A a data member (a const
pointer to a class B object or so) and have this set during construction or
with a Set-function? Class B is designed to deliver the necessary data
through Get-functions. Or do friends come in handy here?

Thanks for tips,

gert
Jul 22 '05 #1
8 1837

"Gert Van den Eynde" <gv******@hotmail.com> wrote in message
news:bv**********@naxos.belnet.be...
Hi all,

I have a question on interface design: I have a set of objects that are
interlinked in the real world: object of class A needs for example for the
operator() an object of class B. On what arguments do you decide whether to
pass a reference to the object of class B to the member function like this
operator()(classB& objB) or to have in class A a data member (a const
pointer to a class B object or so) and have this set during construction or
with a Set-function? Class B is designed to deliver the necessary data
through Get-functions. Or do friends come in handy here?

Thanks for tips,


I think you need containment of B object inside A class.
It's a HAS-A kind of relationship.
If a class is implemented in terms of another class then containment/private
inheritance needs to be used
You say classes A and B are related. Then B object should also get constructed
when your A object gets constructed.
I personally do not like Set/Get functions. They kind of reflect that a class
has been used just for name-sake.

Best wishes,
Sharad

Jul 22 '05 #2

"Gert Van den Eynde" <gv******@hotmail.com> wrote in message
news:bv**********@naxos.belnet.be...
Hi all,

I have a question on interface design: I have a set of objects that are
interlinked in the real world: object of class A needs for example for the
operator() an object of class B. On what arguments do you decide whether to pass a reference to the object of class B to the member function like this
operator()(classB& objB) or to have in class A a data member (a const
pointer to a class B object or so) and have this set during construction or with a Set-function? Class B is designed to deliver the necessary data
through Get-functions. Or do friends come in handy here?

Thanks for tips,

gert


Hi,
These are good questions with no "right" answers, though many will try to
tell you what is "right". The question here is, "who is managing the
B-object"? If you want to hide what is happening to B, then it might be a
good idea to encapsulate the object inside of A (by composition, I mean).
If you have another set of code which is managing both an A and a B, then it
probably does not make much sense (to place a B object inside of A).

Overriding, that last suggestion, however, is that if the B-object is
needed in the implementation of A, then you probably do want it in your
A-class design (or use inheritance, possibly - not likely here).

Again, it's kind of silly...what I wrote. I can think of so many
scenarios where this rule of thumb would be... silly.
If you are fairly new to C++ or this type of design decision making
process, then I recommend that you try as many ways as you can think of.
Look at the tradeoffs of using each technique. Rules of thumb break easily
in C++ world, though many do exist. Try to get some experience with these
decisions and get a feel for what you like most.

Some (sometimes) competing ideas:
Friends
Inheritance (private, protected, public)
Composition (a B inside of an A) by value
Composition by reference
etc.

People will tell you inherit in a "IS-A" relationship and compose in a
"HAS-A" relationship. I don't stick hard and fast to this notion. Many
times, I strive hard to keep away from inheritance and virtual functions.
Not always - sometimes it is the best solution for me - it's just that
things better be really ripe for inheritance before I'll use it.

Anyway, look up H. Sutter's words of wisdom on inheritance and composition
(Exceptional C++ and More Exceptioinal C++). Lippman's book has some good
stuff too (C++ Primer). And, of course, B. Stroustrop will undoubtedly have
some great guidelines for you (The C++ Programming Language).

Shane
Jul 22 '05 #3
>
I think you need containment of B object inside A class.
It's a HAS-A kind of relationship.
If a class is implemented in terms of another class then
containment/private inheritance needs to be used
You say classes A and B are related. Then B object should also get
constructed when your A object gets constructed.
I personally do not like Set/Get functions. They kind of reflect that a
class has been used just for name-sake.


ok, but suppose a third class C pops that has a HAS-A relationship with B.
If I compose again, then I end up in the implementation of class A with
this like objB.objC.memberfunctionofclassC() and the like. What about
memory and data-duplication?

gert
Jul 22 '05 #4

"Gert Van den Eynde" <gv******@hotmail.com> wrote in message
news:bv**********@naxos.belnet.be...

I think you need containment of B object inside A class.
It's a HAS-A kind of relationship.
If a class is implemented in terms of another class then
containment/private inheritance needs to be used
You say classes A and B are related. Then B object should also get
constructed when your A object gets constructed.
I personally do not like Set/Get functions. They kind of reflect that a
class has been used just for name-sake.


ok, but suppose a third class C pops that has a HAS-A relationship with B.
If I compose again, then I end up in the implementation of class A with
this like objB.objC.memberfunctionofclassC() and the like. What about
memory and data-duplication?


Hold pointers/references to the contained class. This way you need to be careful
to
correctly initialize them when the enclosing object gets created. This way your
objects do not become
memory intensive. Also at time of destruction you need to be careful to free up
the used memory.

Each A object holds a unique reference to some B object and so on.
A object initializes B when it gets created.
I don't understand how data-duplication comes into picture.
If your contained object has no state and you mean that all contained objects
are similar then then it should not be declared as a class in the first place.

Best wishes,
Sharad
Jul 22 '05 #5
I don't understand how data-duplication comes into picture.
If your contained object has no state and you mean that all contained
objects are similar then then it should not be declared as a class in the
first place.


Some other classes D and E need the same object of class B do to their work.

gert

Jul 22 '05 #6

"Gert Van den Eynde" <gv******@hotmail.com> wrote in message
news:bv**********@naxos.belnet.be...
I don't understand how data-duplication comes into picture.
If your contained object has no state and you mean that all contained
objects are similar then then it should not be declared as a class in the
first place.


Some other classes D and E need the same object of class B do to their work.


If you think that a single instance of class B is used by all your other classes
then make it a singleton.
Jul 22 '05 #7
Gert Van den Eynde <gv******@hotmail.com> wrote:
I have a question on interface design: I have a set of objects that are
interlinked in the real world: object of class A needs for example for the
operator() an object of class B. On what arguments do you decide whether to
pass a reference to the object of class B to the member function like this
operator()(classB& objB) or to have in class A a data member (a const
pointer to a class B object or so) and have this set during construction or
with a Set-function? Class B is designed to deliver the necessary data
through Get-functions. Or do friends come in handy here?


Which is easer for the client (code that uses A)? That is what you
should do. If you find that clients are constantly calling "setB" before
calling A's member-function that uses B, then you would be better off
passing a B in as a parameter of the member-function. If clients are
constantly having to track which B goes with which A, then having A hold
a B member variable is the right way to go.
Jul 22 '05 #8
So far all of the postings have focused on inheritance, composition, etc.
However, in your original question you stated :
object of class A needs for example for the
operator() and object of class B


Allow me to paraphrase your statement :

A::somefunction() requires and object of class B

In that case why not follow your own suggestion of :

A::somefunction ( B const & b ) { ... }

This is what the argument list of a function are for, to supply the arguments a
function needs to carry out its work. This seems to solve all of the problems
you have outline. Namely :

1) A::somefunction is guaranteed to have an available B or it could not have
been called.

2) If other class functions require the same instance of B then simply pass
that instance to them as well :

C::somefunction ( B const & b ) { ... }
D::somefunction ( B const & b ) { ... }
etc ...

This method also has many other advantages :

1) class are not required to hold pointers or references to objects they may or
may not use. (After all, what guarantee do we have that the client will even
call somefunction.) This falls under the "only pay for what you use" paradigm
with C++ supports very well.

2) you can run A::somefunction multiple times with different B objects with a
single function call.

3) you can run A::somefunction, B::somefunction, ... , X::somefunction with
different B objects at your discretion.

In essence, this exactly what functions are designed to do: take arguments and
do work using them.

Jul 22 '05 #9

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

Similar topics

2
by: Alexander Stippler | last post by:
Can someone explain the following construct to me: What is not clear to me is what the argument to foo is. A pointer to a member? template <class U> static char foo(void (U::*)(void)); ...
4
by: Merlin | last post by:
Hi Imagine the following classes (A class diagram will help) BASE, A, B, C, D, E, F, G. A, B, C, D, G inherit from BASE. E, F inherit from D.
37
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined...
2
by: raxitsheth | last post by:
Hello All... I am using Posix Thread. class Parent { public: virtual void* func(void *)=0;
9
by: tropostropos | last post by:
On Solaris, using the Sun compiler, I get annoying warnings from the following code. The problem is that I am passing a C++ member function pointer to the C library function qsort. Is there a...
6
by: Caleb | last post by:
I have a class that has two member functions of the same name: class MyClass { public: void someFunction(int arg); void someFunction(int arg, char blah); }; I'm trying to get a...
13
by: JohnQ | last post by:
The implementation of classes with virtual functions is conceptually easy to understand: they use vtables. Which begs the question about POD structs: how are they associated with their member...
0
weaknessforcats
by: weaknessforcats | last post by:
Design Patterns: Visitor Introduction Polymorphism requires a class hierarchy where the interface to the hierarchy is in the base class. Virtual functions allow derived classes to override base...
5
by: Tim Frink | last post by:
Hi, I'm experimenting with function pointers and found two questions. Let's assume this code: 1 #include <iostream> 2 class A; 3 4 //////////////////////////////////////////// 5 class B
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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: 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: 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...

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.