invoking member functions without creating an object or pointer of the class?

Discussion in 'C++' started by ypjofficial@indiatimes.com, Jan 16, 2006.

  1. Guest

    Is there any possibility of invoking the member functions of a class
    without creating an object (or even a pointer to ) of that class.

    eg.
    #include <iostream.h>
    class test
    {
    public:
    void fun()
    {
    cout<<"Inside test::fun\n";
    }
    };

    I want to call fun() of class test without creating
    test t or even test * ptr;?
    I searched on the net for the convincing answer but didn't get any.
    (http://www.devx.com/tips/Tip/15846 )

    Or is this a totally insane question? (I won't mind if anyone agrees
    with that :)..)

    Thanks and Regards,
    Yogesh Joshi


    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
    , Jan 16, 2006
    #1
    1. Advertising

  2. Re: invoking member functions without creating an object or pointerof the class?

    wrote:
    > Is there any possibility of invoking the member functions of a class
    > without creating an object (or even a pointer to ) of that class.


    No, unless it's a static member.

    >
    > eg.
    > #include <iostream.h>


    No such standard header.

    > class test
    > {
    > public:
    > void fun()
    > {
    > cout<<"Inside test::fun\n";
    > }
    > };
    >
    > I want to call fun() of class test without creating
    > test t or even test * ptr;?


    Since your 'fun' function does not need an object, make it 'static'.

    > I searched on the net for the convincing answer but didn't get any.
    > (http://www.devx.com/tips/Tip/15846 )
    >
    > Or is this a totally insane question? (I won't mind if anyone agrees
    > with that :)..)


    It's not. But it seems to stem from a poor design (or misunderstanding
    of the purpose of non-static member functions).

    V
    Victor Bazarov, Jan 16, 2006
    #2
    1. Advertising

  3. Marcus Kwok Guest

    In comp.lang.c++ wrote:
    > Is there any possibility of invoking the member functions of a class
    > without creating an object (or even a pointer to ) of that class.
    >
    > eg.
    > #include <iostream.h>
    > class test
    > {
    > public:
    > void fun()
    > {
    > cout<<"Inside test::fun\n";
    > }
    > };
    >
    > I want to call fun() of class test without creating
    > test t or even test * ptr;?
    > I searched on the net for the convincing answer but didn't get any.
    > (http://www.devx.com/tips/Tip/15846 )


    Look at static member functions.

    --
    Marcus Kwok

    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
    Marcus Kwok, Jan 16, 2006
    #3
  4. W Marsh Guest

    On 16 Jan 2006 09:14:36 -0500, wrote:

    >Is there any possibility of invoking the member functions of a class
    >without creating an object (or even a pointer to ) of that class.
    >
    >eg.
    >#include <iostream.h>
    >class test
    >{
    >public:
    > void fun()
    > {
    > cout<<"Inside test::fun\n";
    > }
    >};
    >
    >I want to call fun() of class test without creating
    >test t or even test * ptr;?
    >I searched on the net for the convincing answer but didn't get any.
    >(http://www.devx.com/tips/Tip/15846 )
    >
    >Or is this a totally insane question? (I won't mind if anyone agrees
    >with that :)..)
    >
    >Thanks and Regards,
    >Yogesh Joshi


    fun must be a static member function.

    static void fun()
    {
    std::cout << "inside test::fun\n";
    }

    As the method doesn't use the this pointer at all, it probably should
    be static anyway.

    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
    W Marsh, Jan 16, 2006
    #4
  5. wrote:
    > Is there any possibility of invoking the member functions of a class
    > without creating an object (or even a pointer to ) of that class.


    []

    > Or is this a totally insane question? (I won't mind if anyone agrees
    > with that :)..)


    It seems like one.

    A member function of a properly designed class accesses object's data
    members. If it does not, then the function should not be a member. So,
    if you don't have the data the function operates upon, where is the
    function is supposed to find the data?

    Bear in mind, that a member function is a function with a hidded
    argument that has name 'this' inside the function.

    Given the declaration:

    struct A { void foo(); };

    A::foo() is essentially

    void foo(A* const this);

    And you can hack the language and call it without a valid object like
    this:

    ((A*)0)->foo();

    But once the function accesses data pointed to by 'this' pointer you
    are in troubles.


    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
    Maxim Yegorushkin, Jan 16, 2006
    #5
  6. Howard Guest

    [re-posting after apparent send failure]

    "Maxim Yegorushkin" <> wrote in message
    news:...
    >
    > wrote:
    >> Is there any possibility of invoking the member functions of a class
    >> without creating an object (or even a pointer to ) of that class.

    >
    > []
    >
    >> Or is this a totally insane question? (I won't mind if anyone agrees
    >> with that :)..)

    >
    > It seems like one.
    >


    Why?

    > A member function of a properly designed class accesses object's data
    > members. If it does not, then the function should not be a member. So,
    > if you don't have the data the function operates upon, where is the
    > function is supposed to find the data?
    >


    What about static members? Are you suggesting that they serve no purpose?
    (And, they CAN access static member variables, just not non-static ones.)

    -Howard
    Howard, Jan 16, 2006
    #6
  7. Re: invoking member functions without creating an object or pointerof the class?

    Howard wrote:
    > [re-posting after apparent send failure]
    >
    > "Maxim Yegorushkin" <> wrote in message
    > news:...
    >
    >> wrote:
    >>
    >>>Is there any possibility of invoking the member functions of a class
    >>>without creating an object (or even a pointer to ) of that class.

    >>
    >>[]
    >>
    >>
    >>>Or is this a totally insane question? (I won't mind if anyone agrees
    >>>with that :)..)

    >>
    >>It seems like one.
    >>

    >
    >
    > Why?
    >
    >
    >>A member function of a properly designed class accesses object's data
    >>members. If it does not, then the function should not be a member. So,
    >>if you don't have the data the function operates upon, where is the
    >>function is supposed to find the data?
    >>

    >
    >
    > What about static members? Are you suggesting that they serve no purpose?
    > (And, they CAN access static member variables, just not non-static ones.)


    The CAN access non-static members for any instance of the class, provided
    they have a pointer or a reference to that instance, or they create one
    internally.

    V
    Victor Bazarov, Jan 16, 2006
    #7
  8. benben Guest

    Re: invoking member functions without creating an object or pointer of the class?

    The whole point of using member functions is that they are invoked with
    an object. If you don't want that, don't use a (non-static) member
    function. Use a standalone function or a static member function.

    But if that's for debugging purpose, maybe you can use some pointer
    trick. But I'm not sure how valid the program would go;

    test* t = 0;
    t->fun();

    Ben

    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
    benben, Jan 16, 2006
    #8
  9. Guest

    class test
    {
    public:
    static void fun()
    {
    cout<<"Inside test::fun\n";
    }



    };


    then call test::fun() from main.

    there's things to be aware of with static functions so you might want
    to read up on them if you haven't used them before.


    BTW no such thing as an insane question on this forum... ;)


    hope this helps

    G


    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
    , Jan 16, 2006
    #9
  10. Shark Guest

    >The CAN access non-static members for any instance of the class, provided
    >they have a pointer or a reference to that instance, or they create one
    >internally.


    >V


    They can access private non-static data members? is this class valid
    then:
    class A {
    private:
    int x;
    public:
    static void blah(A* this)
    {
    //assuming x is initialized
    std::cout<<this->x<<std::endl;
    }

    };
    Shark, Jan 16, 2006
    #10
  11. Shark Guest

    >The CAN access non-static members for any instance of the class, provided
    >they have a pointer or a reference to that instance, or they create one
    >internally.
    >V


    Ok, apologies! The example above will not work, but this will work:

    class A {
    private:
    int x;
    public:
    A()
    :x(10)
    {
    }
    static void blah()
    {
    A* u;
    cout<<u->x<<endl;
    }
    };
    Shark, Jan 16, 2006
    #11
  12. Re: invoking member functions without creating an object or pointerof the class?

    Shark wrote:

    >>The CAN access non-static members for any instance of the class, provided
    >>they have a pointer or a reference to that instance, or they create one
    >>internally.

    >
    >
    >>V

    >
    >
    > They can access private non-static data members? is this class valid
    > then:
    > class A {
    > private:
    > int x;
    > public:
    > static void blah(A* this)


    No. 'this' is a keyword, it cannot be used here. Rewrite it and use
    'that', for example.

    > {
    > //assuming x is initialized
    > std::cout<<this->x<<std::endl;


    Same thing. 'this' is a keyword. Use the actual name of the argument
    once you've corrected it there.

    > }
    >
    > };
    >


    Other than the errors I pointed out, yes, the code is well-formed.

    V
    Victor Bazarov, Jan 16, 2006
    #12
  13. Re: invoking member functions without creating an object or pointerof the class?

    Shark wrote:
    >>The CAN access non-static members for any instance of the class, provided
    >>they have a pointer or a reference to that instance, or they create one
    >>internally.
    >>V

    >
    >
    > Ok, apologies! The example above will not work, but this will work:
    >
    > class A {
    > private:
    > int x;
    > public:
    > A()
    > :x(10)
    > {
    > }
    > static void blah()
    > {
    > A* u;
    > cout<<u->x<<endl;


    No. This program has undefined behaviour because it dereferences
    an uninitialised pointer. What would work is this:

    A u;
    std::cout << u.x << std::endl;

    (provided proper includes have been made).

    > }
    > };
    >


    V
    Victor Bazarov, Jan 16, 2006
    #13
  14. Attila Feher Guest

    Re: invoking member functions without creating an object or pointer of the class?

    wrote:
    > Is there any possibility of invoking the member functions of a class
    > without creating an object (or even a pointer to ) of that class.
    >
    > eg.
    > #include <iostream.h>
    > class test
    > {
    > public:
    > void fun()
    > {
    > cout<<"Inside test::fun\n";
    > }
    > };
    >
    > I want to call fun() of class test without creating
    > test t or even test * ptr;?
    > I searched on the net for the convincing answer but didn't get any.
    > (http://www.devx.com/tips/Tip/15846 )


    I think you are looking for a static member function. Get your favorite
    C++ texbook, and read about them. Those kind of functions are member
    functions of a class, but they do not need an object to work on - and
    they do not get an object to work on (unless you pass it as a named
    argument). To simply put: static member functions see and have no
    "this" pointer.

    #include <iostream> // iostream.h is not standard

    class test {
    public:
    static void fun() {
    std::cout<<"Inside test::fun\n";
    }
    };

    The above should work.

    > Or is this a totally insane question? (I won't mind if anyone agrees
    > with that :)..)


    Ehem... If you want to call fun() as a function that works on an
    instance of that class but you don't want to tell which, then it is
    pretty much hopeless. :)

    WW aka Attila

    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
    Attila Feher, Jan 16, 2006
    #14
  15. <> wrote in message
    news:...

    > Is there any possibility of invoking the member functions of a class
    > without creating an object (or even a pointer to ) of that class.


    Sure -- just make the member function static. Static member functions are
    not permitted to use the "this" pointer, which means that you don't need an
    instance of the object to invoke one.



    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
    Andrew Koenig, Jan 16, 2006
    #15
  16. Howard Guest

    "Maxim Yegorushkin" <> wrote in message
    news:...
    >
    > wrote:
    >> Is there any possibility of invoking the member functions of a class
    >> without creating an object (or even a pointer to ) of that class.

    >
    > []
    >
    >> Or is this a totally insane question? (I won't mind if anyone agrees
    >> with that :)..)

    >
    > It seems like one.
    >


    Why?

    > A member function of a properly designed class accesses object's data
    > members. If it does not, then the function should not be a member. So,
    > if you don't have the data the function operates upon, where is the
    > function is supposed to find the data?
    >


    What about static members? Are you suggesting that they serve no purpose?
    (And, they CAN access static member variables, just not non-static ones.)

    -Howard



    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
    Howard, Jan 16, 2006
    #16
  17. Howard wrote:
    > [re-posting after apparent send failure]
    >
    > "Maxim Yegorushkin" <> wrote in message
    > news:...
    > >
    > > wrote:
    > >> Is there any possibility of invoking the member functions of a class
    > >> without creating an object (or even a pointer to ) of that class.

    > >
    > > []
    > >
    > >> Or is this a totally insane question? (I won't mind if anyone agrees
    > >> with that :)..)

    > >
    > > It seems like one.
    > >

    > Why?


    The following elaborates that.

    > > A member function of a properly designed class accesses object's data
    > > members. If it does not, then the function should not be a member. So,
    > > if you don't have the data the function operates upon, where is the
    > > function is supposed to find the data?

    >
    > What about static members?


    The language supports them.

    > Are you suggesting that they serve no purpose?


    I do not. I wrote here about nonstatic member functions, rather than
    static member functions, because OP did not ask about static ones.

    > (And, they CAN access static member variables, just not non-static ones.)


    They can access any member variables, including non static, as Victor
    has already noted.
    Maxim Yegorushkin, Jan 17, 2006
    #17
  18. Re: invoking member functions without creating an object or pointer of the class?

    Maxim Yegorushkin wrote:
    > Bear in mind, that a member function is a function with a hidded
    > argument that has name 'this' inside the function.
    >
    > Given the declaration:
    >
    > struct A { void foo(); };
    >
    > A::foo() is essentially
    >
    > void foo(A* const this);


    With the fundamental difference that in order to call the
    member function A::foo(), you need an object of class type
    A, so if you call foo() with a pointer to A, the latter is
    dereferenced;
    pA->foo();
    is equivalent to
    (*pA).foo();

    > And you can hack the language and call it without a valid object like
    > this:
    >
    > ((A*)0)->foo();
    >
    > But once the function accesses data pointed to by 'this' pointer you
    > are in troubles.


    Or when foo() happens to be virtual, or when you use some
    exotic compiler / compiler options, or when you compile the
    program after midnight at Full Moon - that's what Undefined
    Behaviour is about.

    By the way, even a test
    if(this == 0)
    in the body of foo() may be optimised away by the compiler,
    or may fail to detect that foo() was called on a NULL pointer
    - especially when called on a pointer to a derived class where
    pointers might need adjustment due to multiple inheritance.

    Falk

    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
    =?ISO-8859-1?Q?Falk_Tannh=E4user?=, Jan 17, 2006
    #18
  19. kanze Guest

    wrote:
    > Is there any possibility of invoking the member functions of a
    > class without creating an object (or even a pointer to ) of
    > that class.


    > eg.
    > #include <iostream.h>


    Since someone else pointed out that this header is out of date:

    The standard conformant solution (and the one you should
    definitly prefer unless you are stuck with millions of lines of
    legacy code) is

    #include <iostream>
    #include <ostream>

    and to prepend all references to objects and functions in the
    standard library with std::, e.g. std::cout, instead of cout.

    Both headers are necessary.

    > class test
    > {
    > public:
    > void fun()
    > {
    > cout<<"Inside test::fun\n";
    > }
    > };


    > I want to call fun() of class test without creating test t or
    > even test * ptr;? I searched on the net for the convincing
    > answer but didn't get any.


    As others have pointed out, the answer is to declare the
    function static.

    > (http://www.devx.com/tips/Tip/15846 )


    The example here is totally illegal. If this is the sort of
    thing posted at this site, avoid it like the plague. It's just
    plain wrong.

    --
    James Kanze GABI Software
    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


    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
    kanze, Jan 17, 2006
    #19
  20. Howard Guest

    "Maxim Yegorushkin" <> wrote in message
    news:...
    >
    > Howard wrote:
    >> [re-posting after apparent send failure]
    >>
    >> "Maxim Yegorushkin" <> wrote in message
    >> news:...
    >> >
    >> > wrote:
    >> >> Is there any possibility of invoking the member functions of a class
    >> >> without creating an object (or even a pointer to ) of that class.
    >> >
    >> > []
    >> >
    >> >> Or is this a totally insane question? (I won't mind if anyone agrees
    >> >> with that :)..)
    >> >
    >> > It seems like one.
    >> >

    >> Why?

    >
    > The following elaborates that.


    I see nothing below that elaborates why the question is "insane".

    >> What about static members?

    >
    > The language supports them.


    And they are how to accomplish what the OP asked.

    >
    >> Are you suggesting that they serve no purpose?

    >
    > I do not. I wrote here about nonstatic member functions, rather than
    > static member functions, because OP did not ask about static ones.


    He asked for a solution. Static member functions _are_ a solution, to
    _exactly_ this kind of problem. (If he knew about static members, then why
    ask the question?)

    >
    >> (And, they CAN access static member variables, just not non-static ones.)

    >
    > They can access any member variables, including non static, as Victor
    > has already noted.
    >


    I know that. I never suggested a static function could _not_ access
    non-static data. (Although they need to refer to a specific object, which
    is what the OP didn't want.) I was merely arguing against your point that
    the design must be wrong. I'll repeat a portion of your answer. You said:

    >> > A member function of a properly designed class accesses object's data
    >> > members. If it does not, then the function should not be a member. So,
    >> > if you don't have the data the function operates upon, where is the
    >> > function is supposed to find the data?


    You suggest you don't "have the data", and ask where the function is
    supposed to find the data. My point was that static members can still
    access static members, even if they can't access non-static members, at
    least without referring to some specific object (which is what the OP was
    asking about).

    Also, everything in that paragraph suggests that a "proper design" would
    make a function either a (non-static) member or a non-member. I was
    pointing out (as much to the OP as to you), that static functions are a
    perfectly valid solution. And if you'll look at all the other posts, they
    say the same thing I did.

    Seems to me the question isn't "insane" at all.

    -Howard
    Howard, Jan 17, 2006
    #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. Nagesh
    Replies:
    3
    Views:
    724
    Nagesh
    Dec 26, 2005
  2. ma740988
    Replies:
    4
    Views:
    414
    ma740988
    Apr 7, 2006
  3. Hicham Mouline
    Replies:
    0
    Views:
    418
    Hicham Mouline
    Apr 23, 2009
  4. Hicham Mouline
    Replies:
    1
    Views:
    403
    Michael DOUBEZ
    Apr 24, 2009
  5. Stephen Howe
    Replies:
    2
    Views:
    276
    Stephen Howe
    Nov 6, 2012
Loading...

Share This Page