C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 07-23-2009, 08:36 PM   #1
Registered User
 
Join Date: Jul 2009
Posts: 24
accessing private class data low overhead

currently I am using pointers and functions to access private class date

in

ptrClassA->Setval;
void setval(double val) {itsval = val;}
ptrClassA->Getval
double Getval() {return itsval;}

is there a faster way to do

ptrClassA->itsval ?

many thanks
lawrenced is offline   Reply With Quote
Old 07-23-2009, 09:15 PM   #2
(?<!re)tired
 
Mario F.'s Avatar
 
Join Date: May 2006
Location: Portugal
Posts: 5,220
Quote:
is there a faster way to do

ptrClassA->itsval ?
Make it public?

By implement set and get functions you are theoretically doing the right thing, since you are controlling the access to your class members and providing a common interface that users of your class (you and other programmers declaring objects of your class) can expect.

Some may argue that on examples like yours where the setter and getter functions limit themselves to assign to and retrieve the private member, there is no need for those functions and you could simply make itsval public. That would allow you to use ptrClassA->itsval and skip a function call.

I'd be tempted to agree, however there's two disadvantages of the top of my head you should consider:

. You are limiting your ability to maintain the class. If sometime later you decide that after all you want the class to do other things besides itsval = val; or return itsval; when assigning or retrieving that member, you will then need to create those functions. But the problem is that you have code all around that will stop working as intended and needs to be changed. Anywhere where you are doing something like ptrClassA->itsval = 13.5 or double val = ptrClassA->itsval.

. The other problem (maybe less important, dunno. I find it important though) is that by not implementing functions of this type, users of this class cannot expect a common interface. That is, if all your members are private and to access them they need to use Getxxx and Setxxx (or any other consistent nomenclature you decide) it makes your class easier to use.
__________________
Originally Posted by brewbuck:
Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.



Last edited by Mario F.; 07-23-2009 at 09:18 PM.
Mario F. is online now   Reply With Quote
Old 07-23-2009, 09:28 PM   #3
Registered User
 
Join Date: Jul 2009
Posts: 24
Quote:
Originally Posted by Mario F. View Post
Make it public?


Some may argue that on examples like yours where the setter and getter functions limit themselves to assign to and retrieve the private member, there is no need for those functions and you could simply make itsval public. That would allow you to use ptrClassA->itsval and skip a function call.
how much time will this impact my code if i may be doing multi millions of loops?
lawrenced is offline   Reply With Quote
Old 07-23-2009, 11:24 PM   #4
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,352
You may want to consider implementing those functions inline with the class definition first, to see if avoiding the function call overhead has any measurable improvement on performance.
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is offline   Reply With Quote
Old 07-24-2009, 08:58 AM   #5
Senior software engineer
 
brewbuck's Avatar
 
Join Date: Mar 2007
Location: Portland, OR
Posts: 5,381
Make the set/access functions inline and forget it. If the compiler refuses to inline them, throw your compiler out the window.

Altering the visibility of a member as a speed optimization is unacceptably wrong
__________________
"Congratulations on your purchase. To begin using your quantum computer, set the power switch to both off and on simultaneously." -- raftpeople@slashdot
brewbuck is offline   Reply With Quote
Old 07-24-2009, 09:40 AM   #6
(?<!re)tired
 
Mario F.'s Avatar
 
Join Date: May 2006
Location: Portugal
Posts: 5,220
Yes. I was trying to make a case exactly for that. Should have worded it better. Didn't want the OP to think I was suggesting making it public.
__________________
Originally Posted by brewbuck:
Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.


Mario F. is online now   Reply With Quote
Old 07-24-2009, 10:09 AM   #7
Senior software engineer
 
brewbuck's Avatar
 
Join Date: Mar 2007
Location: Portland, OR
Posts: 5,381
Quote:
Originally Posted by Mario F. View Post
Yes. I was trying to make a case exactly for that. Should have worded it better. Didn't want the OP to think I was suggesting making it public.
It's not really as bad as I make it sound, because making the function inline means you have to expose its internals inside a header file, which means your implementation isn't black-box anymore. One of the fundamental problems with C++, IMHO.

Of course, I can't actually imagine a standard way of allowing specification of inline functions without revealing their code.

