473,387 Members | 1,485 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,387 software developers and data experts.

How to use static in class?

The local variables and local functions are inside class body. You
define a variable to the class "Reg reg;" in the main function. The reg
variable has a pointer. The pointer gives memory address. The memory
address accesses local variable and local function. It is fine design
according to your preferance.
I try to remove a pointer so I let local variable and local function as
global to
access to the memory directly without needing a pointer. I put static
keyword to the local function inside class.
Why can't I put static on func1(), func2(), func3(), and func4()
functions?

Here is an example of x86 instruction and take a look of my source code
below.

00411CEE call Reg::Init (4111A9h) /* Init() function has static -- one x86
instruction */

Reduced x86 instruction helps to increase critical performance in a
loop.

0041328E lea ecx,[reg] /* Init() function has non-static member in class
Reg -- two x86 instructions */
00413291 call Reg::Init (4111FEh)

According to my speed test, extra x86 instruction for the pointer is
slower than one instruction to do direct memory.

/* main.cpp */
class Reg
{
private:
unsigned char a;
unsigned char b;
unsigned char c;

unsigned char funcCycle;

void func1(void);
void func2(void);
void func3(void);
void func4(void);

typedef void (Reg::*pfunc)();
static const pfunc pf[4];

public:
static void Init (void);
static void Run(void);
} _reg;

const Reg::pfunc Reg::pf[4] = { &Reg::func1, &Reg::func2, &Reg::func3,
&Reg::func4 };

void Reg::Init (void)
{
_reg.funcCycle = 0;
}

void Reg::func1(void)
{
_reg.a += 2;
_reg.b += 4;
_reg.c += 8;
}

void Reg::func2(void)
{
_reg.a -= 2;
_reg.b -= 4;
_reg.c -= 8;
}

void Reg::func3(void)
{
_reg.a *= 2;
_reg.b *= 4;
_reg.c *= 8;
}

void Reg::func4(void)
{
_reg.a /= 2;
_reg.b /= 4;
_reg.c /= 8;
}

void Reg::Run(void)
{
(_reg.*pf[_reg.funcCycle])();
_reg.funcCycle++;
_reg.funcCycle &= 3;
}

int main()
{
_reg.Init();

int count = 0;
while (count++ < 10)
_reg.Run();

return 0;
}

--

Yours Truly,
Bryan Parkoff


Jun 27 '08 #1
3 1740
Bryan Parkoff wrote:
The local variables and local functions are inside class body. You
define a variable to the class "Reg reg;" in the main function. The reg
variable has a pointer. The pointer gives memory address. The memory
address accesses local variable and local function. It is fine design
according to your preferance.
I try to remove a pointer so I let local variable and local function as
global to
access to the memory directly without needing a pointer. I put static
keyword to the local function inside class.
Why can't I put static on func1(), func2(), func3(), and func4()
functions?
I've no idea what you are trying to do, but the only thing preventing
you from declaring the member functions static is

typedef void (Reg::*pfunc)();

followed by

const Reg::pfunc Reg::pf[4] = { &Reg::func1, &Reg::func2, &Reg::func3,
&Reg::func4 };

the type of pfunc is a member function. if you want to use static
member functions, use

typedef void (*pfunc)();
>
/* main.cpp */
class Reg
{
private:
unsigned char a;
unsigned char b;
unsigned char c;

unsigned char funcCycle;

void func1(void);
drop the (void) () is idiomatic C++.

--
Ian Collins.
Jun 27 '08 #2
On 15 huhti, 07:13, "Bryan Parkoff" <nos...@nospam.comwrote:
According to my speed test, extra x86 instruction for the
pointer is slower than one instruction to do direct memory.
If you want that kind of optimization then C++ is probably
wrong language for that purpose. Try assembly.
Jun 27 '08 #3
> According to my speed test, extra x86 instruction for the
>pointer is slower than one instruction to do direct memory.

If you want that kind of optimization then C++ is probably
wrong language for that purpose. Try assembly.
Ian Collins already answered my question in another thread. To do x86
assembly is not the best option. I depend on C++ Compiler for best
optimization. I am satisified with Ian Collins' answer. It does what I
need.

Yours Truly,
Bryan Parkoff
Jun 27 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: newbiecpp | last post by:
Java can declare a static nested class. Does C++ have same thing like? class Outer { public: static class Inner { ... }; .... };
15
by: Samee Zahur | last post by:
Question: How do friend functions and static member functions differ in terms of functionality? I mean, neither necessarily needs an object of the class to be created before they are called and...
3
by: Mauzi | last post by:
hi, this may sound odd and noob like, but what is the 'big' difference between static and non-static funcitons ? is there any performace differnce? what is the best way to use them ? thnx ...
3
by: Kirk Marple | last post by:
Just want to see if this is 'by design' or a bug... I have a common List<T> defined in a base class, and the base class has a static property to expose this list. I wanted the derived class to...
9
by: Laban | last post by:
Hi, I find myself using static methods more than I probably should, so I am looking for some advice on a better approach. For example, I am writing an app that involves quite a bit of database...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
12
by: Hemanth | last post by:
Hi, I have a base class with a static constructor and some abstract methods. Derived classes implement these methods. From articles on the web, it appears that there is no guarentee that this...
13
by: learning | last post by:
Hi I have a static class written by other team which encapsulates a database instance. but I need to extend it to incldue other things. I know that C# static class is sealed and can;t be inherited...
14
by: Jess | last post by:
Hello, I learned that there are five kinds of static objects, namely 1. global objects 2. object defined in namespace scope 3. object declared static instead classes 4. objects declared...
5
by: Andy B | last post by:
I have a class that I want to make static but it uses some objects that are instance objects. I keep getting a compiler error saying something about using instance objects in a static class or...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.