question about static "only" member functions

Discussion in 'C++' started by ruud.bos@gmail.com, Aug 26, 2005.

  1. Guest

    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, 2005
    #1
    1. Advertising

  2. David Fisher Guest

    <> wrote in message
    news:...
    > 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
     
    David Fisher, Aug 26, 2005
    #2
    1. Advertising

  3. Guest

    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, 2005
    #3
  4. 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
     
    Karl Heinz Buchegger, Aug 26, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dolphin
    Replies:
    3
    Views:
    1,362
    Pete Becker
    Dec 5, 2007
  2. Hicham Mouline
    Replies:
    0
    Views:
    441
    Hicham Mouline
    Apr 23, 2009
  3. Hicham Mouline
    Replies:
    1
    Views:
    423
    Michael DOUBEZ
    Apr 24, 2009
  4. paul
    Replies:
    8
    Views:
    726
    Alf P. Steinbach
    Apr 30, 2009
  5. ssb
    Replies:
    58
    Views:
    1,678
    Chris M. Thomasson
    Sep 14, 2009
Loading...

Share This Page