No wait, I can, but it involves cryptography and that's just goofy
__________________
"Congratulations on your purchase. To begin using your quantum computer, set the power switch to both off and on simultaneously." -- raftpeople@slashdot
brewbuck is offline   Reply With Quote
Old 07-24-2009, 11:06 AM   #8
(?<!re)tired
 
Mario F.'s Avatar
 
Join Date: May 2006
Location: Portugal
Posts: 5,220
No. It's alright. It was the OP answer to my post that scared me
__________________
Originally Posted by brewbuck:
Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.


Mario F. is online now   Reply With Quote
Old 07-24-2009, 12:44 PM   #9
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
Quote:
Originally Posted by brewbuck View Post
It's not really as bad as I make it sound, because making the function inline means you have to expose its internals inside a header file, which means your implementation isn't black-box anymore. One of the fundamental problems with C++, IMHO.

Of course, I can't actually imagine a standard way of allowing specification of inline functions without revealing their code.
Ah, but both MSVC and GCC 4.x can inline non-inline functions even if they're in different source files, so it's not all that bad.
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 07-24-2009, 03:19 PM   #10
Cat without Hat
 
CornedBee's Avatar
 
Join Date: Apr 2003
Posts: 8,439
4.x can't do it. 4.5 will be able to, but you'll have to explicitly request it.
__________________
All the buzzt!
CornedBee

"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
CornedBee is offline   Reply With Quote
Old 07-24-2009, 03:20 PM   #11
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
Ah, 4.5+ then. But that's alright. You have to explicitly ask MSVC too.
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 07-24-2009, 03:32 PM   #12
Senior software engineer
 
brewbuck's Avatar
 
Join Date: Mar 2007
Location: Portland, OR
Posts: 5,381
Quote:
Originally Posted by Elysia View Post
Ah, but both MSVC and GCC 4.x can inline non-inline functions even if they're in different source files, so it's not all that bad.
I was thinking more along the lines of a user library, where you do not have the source in the first place. What might have been a completely opaque binary implementation now has bits and pieces of the internals leaking out in the form of inline function definitions in the headers.
__________________
"Congratulations on your purchase. To begin using your quantum computer, set the power switch to both off and on simultaneously." -- raftpeople@slashdot
brewbuck is offline   Reply With Quote
Old 07-24-2009, 05:38 PM   #13
(?<!re)tired
 
Mario F.'s Avatar
 
Join Date: May 2006
Location: Portugal
Posts: 5,220
Well, one could save a function call for the getter, retain class opacity and pay with a slight increase in size. Could be surprising at first, but if done consistently I guess class users would get used to it.

Code:
class myClass {

public:

    myClass(): member_(0), member(member_) {}
    myClass(double val): member_(val), member(member_) {}
    
    void member(double val) { member_ = val; }

    const double &member;

private:

    double member_;

};
__________________
Originally Posted by brewbuck:
Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.


Mario F. is online now   Reply With Quote
Old 07-25-2009, 04:25 AM   #14
Cat without Hat
 
CornedBee's Avatar
 
Join Date: Apr 2003
Posts: 8,439
I don't see how that is any advantage over exposing the variable directly.
__________________
All the buzzt!
CornedBee

"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
CornedBee is offline   Reply With Quote
Old 07-25-2009, 06:32 AM   #15
(?<!re)tired
 
Mario F.'s Avatar
 
Join Date: May 2006
Location: Portugal
Posts: 5,220
Not much, no. It's just me exercising.

Guess the only difference over going all the way and exposing the member is that you still retain some functional abstraction. The member is still private and any attempts at changing it have to go through the setter function.

Of course, it's much better to just define the class as everyone expects it and during its use, if there is a need for faster read access to the data member, the class user just copies it to the stack.
__________________
Originally Posted by brewbuck:
Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.


Mario F. is online now   Reply With Quote
Reply

Tags
class, pointer, private variables

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Getting an error with OpenGL: collect2: ld returned 1 exit status Lorgon Jortle C++ Programming 6 05-08-2009 08:18 PM
Accessing private Data members Emeighty C++ Programming 17 08-14-2008 11:16 PM
Can't Access the private member from base class planet_abhi C# Programming 3 01-09-2006 04:30 AM
Dikumud maxorator C++ Programming 1 10-01-2005 06:39 AM
can't insert data into my B-Tree class structure daluu C++ Programming 0 12-05-2002 06:03 PM


All times are GMT -6. The time now is 10:19 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22