469,323 Members | 1,475 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,323 developers. It's quick & easy.

question about static "only" member functions

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

Aug 26 '05 #1
3 1239
<ru******@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr 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
Aug 26 '05 #2
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

Aug 26 '05 #3
ru******@gmail.com 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
kb******@gascad.at
Aug 26 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Arne Kösling | last post: by
3 posts views Thread by Stinky Pete | last post: by
2 posts views Thread by Guy Eschemann | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.