calling class member functions without instantiating object?

Discussion in 'C++' started by Nagesh, Dec 26, 2005.

  1. Nagesh

    Nagesh Guest

    hi,

    I have seen the winvnc(tightvnc server) source code in this I seen
    that class member funtions are calling without instantiating the object
    i.e. like vncService::ShowDefaultProperties() where vncService is a
    class name not an refrence or instantiated object.
    is the above notation is possible or not?if yes how should i declare
    that class(vncService) so that i can call without instantiating the
    object. If any of u know pls answer to this.


    thanking u
    Nagesh
    Nagesh, Dec 26, 2005
    #1
    1. Advertising

  2. "Nagesh" <> wrote in message
    news:...
    : I have seen the winvnc(tightvnc server) source code in this I seen
    : that class member funtions are calling without instantiating the object
    : i.e. like vncService::ShowDefaultProperties() where vncService is a
    : class name not an refrence or instantiated object.
    : is the above notation is possible or not?if yes how should i declare
    : that class(vncService) so that i can call without instantiating the
    : object. If any of u know pls answer to this.

    This is not about the way that the class is declared.
    But the method itself has to be declared as static:
    class vncService {
    public:
    static void ShowDefaultProperties();
    };

    Note that if no instance of vncService is to be created (i.e. it is not
    a class of objects), then you will want to use a namespace instead:
    namespace vncService {
    void ShowDefaultProperties();
    }

    [ Historically, some highly portable code has been using static
    class members rather than namespaces to support outdated compilers.
    But 10 years later, it is unreasonable to do so in new code... ]


    Happy holidays,
    Ivan
    --
    http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
    Brainbench MVP for C++ <> http://www.brainbench.com
    Ivan Vecerina, Dec 26, 2005
    #2
    1. Advertising

  3. Nagesh

    Rolf Magnus Guest

    Nagesh wrote:

    > hi,
    >
    > I have seen the winvnc(tightvnc server) source code in this I seen
    > that class member funtions are calling without instantiating the object
    > i.e. like vncService::ShowDefaultProperties() where vncService is a
    > class name not an refrence or instantiated object.
    > is the above notation is possible or not?


    Yes.

    > if yes how should i declare that class(vncService) so that i can call
    > without instantiating the object.


    It's not the class, but the function that has to be declared in a special
    way. It must be made static.

    class Foo
    {
    public:
    void object_function()
    {
    std::cout << "This must be called on an instance of Foo\n";
    }

    static void class_function()
    {
    std::cout << "This is called on the class itself\n";
    }
    };

    int main()
    {
    Foo f;
    f.object_function();
    Foo::class_function();
    }

    > If any of u know pls answer to this.


    This is very basic stuff. You should get a good book about C++.
    Rolf Magnus, Dec 26, 2005
    #3
  4. Nagesh

    Nagesh Guest

    thank u,
    this is really basic stufff. I am doing c++ after a long time. I forgot
    ..



    Happy holidays,
    Nagesh
    Nagesh, Dec 26, 2005
    #4
    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. Dave
    Replies:
    1
    Views:
    753
    Gianni Mariani
    Nov 19, 2003
  2. Replies:
    22
    Views:
    1,479
    Marcus Kwok
    Mar 23, 2006
  3. Replies:
    17
    Views:
    474
    Pete Becker
    Dec 16, 2007
  4. Hicham Mouline
    Replies:
    0
    Views:
    418
    Hicham Mouline
    Apr 23, 2009
  5. Hicham Mouline
    Replies:
    1
    Views:
    403
    Michael DOUBEZ
    Apr 24, 2009
Loading...

Share This Page