![]() |
| | #1 |
| Registered User Join Date: Jul 2009
Posts: 24
| accessing private class data low overhead 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 | |
| | #2 | |
| (?<!re)tired Join Date: May 2006 Location: Portugal
Posts: 5,220
| Quote:
![]() 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 | |
| | #3 | |
| Registered User Join Date: Jul 2009
Posts: 24
| Quote:
| |
| lawrenced is offline | |
| | #4 |
| C++ Witch 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 | |
| | #5 |
| Senior software engineer 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 | |
| | #6 |
| (?<!re)tired 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 | |
| | #7 | |
| Senior software engineer Join Date: Mar 2007 Location: Portland, OR
Posts: 5,381
| Quote:
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 | |
| | #8 |
| (?<!re)tired 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 | |
| | #9 | ||
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| Quote:
__________________ 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:
| ||
| Elysia is offline | |
| | #10 |
| Cat without Hat 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 | |
| | #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:
| |
| Elysia is offline | |
| | #12 |
| Senior software engineer Join Date: Mar 2007 Location: Portland, OR
Posts: 5,381
| 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 | |
| | #13 |
| (?<!re)tired 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 | |
| | #14 |
| Cat without Hat 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 | |
| | #15 |
| (?<!re)tired 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 | |
![]() |
| Tags |
| class, pointer, private variables |
| Thread Tools | |
| Display Modes | |
|
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 |