Thread: friend declarations and scope

  1. #1
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446

    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?
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    Ireland
    Posts
    8,446
    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.
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    Ireland
    Posts
    8,446
    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.
    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
    Ireland
    Posts
    8,446
    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.
    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,057
    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
    Ireland
    Posts
    8,446
    I'm fully aware of that thread
    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