473,725 Members | 2,053 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

API design choice pointer vs. object

Hi,
I am designing an API and the problem that I have is more of a
design issue. In my API say I have a class A and B, as shown below

class A{
public:
void doSomethingWith B( B * b)
{
//do something with b
//possibly store in a list
listB.push_back (b);
}
private:
std::vector<B *> listB;
};

class B
{
///some declarations and /or data members
};

Now the problem is either the interface to doSomethingWith B could be

void doSomethingWith B( B * b)
or
void doSomethingWith B( B & b)

If I use pointers they come with all the mess as then I might have to
do some sort of reference counting on them to tell somebody that I am
storing the pointer to B and possibly call delete or unref on them when
I remove them from the list if the ref count goes to 0. This adds lots
of memory management overhead /code to the piece above.

If I use reference rather then pointers, then probably I am not giving
the user of the API the full flexibility. I am not very sure that what
pros /cons will it have if I don't provide a interface with pointers.

Frankly until now I almost used to use pointers everywhere for member
objects, for method parameters, method return type. And this I feel is
bad.

If people can provide reasons for/against the pointer/reference
interface then it would help a lot.

Thanks,
Divick

Mar 14 '06 #1
17 2590
Divick wrote:
[..]
If people can provide reasons for/against the pointer/reference
interface then it would help a lot.


Can you say, "Google"? "Pointer versus reference" discussion is such
a dead horse that I can barely see the tracks in the dust where the whip
hit it last time we were beating it.

V
--
Please remove capital As from my address when replying by mail
Mar 14 '06 #2

Divick wrote:
Hi,
I am designing an API and the problem that I have is more of a
design issue. In my API say I have a class A and B, as shown below

class A{
public:
void doSomethingWith B( B * b)
{
//do something with b
//possibly store in a list
listB.push_back (b);
}
private:
std::vector<B *> listB;
};

class B
{
///some declarations and /or data members
};

Now the problem is either the interface to doSomethingWith B could be

void doSomethingWith B( B * b)
or
void doSomethingWith B( B & b)


Is B polymorphic? If yes you have your answer, it must be the former.

Do you want to keep copies of B or the actual B itself? If copies then
it can be the later but otherwise must be former.

For reference counting and such if you have the option you should use
Boost's shared_ptr. If not then create one...it isn't too tough.

Mar 14 '06 #3
Noah Roberts <ro**********@g mail.com> wrote:

Divick wrote:
Now the problem is either the interface to doSomethingWith B could be

void doSomethingWith B( B * b)
or
void doSomethingWith B( B & b)


Is B polymorphic? If yes you have your answer, it must be the former.


I think polymorphism works with references too:
#include <iostream>

class A {
public:
virtual ~A() { }
virtual void do_something() { std::cout << "A::do_somethin g()\n"; }
};

class B : public A {
public:
virtual void do_something() { std::cout << "B::do_somethin g()\n"; }
};

void do_it(A& a)
{
a.do_something( );
}

int main()
{
B b;
do_it(b);

return 0;
}
Output:
B::do_something ()
--
Marcus Kwok
Mar 14 '06 #4
Noah Roberts wrote:
Divick wrote:
Hi,
I am designing an API and the problem that I have is more of a
design issue. In my API say I have a class A and B, as shown below

class A{
public:
void doSomethingWith B( B * b)
{
//do something with b
//possibly store in a list
listB.push_back (b);
}
private:
std::vector<B *> listB;
};

class B
{
///some declarations and /or data members
};

Now the problem is either the interface to doSomethingWith B could be

void doSomethingWith B( B * b)
or
void doSomethingWith B( B & b)


Is B polymorphic? If yes you have your answer, it must be the former.


That should not be a factor in making a determiniation.
If B is polymorphic, it will work with pointers or reference type.
Author's like Herb Sutter, recommend preferring to use reference type
over pointer type, because a pointer can mean two things, and therefore
more ambiguous.

I recommend a reference type, unless you know for sure you need a
pointer.

Mar 14 '06 #5
void doSomethingWith B( B * b)
or
void doSomethingWith B( B & b)

If I use pointers they come with all the mess as then I might have to
do some sort of reference counting on them to tell somebody that I am
storing the pointer to B and possibly call delete or unref on them when
I remove them from the list if the ref count goes to 0. This adds lots
of memory management overhead /code to the piece above.

If I use reference rather then pointers, then probably I am not giving
the user of the API the full flexibility. I am not very sure that what
pros /cons will it have if I don't provide a interface with pointers.


....or quite opposite. In fact, when designed right, you can gain much
more flexibility without pointers and passing ownership (actually, this
is about ownership rahter than anything else). See

http://upp.sourceforge .net/www$uppweb$over view$en-us.html

(I think the "Who owns widgets" section is the most relevant here).

