Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > calling class member functions without instantiating object?

Reply
Thread Tools

calling class member functions without instantiating object?

 
 
Nagesh
Guest
Posts: n/a
 
      12-26-2005
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

 
Reply With Quote
 
 
 
 
Ivan Vecerina
Guest
Posts: n/a
 
      12-26-2005
"Nagesh" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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


 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      12-26-2005
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++.

 
Reply With Quote
 
Nagesh
Guest
Posts: n/a
 
      12-26-2005
thank u,
this is really basic stufff. I am doing c++ after a long time. I forgot
..



Happy holidays,
Nagesh

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
overloading non-template member functions with template member functions Hicham Mouline C++ 1 04-24-2009 07:47 AM
overloading non-template member functions with template member functions Hicham Mouline C++ 0 04-23-2009 11:42 AM
declaring class member functions as friend without full class definition Hicham Mouline C++ 0 11-11-2008 09:19 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class. DJ Dev ASP .Net 3 02-08-2004 04:19 PM



Advertisments