Is it legal code?

Discussion in 'C++' started by Paul, Feb 19, 2011.

  1. Paul

    Paul Guest

    Is it legal to invoke a (non static) member function without an object?

    I read something in the C++ standard that it may not be standard compliant
    code to invoke a (non static)member function directly from a pointer.
    Paul, Feb 19, 2011
    #1
    1. Advertising

  2. Paul

    itaj sherman Guest

    On Feb 19, 1:27 pm, "Paul" <> wrote:
    > Is it legal to invoke a (non static) member function without an object?


    I think a better question would be:
    Is it legal to invoke an N-ary function, with less than N arguments?
    * A defaut value is considered an argument.
    * That would include the argument referred to by 'this' in non-static
    member functions. A non-staic member funcion has arity 1 or bigger.

    I think 5.2.2, 9.3, 9.3.1 might be relevant.

    >
    > I read something in the C++ standard that it may not be standard compliant
    > code to invoke a (non static)member function directly from a pointer.


    Which paragraph in the standard are you referring to?

    itaj
    itaj sherman, Feb 19, 2011
    #2
    1. Advertising

  3. Paul

    Paul Guest

    I found two quotes from the standard:

    "5.2.2
    For a member function call, the postfix expression shall be an implicit
    (9.3.1, 9.4) or explicit class member access (5.2.5) whose idexpression is a
    function member name, or a pointertomember expression (5.5) selecting a
    function member. The first expression in the postfix expression is then
    called the object expression, and the call
    is as a member of the object pointed to or referred to. In the case of an
    implicit class member access, the
    implied object is the one pointed to by this. [Note: a member function call
    of the form f() is interpreted
    as (*this).f() (see 9.3.1). ]"


    "9.3.1 Nonstatic member functions [class.mfct.nonstatic]
    1 A nonstatic member function may be called for an object of its class type,
    or for an object of a class derived
    (10) from its class type, using the class member access syntax (5.2.5,
    13.3.1.1). A nonstatic member function
    may also be called directly using the function call syntax (5.2.2, 13.3.1.1)
    - from within the body of a member function of its class or of a class
    derived from its class, or
    - from a meminitializer
    (12.6.2) for a constructor for its class or for a class derived from its
    class.
    If a nonstatic member function of a class X is called for an object that is
    not of type X, or of a type derived
    from X, the behavior is undefined."



    Please note the last sentence. It seems clear to me that any nonstatic
    member function can *only* be called on an object.
    Paul, Feb 19, 2011
    #3
  4. Paul

    Paul Guest

    "itaj sherman" <> wrote in message
    news:...
    On Feb 19, 1:27 pm, "Paul" <> wrote:
    > Is it legal to invoke a (non static) member function without an object?


    I think a better question would be:
    Is it legal to invoke an N-ary function, with less than N arguments?
    * A defaut value is considered an argument.
    * That would include the argument referred to by 'this' in non-static
    member functions. A non-staic member funcion has arity 1 or bigger.

    I think 5.2.2, 9.3, 9.3.1 might be relevant.

    >
    > I read something in the C++ standard that it may not be standard compliant
    > code to invoke a (non static)member function directly from a pointer.


    Which paragraph in the standard are you referring to?

    ..........................................................................

    sorry indenting probs ,
    see other post.
    Paul, Feb 19, 2011
    #4
  5. Paul

    Paul N Guest

    On Feb 19, 12:26 pm, "Paul" <> wrote:
    > I found two quotes from the standard:
    >
    > "5.2.2
    > For a member function call, the postfix expression shall be an implicit
    > (9.3.1, 9.4) or explicit class member access (5.2.5) whose idexpression is a
    > function member name, or a pointertomember expression (5.5) selecting a
    > function member. The first expression in the postfix expression is then
    > called the object expression, and the call
    > is as a member of the object pointed to or referred to. In the case of an
    > implicit class member access, the
    > implied object is the one pointed to by this. [Note: a member function call
    > of the form f() is interpreted
    > as (*this).f() (see 9.3.1). ]"
    >
    > "9.3.1 Nonstatic member functions [class.mfct.nonstatic]
    > 1 A nonstatic member function may be called for an object of its class type,
    > or for an object of a class derived
    > (10) from its class type, using the class member access syntax (5.2.5,
    > 13.3.1.1). A nonstatic member function
    > may also be called directly using the function call syntax (5.2.2, 13.3.1..1)
    > - from within the body of a member function of its class or of a class
    > derived from its class, or
    > - from a meminitializer
    > (12.6.2) for a constructor for its class or for a class derived from its
    > class.
    > If a nonstatic member function of a class X is called for an object that is
    > not of type X, or of a type derived
    > from X, the behavior is undefined."
    >
    > Please note the last sentence. It seems clear to me that any nonstatic
    > member function can *only* be called on an object.


    Yes, your second quote seems to answer your question exactly. The
    exceptions in the quoted passage allow, for instance, one member
    function to call another - in this case, if the first member function
    is being called on an object of the proper type then obviously the
    second one is too.

    I think people have written code in the past where they cast a NULL
    pointer to an object of the desired type and use that to call a member
    function, but this is undefined behaviour. (Though it presumably works
    for them.)

    The other Paul.
    Paul N, Feb 19, 2011
    #5
  6. Paul

    itaj sherman Guest

    On Feb 19, 2:26 pm, "Paul" <> wrote:
    > I found two quotes from the standard:
    >
    > "5.2.2
    > For a member function call, the postfix expression shall be an implicit
    > (9.3.1, 9.4) or explicit class member access (5.2.5) whose idexpression is a
    > function member name, or a pointertomember expression (5.5) selecting a
    > function member. The first expression in the postfix expression is then
    > called the object expression, and the call
    > is as a member of the object pointed to or referred to. In the case of an
    > implicit class member access, the
    > implied object is the one pointed to by this. [Note: a member function call
    > of the form f() is interpreted
    > as (*this).f() (see 9.3.1). ]"
    >
    > "9.3.1 Nonstatic member functions [class.mfct.nonstatic]
    > 1 A nonstatic member function may be called for an object of its class type,
    > or for an object of a class derived
    > (10) from its class type, using the class member access syntax (5.2.5,
    > 13.3.1.1). A nonstatic member function
    > may also be called directly using the function call syntax (5.2.2, 13.3.1..1)
    > - from within the body of a member function of its class or of a class
    > derived from its class, or
    > - from a meminitializer
    > (12.6.2) for a constructor for its class or for a class derived from its
    > class.
    > If a nonstatic member function of a class X is called for an object that is
    > not of type X, or of a type derived
    > from X, the behavior is undefined."
    >
    > Please note the last sentence. It seems clear to me that any nonstatic
    > member function can *only* be called on an object.


    Yeah, and the function:

    void foo( X&, Y& );

    can only be called on two objects (specifically instances of X and Y).
    What's so special about the member case?

    itaj
    itaj sherman, Feb 19, 2011
    #6
  7. Paul

    Paul Guest

    "itaj sherman" <> wrote in message
    news:...
    On Feb 19, 2:26 pm, "Paul" <> wrote:
    > I found two quotes from the standard:
    >
    > "5.2.2
    > For a member function call, the postfix expression shall be an implicit
    > (9.3.1, 9.4) or explicit class member access (5.2.5) whose idexpression is
    > a
    > function member name, or a pointertomember expression (5.5) selecting a
    > function member. The first expression in the postfix expression is then
    > called the object expression, and the call
    > is as a member of the object pointed to or referred to. In the case of an
    > implicit class member access, the
    > implied object is the one pointed to by this. [Note: a member function
    > call
    > of the form f() is interpreted
    > as (*this).f() (see 9.3.1). ]"
    >
    > "9.3.1 Nonstatic member functions [class.mfct.nonstatic]
    > 1 A nonstatic member function may be called for an object of its class
    > type,
    > or for an object of a class derived
    > (10) from its class type, using the class member access syntax (5.2.5,
    > 13.3.1.1). A nonstatic member function
    > may also be called directly using the function call syntax (5.2.2,
    > 13.3.1.1)
    > - from within the body of a member function of its class or of a class
    > derived from its class, or
    > - from a meminitializer
    > (12.6.2) for a constructor for its class or for a class derived from its
    > class.
    > If a nonstatic member function of a class X is called for an object that
    > is
    > not of type X, or of a type derived
    > from X, the behavior is undefined."
    >
    > Please note the last sentence. It seems clear to me that any nonstatic
    > member function can *only* be called on an object.


    --Yeah, and the function:

    --void foo( X&, Y& );

    --can only be called on two objects (specifically instances of X and Y).
    --What's so special about the member case?

    My newsreader not indenting your test , so I've manually marked your text
    with --.

    You seem confused about the meaning of "calling a member function on an
    object".
    You give an example of a non member fucntion and then ask .what is so
    special about member functions. I don't know where you are coming from , do
    you know the difference between a member function and non member function?
    Paul, Feb 19, 2011
    #7
  8. Paul

    Paul Guest

    "Paul N" <> wrote in message
    news:...
    On Feb 19, 12:26 pm, "Paul" <> wrote:
    > I found two quotes from the standard:
    >
    > "5.2.2
    > For a member function call, the postfix expression shall be an implicit
    > (9.3.1, 9.4) or explicit class member access (5.2.5) whose idexpression is
    > a
    > function member name, or a pointertomember expression (5.5) selecting a
    > function member. The first expression in the postfix expression is then
    > called the object expression, and the call
    > is as a member of the object pointed to or referred to. In the case of an
    > implicit class member access, the
    > implied object is the one pointed to by this. [Note: a member function
    > call
    > of the form f() is interpreted
    > as (*this).f() (see 9.3.1). ]"
    >
    > "9.3.1 Nonstatic member functions [class.mfct.nonstatic]
    > 1 A nonstatic member function may be called for an object of its class
    > type,
    > or for an object of a class derived
    > (10) from its class type, using the class member access syntax (5.2.5,
    > 13.3.1.1). A nonstatic member function
    > may also be called directly using the function call syntax (5.2.2,
    > 13.3.1.1)
    > - from within the body of a member function of its class or of a class
    > derived from its class, or
    > - from a meminitializer
    > (12.6.2) for a constructor for its class or for a class derived from its
    > class.
    > If a nonstatic member function of a class X is called for an object that
    > is
    > not of type X, or of a type derived
    > from X, the behavior is undefined."
    >
    > Please note the last sentence. It seems clear to me that any nonstatic
    > member function can *only* be called on an object.


    --Yes, your second quote seems to answer your question exactly. The
    --exceptions in the quoted passage allow, for instance, one member
    --function to call another - in this case, if the first member function
    --is being called on an object of the proper type then obviously the
    --second one is too.

    Yes I agree.

    --I think people have written code in the past where they cast a NULL
    --pointer to an object of the desired type and use that to call a member
    --function, but this is undefined behaviour. (Though it presumably works
    --for them.)

    Yup I understand this, what I was trying to establish is if it's standard
    complaint code. As this quote from standards suggests it it's undefined
    behaviour it means its non standard compliant code.

    Ty for your post.
    Paul, Feb 19, 2011
    #8
  9. Paul

    itaj sherman Guest

    On Feb 19, 3:06 pm, "Paul" <> wrote:
    > "itaj sherman" <> wrote in message
    >
    > news:...
    > On Feb 19, 2:26 pm, "Paul" <> wrote:
    >
    >
    >
    >
    >
    > > I found two quotes from the standard:

    >
    > > "5.2.2
    > > For a member function call, the postfix expression shall be an implicit
    > > (9.3.1, 9.4) or explicit class member access (5.2.5) whose idexpression is
    > > a
    > > function member name, or a pointertomember expression (5.5) selecting a
    > > function member. The first expression in the postfix expression is then
    > > called the object expression, and the call
    > > is as a member of the object pointed to or referred to. In the case of an
    > > implicit class member access, the
    > > implied object is the one pointed to by this. [Note: a member function
    > > call
    > > of the form f() is interpreted
    > > as (*this).f() (see 9.3.1). ]"

    >
    > > "9.3.1 Nonstatic member functions [class.mfct.nonstatic]
    > > 1 A nonstatic member function may be called for an object of its class
    > > type,
    > > or for an object of a class derived
    > > (10) from its class type, using the class member access syntax (5.2.5,
    > > 13.3.1.1). A nonstatic member function
    > > may also be called directly using the function call syntax (5.2.2,
    > > 13.3.1.1)
    > > - from within the body of a member function of its class or of a class
    > > derived from its class, or
    > > - from a meminitializer
    > > (12.6.2) for a constructor for its class or for a class derived from its
    > > class.
    > > If a nonstatic member function of a class X is called for an object that
    > > is
    > > not of type X, or of a type derived
    > > from X, the behavior is undefined."

    >
    > > Please note the last sentence. It seems clear to me that any nonstatic
    > > member function can *only* be called on an object.

    >
    > --Yeah, and the function:
    >
    > --void foo( X&, Y& );
    >
    > --can only be called on two objects (specifically instances of X and Y).
    > --What's so special about the member case?
    >
    > My newsreader not indenting your test , so I've manually marked your text
    > with  --.
    >
    > You seem confused about the meaning of "calling a member function on an
    > object".
    > You give an example of a non member fucntion and then ask .what is so
    > special about member functions. I don't know where you are coming from , do
    > you know the difference between a member function and non member function?- Hide quoted text -
    >


    No.
    I don't think you read carefully everything I said.

    itaj
    itaj sherman, Feb 19, 2011
    #9
  10. Paul wrote:

    > Is it legal to invoke a (non static) member function without an object?
    >
    > I read something in the C++ standard that it may not be standard compliant
    > code to invoke a (non static)member function directly from a pointer.


    It is undefined behavior if you do so.

    struct A { void f() { } };
    int main() { ((A*)0)->f(); }

    Undefined. No object.
    Johannes Schaub (litb), Feb 19, 2011
    #10
  11. Paul

    itaj sherman Guest

    On Feb 19, 5:19 pm, "Johannes Schaub (litb)"
    <> wrote:
    > Paul wrote:
    > > Is it legal to invoke a (non static) member function without an object?

    >
    > > I read something in the C++ standard that it may not be standard compliant
    > > code to invoke a (non static)member function directly from a pointer.

    >
    > It is undefined behavior if you do so.
    >
    >    struct A { void f() { } };
    >    int main() { ((A*)0)->f(); }
    >
    > Undefined. No object.


    struct A {};
    void foo( A& );
    int main() { foo( *((A*)0) ); }

    This is undefined behaviour just as well.
    There's nothing in this issue that has specifically to do with
    nonstatic member functions.

    itaj
    itaj sherman, Feb 19, 2011
    #11
  12. Paul

    gwowen Guest

    On Feb 19, 11:27 am, "Paul" <> wrote:
    > Is it legal to invoke a (non static) member function without an object?
    >
    > I read something in the C++ standard that it may not be standard compliant
    > code to invoke a (non static)member function directly from a pointer.


    If the pointer points to an object that still exists, its fine.
    Otherwise, no. Essentially the same as dereferencing the pointer.
    gwowen, Feb 19, 2011
    #12
  13. itaj sherman wrote:

    > On Feb 19, 5:19 pm, "Johannes Schaub (litb)"
    > <> wrote:
    >> Paul wrote:
    >> > Is it legal to invoke a (non static) member function without an object?

    >>
    >> > I read something in the C++ standard that it may not be standard
    >> > compliant code to invoke a (non static)member function directly from a
    >> > pointer.

    >>
    >> It is undefined behavior if you do so.
    >>
    >> struct A { void f() { } };
    >> int main() { ((A*)0)->f(); }
    >>
    >> Undefined. No object.

    >
    > struct A {};
    > void foo( A& );
    > int main() { foo( *((A*)0) ); }
    >
    > This is undefined behaviour just as well.
    > There's nothing in this issue that has specifically to do with
    > nonstatic member functions.
    >


    Yes, please act in a troll-way.
    Johannes Schaub (litb), Feb 19, 2011
    #13
  14. Paul

    Paul Guest

    "gwowen" <> wrote in message
    news:...
    On Feb 19, 11:27 am, "Paul" <> wrote:
    > Is it legal to invoke a (non static) member function without an object?
    >
    > I read something in the C++ standard that it may not be standard compliant
    > code to invoke a (non static)member function directly from a pointer.


    --If the pointer points to an object that still exists, its fine.
    --Otherwise, no. Essentially the same as dereferencing the pointer.

    Firstly sorry ABOUT INDENTING PROBS.

    Is it the case that a function pointer can be an object becuse its not
    actually a function , its a pointer.?
    I'm not sure it helps the situation anyway because what I was trying to
    deduce was whether or not a (nonstatic)member function is allowed to be
    called without the presencce of an object.
    I think object in this context is ( an instance of class type) , not purely
    an object in the context used within the C++ standard.
    Paul, Feb 20, 2011
    #14
  15. Paul

    Paul Guest

    "gwowen" <> wrote in message
    news:...
    On Feb 19, 11:27 am, "Paul" <> wrote:
    > Is it legal to invoke a (non static) member function without an object?
    >
    > I read something in the C++ standard that it may not be standard compliant
    > code to invoke a (non static)member function directly from a pointer.


    --If the pointer points to an object that still exists, its fine.
    --Otherwise, no. Essentially the same as dereferencing the pointer.

    So given that the following is true:

    "If a nonstatic member function of a class X is called for an object that
    is not of type X, or of a type derived from X, the behavior is undefined."

    It follows that if an object(or derived object) does not exist then it is
    undefined behaviour to call its respective nonstatic member function?
    Therefore it must be true that a member function does not exist without an
    object.
    Paul, Feb 20, 2011
    #15
  16. Paul

    Paul Guest

    "itaj sherman" <> wrote in message
    news:...
    On Feb 19, 3:06 pm, "Paul" <> wrote:
    > "itaj sherman" <> wrote in message
    >
    > news:...
    > On Feb 19, 2:26 pm, "Paul" <> wrote:
    >
    >
    >
    >
    >
    > > I found two quotes from the standard:

    >
    > > "5.2.2
    > > For a member function call, the postfix expression shall be an implicit
    > > (9.3.1, 9.4) or explicit class member access (5.2.5) whose idexpression
    > > is
    > > a
    > > function member name, or a pointertomember expression (5.5) selecting a
    > > function member. The first expression in the postfix expression is then
    > > called the object expression, and the call
    > > is as a member of the object pointed to or referred to. In the case of
    > > an
    > > implicit class member access, the
    > > implied object is the one pointed to by this. [Note: a member function
    > > call
    > > of the form f() is interpreted
    > > as (*this).f() (see 9.3.1). ]"

    >
    > > "9.3.1 Nonstatic member functions [class.mfct.nonstatic]
    > > 1 A nonstatic member function may be called for an object of its class
    > > type,
    > > or for an object of a class derived
    > > (10) from its class type, using the class member access syntax (5.2.5,
    > > 13.3.1.1). A nonstatic member function
    > > may also be called directly using the function call syntax (5.2.2,
    > > 13.3.1.1)
    > > - from within the body of a member function of its class or of a class
    > > derived from its class, or
    > > - from a meminitializer
    > > (12.6.2) for a constructor for its class or for a class derived from its
    > > class.
    > > If a nonstatic member function of a class X is called for an object that
    > > is
    > > not of type X, or of a type derived
    > > from X, the behavior is undefined."

    >
    > > Please note the last sentence. It seems clear to me that any nonstatic
    > > member function can *only* be called on an object.

    >
    > --Yeah, and the function:
    >
    > --void foo( X&, Y& );
    >
    > --can only be called on two objects (specifically instances of X and Y).
    > --What's so special about the member case?
    >
    > My newsreader not indenting your test , so I've manually marked your text
    > with --.
    >
    > You seem confused about the meaning of "calling a member function on an
    > object".
    > You give an example of a non member fucntion and then ask .what is so
    > special about member functions. I don't know where you are coming from ,
    > do
    > you know the difference between a member function and non member
    > function?- Hide quoted text -
    >


    --No.
    --I don't think you read carefully everything I said.

    No I try to but sorry please explain
    Paul, Feb 20, 2011
    #16
  17. Paul

    Öö Tiib Guest

    On Feb 20, 3:09 am, "Paul" <> wrote:
    > "gwowen" <> wrote in message
    >
    > news:...
    > On Feb 19, 11:27 am, "Paul" <> wrote:
    >
    > > Is it legal to invoke a (non static) member function without an object?

    >
    > > I read something in the C++ standard that it may not be standard compliant
    > > code to invoke a (non static)member function directly from a pointer.

    >
    > --If the pointer points to an object that still exists, its fine.
    > --Otherwise, no. Essentially the same as dereferencing the pointer.
    >
    > So given that the following is true:
    >
    > "If a nonstatic member function of a class X is called for an object that
    > is not of type X, or of a type derived from X, the behavior is undefined."
    >
    > It follows that if an object(or derived object) does not exist then it is
    > undefined behaviour to call its respective nonstatic member function?
    > Therefore it must be true that a member function does not exist without an
    > object.


    Nope. What follows is that A) Member function is available for calling
    regardless if any objects exist. B) In C++ language it is possible to
    call member function on something that is not really proper object of
    correct type. C) The behavior resulting by such wrong call is
    undefined, because good people do not do it.
    Öö Tiib, Feb 20, 2011
    #17
  18. Paul

    Paul Guest

    "Öö Tiib" <> wrote in message
    news:...
    On Feb 20, 3:09 am, "Paul" <> wrote:
    > "gwowen" <> wrote in message
    >
    > news:...
    > On Feb 19, 11:27 am, "Paul" <> wrote:
    >
    > > Is it legal to invoke a (non static) member function without an object?

    >
    > > I read something in the C++ standard that it may not be standard
    > > compliant
    > > code to invoke a (non static)member function directly from a pointer.

    >
    > --If the pointer points to an object that still exists, its fine.
    > --Otherwise, no. Essentially the same as dereferencing the pointer.
    >
    > So given that the following is true:
    >
    > "If a nonstatic member function of a class X is called for an object that
    > is not of type X, or of a type derived from X, the behavior is undefined."
    >
    > It follows that if an object(or derived object) does not exist then it is
    > undefined behaviour to call its respective nonstatic member function?
    > Therefore it must be true that a member function does not exist without an
    > object.


    --Nope. What follows is that A) Member function is available for calling
    --regardless if any objects exist.

    But this is non standard. And also not guaranteed because if we get non
    standard it is possible that I overwrite the memory where the function
    defintion was. So it's a very incorrect statement to make is it not?
    ..
    -- B) In C++ language it is possible to
    --call member function on something that is not really proper object of
    --correct type.
    -- C) The behavior resulting by such wrong call is
    --undefined, because good people do not do it.

    Your B and C seems to be regarding the same point. What you seem to be
    saying is that its possible to call a member function without an
    object(which we already know), but it is incorrect to do so. Does this mean
    you are saying it would be invalid standard C++ code?
    Paul, Feb 20, 2011
    #18
  19. Paul

    Öö Tiib Guest

    On Feb 20, 4:14 am, "Paul" <> wrote:
    > "Öö Tiib" <> wrote in message
    >
    > news:...
    > On Feb 20, 3:09 am, "Paul" <> wrote:
    >
    >
    >
    >
    >
    > > "gwowen" <> wrote in message

    >
    > >news:....
    > > On Feb 19, 11:27 am, "Paul" <> wrote:

    >
    > > > Is it legal to invoke a (non static) member function without an object?

    >
    > > > I read something in the C++ standard that it may not be standard
    > > > compliant
    > > > code to invoke a (non static)member function directly from a pointer.

    >
    > > --If the pointer points to an object that still exists, its fine.
    > > --Otherwise, no. Essentially the same as dereferencing the pointer.

    >
    > > So given that the following is true:

    >
    > > "If a nonstatic member function of a class X is called for an object that
    > > is not of type X, or of a type derived from X, the behavior is undefined."

    >
    > > It follows that if an object(or derived object) does not exist then it is
    > > undefined behaviour to call its respective nonstatic member function?
    > > Therefore it must be true that a member function does not exist without an
    > > object.

    >
    > --Nope. What follows is that A) Member function is available for calling
    > --regardless if any objects exist.
    >
    > But this is non standard. And also not guaranteed because if we get non
    > standard it is possible that I overwrite the memory where the function
    > defintion was. So it's a very incorrect statement to make is it not?
    > .


    Any attempts to access member functions as storage in memory that you
    can overwrite with something are, yes, non standard. In reality the
    function may exist somewhere in memory as bytes, but C++ language does
    not handle it so. In some system the executable code resides in read-
    only memory and in others the data resides in non-executable
    memory ... so it is highly platform-specific.

    > -- B) In C++ language it is possible to
    > --call member function on something that is not really proper object of
    > --correct type.
    > -- C) The behavior resulting by such wrong call is
    > --undefined, because good people do not do it.
    >
    > Your B and C seems to be regarding the same point. What you seem to be
    > saying  is that its possible to call a member function without an
    > object(which we already know), but it is incorrect to do so. Does this mean
    > you are saying it would be invalid standard C++ code?


    Yes. It will be compiled by standard-compliant compiler so it is legal
    code and it will run with undefined results so it is invalid code.
    Öö Tiib, Feb 20, 2011
    #19
  20. Paul

    gwowen Guest

    On Feb 20, 1:09 am, "Paul" <> wrote:
    > It follows that if an object(or derived object) does not exist then it is
    > undefined behaviour to call its respective nonstatic member function?
    > Therefore it must be true that a member function does not exist without an
    > object.


    No. That is simply not a syllogism. "X is undefined behaviour" is not
    equivalent to "The constituent parts of X do not exist."

    It is undefined behaviour to call strlen(const char *) on a pointer
    that does not point to a valid NULL terminated string. That does not
    mean that the strlen() function does not exist until a valid string
    exists, and that it ceases to exist when all such strings go out of
    scope.

    Setting aside the peculiar ontological implications of such a
    deduction, you can (for example) take the address of "strlen()".
    gwowen, Feb 20, 2011
    #20
    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. Chris Johnson
    Replies:
    3
    Views:
    468
    C Johnson
    Aug 14, 2003
  2. SenderX
    Replies:
    7
    Views:
    372
    Greg Comeau
    Aug 29, 2003
  3. Ed J

    Is this legal code?

    Ed J, Jun 16, 2004, in forum: C++
    Replies:
    3
    Views:
    552
  4. Jim Ford

    Is this legal C code?

    Jim Ford, Jan 26, 2004, in forum: C Programming
    Replies:
    7
    Views:
    358
    James Hu
    Jan 28, 2004
  5. Ancient_Hacker

    Weird bit of code-- legal???

    Ancient_Hacker, Sep 13, 2006, in forum: C Programming
    Replies:
    29
    Views:
    589
    jaysome
    Sep 15, 2006
Loading...

Share This Page