Mirek
Mar 15 '06 #6
In article <11************ **********@i39g 2000cwa.googleg roups.com>,
"Divick" <di************ @gmail.com> wrote:
Hi,
I am designing an API and the problem that I have is more of a
design issue. In my API say I have a class A and B, as shown below

class A{
public:
void doSomethingWith B( B * b)
{
//do something with b
//possibly store in a list
listB.push_back (b);
}
private:
std::vector<B *> listB;
};

class B
{
///some declarations and /or data members
};

Now the problem is either the interface to doSomethingWith B could be

void doSomethingWith B( B * b)
or
void doSomethingWith B( B & b)

If people can provide reasons for/against the pointer/reference
interface then it would help a lot.


In this particular case, because of the possibility of A storing the
object beyond the return of the function, I would use a pointer.
--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Mar 15 '06 #7
>>..or quite opposite. In fact, when designed right, you can gain much
more flexibility without pointers and passing ownership (actually, this
is about ownership rahter than anything else). See http://upp.sourceforge .net/www$uppweb$over view$en-us.html (I think the "Who owns widgets" section is the most relevant here).

I did not really get the point that this page is referring to. Could
you please explain what this page tries to explain.

Thanks,
Divick

Mar 16 '06 #8
>>In this particular case, because of the possibility of A storing the
object beyond the return of the function, I would use a pointer.

What is the reason behind? Could you please elaborate this more.

Thanks,
Divick

Mar 16 '06 #9
Divick wrote:
(I think the "Who owns widgets" section is the most relevant
here).


I did not really get the point that this page is referring to. Could
you please explain what this page tries to explain.


It's probably referring to some lifetime management issues.

You should have a clear policy of how objects are allocated and how
objects are deleted.

If you are pushing "random" pointers into a list, who manages the
lifetime of the object to which it points? Is it the list? So that all
accesses are done via the list, and if it is removed from the list it is
destroyed? Is it some other entity? Will that other enitity remove it
from the list?

You can use a smart pointer to do the reference counting for you, in
which case I suggest boost::shared_p tr.

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Mar 16 '06 #10

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

Similar topics

3
1487
by: andy2O | last post by:
Hello comp.lang.py, Can you help me with ideas for the following (somewhat newbie) OO design question in Python? Note, I'm using psuedo-code, not actual Python for the examples! Background: ----------- I need to represent a small variety of mathematical constructs symbolically using Python classes.
2
4744
by: Jacek Dziedzic | last post by:
Hi! Say a have a class called black_box which, among other things, contains an array of cells like this: class black_box { private: // ... public: black_box_details_struct details;
0
1006
by: James Saker | last post by:
I've been working on a Python class object for APRS (Automatic Position Reporting System - a digital mode used by amateur radio operators) and have been going through David Mertz's Text Processing in Python book to help in the construction of the parser component of the class. I was curious if there was a good reference on the most pythonic design of parsing objects. For instance, should one parse raw or formatted data upon...
1
1724
by: rt | last post by:
hi, iam using vs 2003 ,one of my .aspx all my connections,datadapter,command objects in Design view are missing????? i closed and reopen also, but there is no redline in code editor. i cant belive it even i pasted my one of backup file .aspx, it still remain like this,
2
1799
by: Chris Murphy via DotNetMonster.com | last post by:
Hey guys, I've been hitting a brick wall with a problem I've come accross in developing an application. Background: The application uses one primary class that I'm trying to implement with the singleton pattern in mind. The design pattern seems to be working properly (complex and simple variations of it) Serialzation/Deserialzation seems to work okay. I run into a problem when I attempt to load the file back into the
4
1582
by: =?ISO-8859-15?Q?Luigi_Malag=F2?= | last post by:
Hello, i'm new to function pointers. I have some code that parses some configuration files using flex and bison. I have to use the same parse for different files, but the information i parse have to be add to different collections. I have a C++ class that wraps the code that invoce the parser. I pass to the parser an object that works as a container for the different object i have to parse. Let make an example. I have to parse men.txt...
3
3453
by: josh | last post by:
Hi I noticed that when I make a function with a base class parameter that is a reference than at run-time the compiler calls a base class function also if I have passed a derived object. But if I make that function with a pointer to a base class and then I pass the derived class at run-time the compiler calls a derived class function thanks to dynamic binding.
12
2176
by: bullockbefriending bard | last post by:
I am a complete ignoramus and newbie when it comes to designing and coding networked clients (or servers for that matter). I have a copy of Goerzen (Foundations of Python Network Programming) and once pointed in the best direction should be able to follow my nose and get things sorted... but I am not quite sure which is the best path to take and would be grateful for advice from networking gurus. I am writing a program to display horse...
0
8752
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,...
0
9401
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9257
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8096
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...
0
4517
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4782
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3221
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
2
2634
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2157
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.