Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > question about static "only" member functions

Reply
Thread Tools

question about static "only" member functions

 
 
ruud.bos@gmail.com
Guest
Posts: n/a
 
      08-26-2005
Hi list,

As a C++ newbie, I have a question about static member functions.
Suppose I have the following class definition:

class MyClass
{
public:
static void MyFunc();
};

static void MyClass::MyFunc()
{
// Do something useful here
}

Now I would like to enforce static usage of this function (i.e. if an
instance of this class is created, it should not be possible to use
MyFunc on that instance (might throw an exception), only
MyClass::MyFunc() should work)

Since static functions don't have access to the this prointer, I can't
check whether the function is invoked from an instance or as a static.
Does someone know a solution to this? Any help would be appreciated.

Cheers,
Ruud Bos

 
Reply With Quote
 
 
 
 
David Fisher
Guest
Posts: n/a
 
      08-26-2005
<> wrote in message
news: oups.com...
> Suppose I have the following class definition:
>
> class MyClass
> {
> public:
> static void MyFunc();
> };
>
> static void MyClass::MyFunc()


Don't need to write "static" in the definition of MyFunc(), just in the
declaration (inside the class definition), as you have already done above.

> {
> // Do something useful here
> }
>
> Now I would like to enforce static usage of this function (i.e. if an
> instance of this class is created, it should not be possible to use
> MyFunc on that instance (might throw an exception), only
> MyClass::MyFunc() should work)


It is already enforced (by the language definition) ... it is not possible
to call MyFunc() with an object. If your compiler allows "MyClass x;
x.MyFunc();" it is lying to you, and calling MyClass::MyFunc() without an
object anyway.

David Fisher


 
Reply With Quote
 
 
 
 
ruud.bos@gmail.com
Guest
Posts: n/a
 
      08-26-2005
Then I'm affraid my MS vc98 compiler is lying on me, since I'm able to
create an instance and invoke MyFunc() without problems. Anyway, thanks
for the help.

And btw sorry for the copy/paste error in the MyFunc function
definition.
Should indeed be void MyClass::MyFunc() without the static modifier

Ruud Bos

 
Reply With Quote
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      08-26-2005
wrote:
>
> Hi list,
>
> As a C++ newbie, I have a question about static member functions.
> Suppose I have the following class definition:
>
> class MyClass
> {
> public:
> static void MyFunc();
> };
>
> static void MyClass::MyFunc()
> {
> // Do something useful here
> }
>
> Now I would like to enforce static usage of this function (i.e. if an
> instance of this class is created, it should not be possible to use
> MyFunc on that instance (might throw an exception), only
> MyClass::MyFunc() should work)
>
> Since static functions don't have access to the this prointer, I can't
> check whether the function is invoked from an instance or as a static.
> Does someone know a solution to this?


There is no real solution. Maybe because there really is no solution needed.

Just curious: Why do think there is a problem in practice?
A static member function is just a free standing function residing
in the class scope. So if you can live with dropping that scope, just
make it a free standing function.

--
Karl Heinz Buchegger

 
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
question re. usage of "static" within static member functions of aclass ssb C++ 58 09-14-2009 03:09 PM
static member functions vs. member function pointers paul C++ 8 04-30-2009 11:03 AM
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
Can a static member function access non-static member? dolphin C++ 3 12-05-2007 12:39 PM



Advertisments