Is violating access scope an undefined behavior?

Discussion in 'C++' started by Kira Yamato, Feb 27, 2008.

  1. Kira Yamato

    Kira Yamato Guest

    // Is the following code well-defined?

    class A
    {
    private:
    int foo(int n);

    public:
    typedef int (A::*ftype)(int);

    ftype get() const { return &A::foo; }
    };

    int main()
    {
    A a;
    A::ftype f = a.get();

    (a.*f)(5);

    return 0;
    }

    // Thank you.

    // --

    // kira
    Kira Yamato, Feb 27, 2008
    #1
    1. Advertising

  2. Kira Yamato

    Guest

    On Feb 27, 2:10 pm, Kira Yamato <> wrote:
    > // Is the following code well-defined?
    >
    > class A
    > {
    > private:
    >         int foo(int n);
    >
    > public:
    >         typedef int (A::*ftype)(int);
    >
    >         ftype get() const { return &A::foo; }
    >
    > };
    >
    > int main()
    > {
    >         A       a;
    >         A::ftype f = a.get();
    >
    >         (a.*f)(5);
    >
    >         return 0;
    >
    > }
    >
    > // Thank you.
    >
    > // --
    >
    > // kira


    compiling the code with xlC(AIX) and CC(solaris) compilers, the error
    is
    Undefined symbol: A::foo(int)

    Thanks,
    Balaji.
    , Feb 27, 2008
    #2
    1. Advertising

  3. On Feb 27, 10:10 am, Kira Yamato <> wrote:
    > // Is the following code well-defined?
    >
    > class A
    > {
    > private:
    >         int foo(int n);
    >
    > public:
    >         typedef int (A::*ftype)(int);
    >
    >         ftype get() const { return &A::foo; }
    >
    > };
    >
    > int main()
    > {
    >         A       a;
    >         A::ftype f = a.get();
    >
    >         (a.*f)(5);
    >
    >         return 0;
    >
    > }
    >
    > // Thank you.
    >
    > // --
    >
    > // kira


    It is not the behaviour what is undefined here but the function foo as
    the compiler indicates. g++ gives the following message:

    ... In function `A::get() const':
    : undefined reference to `A::foo(int)'

    As soon as you define your function foo, it will compile and run.

    In this code fragment, you just try to demonstrate how one can misuse
    an object-oriented language concept, don't you?

    Best Regards,
    Szabolcs
    Szabolcs Ferenczi, Feb 27, 2008
    #3
  4. Kira Yamato

    Guest

    On Feb 27, 8:22 am, Szabolcs Ferenczi <>
    wrote:
    > On Feb 27, 10:10 am, Kira Yamato <> wrote:
    >
    >
    >
    > > // Is the following code well-defined?

    >
    > > class A
    > > {
    > > private:
    > > int foo(int n);

    >
    > > public:
    > > typedef int (A::*ftype)(int);

    >
    > > ftype get() const { return &A::foo; }

    >
    > > };

    >
    > > int main()
    > > {
    > > A a;
    > > A::ftype f = a.get();

    >
    > > (a.*f)(5);

    >
    > > return 0;

    >
    > > }

    >
    > > // Thank you.

    >
    > > // --

    >
    > > // kira

    >
    > It is not the behaviour what is undefined here but the function foo as
    > the compiler indicates. g++ gives the following message:
    >
    > ... In function `A::get() const':
    > : undefined reference to `A::foo(int)'
    >
    > As soon as you define your function foo, it will compile and run.
    >

    function foo is already defined in the private section(and the post is
    about breaking it using function pointer).

    > In this code fragment, you just try to demonstrate how one can misuse
    > an object-oriented language concept, don't you?
    >
    > Best Regards,
    > Szabolcs



    Unfortunately, the same code compiles under g++ 3.4.4(my version of g+
    + on my toshiba tecra 9000 laptop with FreeBSD 6.1). I would say the
    code is correct syntatically but not semantically. As pointed out
    earlier, we are trying to break the languages rules or using the dark
    corners of the language. If interested, google for writing oo code
    using 'c'.

    Hope someone posts what the standard says. Anyhow, hats off to you.

    Thanks,
    Balaji.
    , Feb 27, 2008
    #4
  5. On Feb 27, 5:13 pm, wrote:

    > function foo is already defined in the private section(and the post is
    > about breaking it using function pointer).


    Well, it is defined but not implemented.

    > Unfortunately, the same code compiles under g++ 3.4.4(my version of g+
    > + on my toshiba tecra 9000 laptop with FreeBSD 6.1). I would say the
    > code is correct syntatically but not semantically. As pointed out


    What you get is actually an error from the linker. If you just compile
    it with the -c flag, it compiles. But the linker does not find the
    code body and hence the error.

    Best Regards,
    Szabolcs
    Szabolcs Ferenczi, Feb 27, 2008
    #5
  6. Kira Yamato

    peter koch Guest

    On 27 Feb., 10:10, Kira Yamato <> wrote:
    > // Is the following code well-defined?
    >
    > class A
    > {
    > private:
    >         int foo(int n);
    >
    > public:
    >         typedef int (A::*ftype)(int);
    >
    >         ftype get() const { return &A::foo; }
    >
    > };
    >
    > int main()
    > {
    >         A       a;
    >         A::ftype f = a.get();
    >
    >         (a.*f)(5);
    >
    >         return 0;
    >
    > }
    >

    Of course it is. Also, there's no violation of access scope as the
    title seems to imply.

    /Peter
    peter koch, Feb 27, 2008
    #6
  7. Kira Yamato

    Guest

    On Feb 27, 12:38 pm, peter koch <> wrote:
    > On 27 Feb., 10:10, Kira Yamato <> wrote:
    >
    > > // Is the following code well-defined?
    > > [snip]

    >
    > Of course it is. Also, there's no violation of access scope as the
    > title seems to imply.


    Yes; it is similar in spirit to doing something like this:

    class A {
    public:
    int & value (void) { return _value; }
    private:
    int _value;
    };

    Where, by returning a reference to a private member through a public
    function, you let a caller modify the "private" member. Having things
    be "private" or "protected" doesn't really put any code or data in a
    magical protected area of memory or something; you can still access
    them from anywhere. "Private" and "protected" exist to help a
    programmer enforce access rules at compile time, and also to add some
    convenient rules to how derived classes inherit access to member
    variables (in the case of "private" vs. "protected"). You can still
    access the data from anywhere though.

    Think of it like... you have an XBox 360 in your living room. It's
    declared as "private", so people can't just walk in and play it
    whenever they feel like it. However you can send out a public
    invitation to your friends, and give them full access to your private
    XBox. Not a problem. This is similar to providing public function
    pointers to private functions or public access to private members. Of
    course somebody could just break into your house and start playing
    your video games -- that's more equivalent to accessing private data
    by accessing the instance's block of memory directly instead of by
    named members.

    Jason
    , Feb 27, 2008
    #7
  8. Kira Yamato wrote:
    > ...
    > A::ftype f = a.get();
    > (a.*f)(5);
    > ...


    Access control in C++ is purely compile-time concept. What you are doing
    here does not "violate access scope" in any way and, therefore, does
    not trigger any kind of "undefined behavior".

    --
    Best regards,
    Andrey Tarasevich
    Andrey Tarasevich, Feb 27, 2008
    #8
  9. Kira Yamato

    Kira Yamato Guest

    On 2008-02-27 13:51:26 -0500, ""
    <> said:

    > On Feb 27, 12:38 pm, peter koch <> wrote:
    >> On 27 Feb., 10:10, Kira Yamato <> wrote:
    >>
    >>> // Is the following code well-defined?
    >>> [snip]

    >>
    >> Of course it is. Also, there's no violation of access scope as the
    >> title seems to imply.

    >
    > Yes; it is similar in spirit to doing something like this:
    >
    > class A {
    > public:
    > int & value (void) { return _value; }
    > private:
    > int _value;
    > };
    >
    > Where, by returning a reference to a private member through a public
    > function, you let a caller modify the "private" member. Having things
    > be "private" or "protected" doesn't really put any code or data in a
    > magical protected area of memory or something; you can still access
    > them from anywhere. "Private" and "protected" exist to help a
    > programmer enforce access rules at compile time, and also to add some
    > convenient rules to how derived classes inherit access to member
    > variables (in the case of "private" vs. "protected"). You can still
    > access the data from anywhere though.
    >
    > Think of it like... you have an XBox 360 in your living room. It's
    > declared as "private", so people can't just walk in and play it
    > whenever they feel like it. However you can send out a public
    > invitation to your friends, and give them full access to your private
    > XBox. Not a problem. This is similar to providing public function
    > pointers to private functions or public access to private members. Of
    > course somebody could just break into your house and start playing
    > your video games -- that's more equivalent to accessing private data
    > by accessing the instance's block of memory directly instead of by
    > named members.
    >
    > Jason


    Thanks everyone. I just wanted to make sure I'm not doing anything
    whose results could be compiler-dependent.

    --

    // kira
    Kira Yamato, Feb 27, 2008
    #9
  10. Kira Yamato

    James Kanze Guest

    On Feb 27, 10:21 pm, Andrey Tarasevich <>
    wrote:
    > Kira Yamato wrote:


    > > ...


    > > A::ftype f = a.get();
    > > (a.*f)(5);
    > > ...


    > Access control in C++ is purely compile-time concept. What you
    > are doing here does not "violate access scope" in any way and,
    > therefore, does not trigger any kind of "undefined behavior".


    Also, access control really only concerns names, and not objects
    or types. If you write something like:

    class C
    {
    class Nested { ... } ;
    public:
    typedef Nested N ;
    // ...
    } ;

    client code can use N, but not Nested, even though they both
    mean exactly the same thing. And any access to a member of
    Nested (e.g. through an object declared with type C::N) depends
    on the access controls active in Nested.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Feb 28, 2008
    #10
  11. In message
    <>,
    Szabolcs Ferenczi <> writes
    >On Feb 27, 5:13 pm, wrote:
    >
    >> function foo is already defined in the private section(and the post is
    >> about breaking it using function pointer).

    >
    >Well, it is defined but not implemented.


    No. In standard terminology it is declared but not defined.

    >> Unfortunately, the same code compiles under g++ 3.4.4(my version of g+
    >> + on my toshiba tecra 9000 laptop with FreeBSD 6.1). I would say the
    >> code is correct syntatically but not semantically. As pointed out

    >
    >What you get is actually an error from the linker. If you just compile
    >it with the -c flag, it compiles. But the linker does not find the
    >code body and hence the error.
    >
    >Best Regards,
    >Szabolcs


    --
    Richard Herring
    Richard Herring, Feb 28, 2008
    #11
    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. Replies:
    4
    Views:
    505
    Kevin Spencer
    Sep 6, 2005
  2. bazzer
    Replies:
    1
    Views:
    4,952
    =?Utf-8?B?Y2xpY2tvbg==?=
    Apr 6, 2006
  3. Mantorok Redgormor
    Replies:
    70
    Views:
    1,733
    Dan Pop
    Feb 17, 2004
  4. Frederick Gotham

    Violating sequence point?

    Frederick Gotham, Jul 5, 2006, in forum: C Programming
    Replies:
    15
    Views:
    459
    Ben Bacarisse
    Jul 6, 2006
  5. Frederick Gotham

    Violating Sequence Point?

    Frederick Gotham, Jul 5, 2006, in forum: C++
    Replies:
    5
    Views:
    405
    Andrew Koenig
    Jul 5, 2006
Loading...

Share This Page