D
Divick
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 doSomethingWithB( 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 doSomethingWithB could be
void doSomethingWithB( B * b)
or
void doSomethingWithB( 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
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 doSomethingWithB( 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 doSomethingWithB could be
void doSomethingWithB( B * b)
or
void doSomethingWithB( 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