473,378 Members | 1,541 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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 1364
<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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Arne Kösling | last post by:
Hi ! I am new to Web Services. Therefore I ve set up a PHP Installation on Windows (PHP 4.3.2 and Apache 1.3.29). I have tested PHP alone and then installed PEAR. Now I am stuck there (Before...
2
by: php newbie | last post by:
I am trying to write a cursor to update certain rows in a particular order as follows: (I need the cursor version, not SQL, as the update logic depends on the order of rows and some other...
1
by: Harald Deischinger | last post by:
I am using a source file with the following style (I know it is not very beautiful but it is working): SRC1.cpp: static void vFoo() { // bla bla bla } static int iInit1(name, fun) { //...
3
by: Vic | last post by:
Dear All, I have a database of laboratory records in Access 2000. There is one form which acts as an interface to input experimetal data. This form incorporates information from several tables....
3
by: Stinky Pete | last post by:
I've mananged to get Access Run-time working on a Win 98 PC. I was stoked at that achievement, however I'm now stuck in that the files are opening Read only. That is, I can view all the records...
2
by: Guy Eschemann | last post by:
When running the following code through splint, I get two warnings that I don't fully understand. This is the code : --- typedef struct { int *pData; } t_MyStruct;
1
by: John Grandy | last post by:
How to remove "Read Only" status on a file on a remote server? My goal is to delete the file, but System.File.Delete(\\machine1\c$\folder1\file1.txt) is giving me an "Access is denied"...
8
by: John E Katich | last post by:
When attempt to use the Event Wizard I get the following error message: "Add/Remove of the function impossible, because the parent class code is read only" The Project was convert from VC 6.0....
206
by: WaterWalk | last post by:
I've just read an article "Building Robust System" by Gerald Jay Sussman. The article is here: http://swiss.csail.mit.edu/classes/symbolic/spring07/readings/robust-systems.pdf In it there is a...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.