Friend class and NULL object member functions

Discussion in 'C++' started by KriS, Apr 18, 2006.

  1. KriS

    KriS Guest

    Hi,

    I have two simple questions:

    1) Do I have to write storage-class-specifier, when I want to declare a
    class as friend:

    class Bar {};
    class Foo { friend ??? Bar; };

    VC++8 doesn't complain about it, g++ 3.4.4 throws an error and I can't
    find anything about it in c++'98 standart.


    2)
    #include <iostream>

    class Foo {
    public: void print() { std::cout << "smth" << std::endl; }
    };

    int main() {
    Foo* f = NULL;
    f->print();
    }

    Works fine under g++ 3.4.4 and vc++8. As I understand this is undefined
    behavior?

    --
    KriS
     
    KriS, Apr 18, 2006
    #1
    1. Advertising

  2. KriS

    Noah Roberts Guest

    KriS wrote:
    > Hi,
    >
    > I have two simple questions:
    >
    > 1) Do I have to write storage-class-specifier, when I want to declare a
    > class as friend:
    >
    > class Bar {};
    > class Foo { friend ??? Bar; };
    >
    > VC++8 doesn't complain about it, g++ 3.4.4 throws an error and I can't
    > find anything about it in c++'98 standart.


    11.4.2 of 2003 states that a "elaborated-type-specifier" is required.
    >
    >
    > 2)
    > #include <iostream>
    >
    > class Foo {
    > public: void print() { std::cout << "smth" << std::endl; }
    > };
    >
    > int main() {
    > Foo* f = NULL;
    > f->print();
    > }
    >
    > Works fine under g++ 3.4.4 and vc++8. As I understand this is undefined
    > behavior?


    That is correct.
     
    Noah Roberts, Apr 18, 2006
    #2
    1. Advertising

  3. KriS

    andrew Guest

    Noah Roberts wrote:
    > KriS wrote:
    >> Hi,
    >>
    >> I have two simple questions:
    >>

    snipped
    >> 2)
    >> #include <iostream>
    >>
    >> class Foo {
    >> public: void print() { std::cout << "smth" << std::endl; }
    >> };
    >>
    >> int main() {
    >> Foo* f = NULL;
    >> f->print();
    >> }
    >>
    >> Works fine under g++ 3.4.4 and vc++8. As I understand this is undefined
    >> behavior?

    >
    > That is correct.
    >

    More specifically, it only "works fine" because Foo::print() does not
    reference any member variables. If it did, it would find that the
    implicit this Foo* parameter would be NULL and cause a runtime memory
    access error. Its probably better to make methods that do not use member
    variables static to remove temptation.

    Andrew
     
    andrew, Apr 20, 2006
    #3
  4. KriS

    Fei Liu Guest

    KriS wrote:
    > Hi,
    >
    > I have two simple questions:
    >
    > 1) Do I have to write storage-class-specifier, when I want to declare a
    > class as friend:
    >
    > class Bar {};
    > class Foo { friend ??? Bar; };
    >
    > VC++8 doesn't complain about it, g++ 3.4.4 throws an error and I can't
    > find anything about it in c++'98 standart.
    >


    you have to tell the compiler either it's a class or a method. In this
    case, you clearly wanted to grant class Bar friend access. So use
    'friend class Bar;'
     
    Fei Liu, Apr 20, 2006
    #4
  5. KriS

    Noah Roberts Guest

    andrew wrote:
    > Noah Roberts wrote:
    > > KriS wrote:
    > >> Hi,
    > >>
    > >> I have two simple questions:
    > >>

    > snipped
    > >> 2)
    > >> #include <iostream>
    > >>
    > >> class Foo {
    > >> public: void print() { std::cout << "smth" << std::endl; }
    > >> };
    > >>
    > >> int main() {
    > >> Foo* f = NULL;
    > >> f->print();
    > >> }
    > >>
    > >> Works fine under g++ 3.4.4 and vc++8. As I understand this is undefined
    > >> behavior?

    > >
    > > That is correct.
    > >

    > More specifically, it only "works fine" because Foo::print() does not
    > reference any member variables.


    "works fine" is one possible outcome of undefined behavior. Because
    not referencing a member variable doesn't blow up here and now doesn't
    mean it won't later or elsewhere. Undefined is undefined.
     
    Noah Roberts, Apr 20, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gergely Korodi

    Question about friend member functions

    Gergely Korodi, Jul 28, 2004, in forum: C++
    Replies:
    2
    Views:
    347
    Michael D. Borghardt
    Jul 30, 2004
  2. Hicham Mouline
    Replies:
    0
    Views:
    343
    Hicham Mouline
    Nov 11, 2008
  3. Hicham Mouline
    Replies:
    0
    Views:
    441
    Hicham Mouline
    Apr 23, 2009
  4. Hicham Mouline
    Replies:
    1
    Views:
    423
    Michael DOUBEZ
    Apr 24, 2009
  5. Peter
    Replies:
    2
    Views:
    286
    Öö Tiib
    Jun 6, 2013
Loading...

Share This Page