calling class member functions without instantiating object?

N

Nagesh

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
 
I

Ivan Vecerina

: 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
 
R

Rolf Magnus

Nagesh said:
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++.
 
N

Nagesh

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



Happy holidays,
Nagesh
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top