question about static "only" member functions

R

ruud.bos

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
 
D

David Fisher

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
 
R

ruud.bos

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
 
K

Karl Heinz Buchegger

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.
 

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

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top