Hi,
diesen Artikel habe ich auch in einem News-Brett gepostet, daher
ist er auf Englisch, fuer Hilfe waere ich dankbar!
I have a class with a private copy-constructor (it has a copy
constructor which needs one more arg though). There is no way to
put that into a vector, is there? So I tried to use references to
that class. But vector can neither deal with references, right?
It’s a bit complicated. The class I talk about is nested in
another class. The nested class needs a pointer to the outside,
giving me a Java like call back construct. Now I want to have a
vector of that nested type, and to keep the depending code simple
I’d like to use a vector of objects. Second option would be
vector of references. I’d hate to have to use pointers since the
whole stuff is part of the basic hirarchy for dozens of classes
that depend on it …
Any ideas? Or are vectors actually able to deal with the problem?
I could also provide a copyconstructor. But that would be unsafe.
It should then only be used by vector not by users of the
surrounding class. Again I don’t want to make the nested class
private or protected since it being public provides a nicer
interface for initializing the surrounding class.
I also thought about making vector a friend of my nested class.
That would be fine as far as I am concerned (since the actual
vector in the outside class is private), but that’s not enough.
Using my compiler (gcc 2.95.2) I have to make some template
function also a friend. Example code:
#include
class A
{
private:
A(const A&:wink: {}
public:
A() {}
friend vector; // I could live with this
friend void construct (A \*, const A &:wink:;
// that construct thing might make the code unportable
};
void main()
{
vector va;
// vector vra; // not possible
va.push\_back (A () );
}
Trouble is that construct stuff. Is that part of the STL-Standard
or of the implementation of my compiler. Thus will it be
portable? Oh damnit I just found out, that I have to make two
more friends, to make clearing the vector and other stuff work.
The friends for actual code (I am too lazy, to work it out with
the example …) would be:
friend class Plug::LinkTable::SameIdLinks \*
\_\_copy(Plug::LinkTable::SameIdLinks \*,
Plug::LinkTable::SameIdLinks \*,
Plug::LinkTable::SameIdLinks \*,
random\_access\_iterator\_tag, ptrdiff\_t \*);
friend class Plug::LinkTable::SameIdLinks \*
\_\_copy\_backward(Plug::LinkTable::SameIdLinks \*,
Plug::LinkTable::SameIdLinks \*,
Plug::LinkTable::SameIdLinks \*,
random\_access\_iterator\_tag, ptrdiff\_t \*);
Lol, that can’t be portable. So, any ideas?
Cheers
Thorsten