By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,849 Members | 2,164 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,849 IT Pros & Developers. It's quick & easy.

calling class member functions without instantiating object?

P: n/a
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

Dec 26 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"Nagesh" <na************@yahoo.com> wrote in message
news:11*********************@g43g2000cwa.googlegro ups.com...
: 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
Dec 26 '05 #2

P: n/a
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++.

Dec 26 '05 #3

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

Happy holidays,
Nagesh

Dec 26 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.