[Qt-interest] tracking uniqueness
Andreas Pakulat
apaku at gmx.de
Sat Apr 24 22:29:26 CEST 2010
On 24.04.10 12:36:31, Oliver Heins wrote:
> Thiago Macieira <thiago at kde.org> writes:
>
> > Em Sábado 24. Abril 2010, às 10.10.29, Oliver Heins escreveu:
> >> Andreas Pakulat <apaku at gmx.de> writes:
> >> > You seem to have a bit of misconception about this. There is no Editor
> >> > instance in your code. All you do is declare a class and a nested class,
> >> > both can be used without an instance of the other as far as your example
> >> > goes. In particular, this:
> >> >
> >> > class Editor
> >> > {
> >> >
> >> > public:
> >> > class Token
> >> > {
> >> > };
> >> >
> >> > };
> >> >
> >> > Editor::Token t;
> >> >
> >> > is perfectly valid compiling code. And there's no instance of Editor
> >> > generated here, there's only a single instance of Token.
> >>
> >> So it's not possible in C++ to describe the idea: »Every instance of
> >> Token is held by an instance of Editor«?
> >
> > Of course it is.
> >
> > The counter is per editor, right? So it's a property of the editor.
>
> Yes, but I have to hand over the id exlicitely. I hoped to do something
> like this:
>
> class Editor
> {
> class Token
> {
> public:
> Token () : uniqId(++id_) { }
> private:
> quint32 uniqId;
> }
>
> quint32 id_;
> };
>
> or to make id_ somehow static only to the instance of Editor.
>
> But both doesn't work. Instead, I have to pass id_ explicitely in every
> constructor call:
How should they, there is no Editor instance in the above code. It looks
a bit like you're coming from a java background where an inline-declared
class can access member variables from the surrounding class. This is
not possible in C++. Any object A can only know about its own members and
any global variables. Access to another objects B members always has to
go through either a member variable of A for B or a global variable for
B(or a function returning B).
Andreas
--
Write yourself a threatening letter and pen a defiant reply.
More information about the Qt-interest-old
mailing list