www.digitalmars.com Home | Search | C & C++ | D | DMDScript | News Groups | index | prev | next
Archives

D Programming
D
D.gnu
digitalmars.D
digitalmars.D.bugs
digitalmars.D.dtl
digitalmars.D.dwt
digitalmars.D.announce
digitalmars.D.learn
digitalmars.D.debugger

C/C++ Programming
c++
c++.announce
c++.atl
c++.beta
c++.chat
c++.command-line
c++.dos
c++.dos.16-bits
c++.dos.32-bits
c++.idde
c++.mfc
c++.rtl
c++.stl
c++.stl.hp
c++.stl.port
c++.stl.sgi
c++.stlsoft
c++.windows
c++.windows.16-bits
c++.windows.32-bits
c++.wxwindows

digitalmars.empire
digitalmars.DMDScript

c++ - Declaring a struct pointer inside a member function.

↑ ↓ ← Edward A. Waugh <edward_waugh hotmail.com> writes:
In the following code the declaration of bar *x is fine in
x() because its a member of class foo but its not OK in y()
because its a friend function:

class foo {

    public:

    void x() {
        bar *x;  // This declaration is OK.
    }

    friend void y() {
        bar *x;   // Type bar is unavailable here.
    }

    private:

    struct bar {
	int value;
        bar *next;
    };

};

I don't understand this because I thought that the
declaration of a function to be a friend of the class was
to allow it to have access to all the private values and
types in the class.  That is to say treat the function as
if it was a member of the class.

But this is not happening and I am getting frustrated with
C++ being so fussy.  It seems to me perfectly reasonable
that y() should be able to declare pointers of type bar.
The only I can get this to work is to take the declaration
of bar outside of the declaration of foo i.e. both are
first level declarations.  I don't want that because bar is
only supposed to be used by foo.

Thanks,
Edward
Oct 03 2006
↑ ↓ → Heinz Saathoff <newshsaat arcor.de> writes:
Hi,

Edward A. Waugh wrote ...
 In the following code the declaration of bar *x is fine in
 x() because its a member of class foo but its not OK in y()
 because its a friend function:
 
 class foo {
 
     public:
 
     void x() {
         bar *x;  // This declaration is OK.
     }
 
     friend void y() {
         bar *x;   // Type bar is unavailable here.
     }
 
     private:
 
     struct bar {
 	int value;
         bar *next;
     };
 
 };
 
 I don't understand this because I thought that the
 declaration of a function to be a friend of the class was
 to allow it to have access to all the private values and
 types in the class.  That is to say treat the function as
 if it was a member of the class.

Friends are NOT members! Therefore y() has no this-pointer and the scope to search for variables is outside the class scope. It might be clearer if you don't define the functions inline: class foo { public: void x(); friend void y(); private: struct bar { int value; bar *next; }; }; // Definition outside void foo::x() { // ^^^^^ // foo:: tells the compiler that x is a class member bar *x; // This declaration is OK. } void y() { // ^^^^ // ordinary global function! bar *x; // Type bar is unavailable here. foo::bar *z; // this is ok because y() as a friend // is allowed to use foo::bar } - Heinz
Oct 05 2006