friend declarations and scope

This is a discussion on friend declarations and scope within the C++ Programming forums, part of the General Programming Boards category; Code: class myClass; class otherClass { public: otherClass(int x): valor(x) {} inline int sum(myClass &obj); private: int valor; }; class ...

  1. #1
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,581

    friend declarations and scope

    Code:
    class myClass;
    
    class otherClass {
    public:
        otherClass(int x): valor(x) {}
    
        inline int sum(myClass &obj);
    private:
        int valor;
    };
    
    class myClass {
    public:
        myClass(int x): val(x) {}
    
        friend otherClass::sum(myClass&);
    private:
        int val;
    };
    
    inline int otherClass::sum(myClass &obj) {
        return obj.val + valor;
    }
    The code serves only to ilustrate my question.

    I believe C++ Primer (Lippman and others) fails miserably when explaining friend function declarations. A small glitch, I'm sure, on an otherwise awsome book. Or maybe I'm not following this rather small section correctly. But while it explains to:

    - Define the function only after the class granting friendship is defined.
    - Define the friend class before the class granting friendship is defined.

    However, it didn't mention that obviously the function declaration in otherClass presents myClass. And that name can't possibly be in scope yet because I'm forced to only declare it later.

    Previous chapters taught me well enough and so it wasn't hard for me to understand all I needed to do was to declare myClass (not define it) and thus introduce it to the scope. The code now compiles and executes correctly when tested.

    My question is though (since i'm not used to C++ Primer containing flaws), is this the correct way? Or am I missing something here?
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,271
    I believe the use of a forward declaration here makes sense, though there's a syntax error in the friend statement since otherClass::sum's return type is omitted.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,581
    You are quite right laserlight. I didn't realize the error before you mentioned it. Quite curious the fact VC++ 2005 doesn't even issue a warning.

    minGW, on the other hand, was quick to catch it.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,271
    Quite curious the fact VC++ 2005 doesn't even issue a warning.
    Could be backwards compatibility (combatibility?) with earlier versions of the compiler, such that an omitted return type defaults to int.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,581
    Could be. Got the irony

    However, VC++ 2005 does not accept default int anymore. At least it does not on any other circumstance so far (function and object declarations, both). Well... truth the matter is that it throws a Compiler Warning C4430 that is treated as an error (combatibility, I know). However, I do have the compiler configured for level 4 warnings. And on this particular case that warning is not being thrown.

    I'm getting inclined to believe this to be a bug.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  6. #6
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,581
    For anyone interested, this seems to be a bug. I presented the problem on the MSDN Forums and one of the MVPs apparently "acknowledged" something is askew.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,581
    I'm fully aware of that thread
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Scope And Parameter Passing
    By djwicks in forum C Programming
    Replies: 6
    Last Post: 03-28-2005, 08:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21