|
|
Wll very first thing is if you want to access private members you have to write get/set function, if you do not want to access the variables we shouldnt write. Just read "Thinking in c++" by Bruce Eckel and "Effective C++" by Scott meyer for thorough understanding and dont mind to surf the followinf site
http://cplus.about.com/library/blcplustut.htm?PM=ss12_cplus
regards
Balkrishna Talele
"Every question has good meaning"
|
|
|
|
|
Thanks a ton, I will check those out and read that site.
-x
|
|
|
|
|
Generally i don't use/see a get/set for each member, let alone make them all private/protected.
Iff a member state is 'linked' to another member state then they will both be private or protected and a get/set will be created for each in order to enforce the state relationship.
But having this :
class Foo {
private:
int x;
public:
int GetX ( void ) const { return(x); }
bool SetX ( int X ) { x = X; return(true); }
);
Adds nothing, and make things more complex than need be (in my opinion).
We are thinking developers, not blind pattern slaves ... or so the theory goes.
...cmk
Save the whales - collect the whole set
|
|
|
|
|
I don't think this is smart idea to make ALL members private and make get/set methods for them. It will make classes and their usage too big and inefficient. But this depends on class type.
Even in MFC you'll find some classes with public members, like CRect (top, left...), CWnd (m_hwnd), ....
rrrado
|
|
|
|
|
rrrado wrote:
It will make classes and their usage too big and inefficient.
You haven't measured this assertion. Firstly, the use of a get or set accessor method, if all it does is set or get the private variable, amounts to a very few instructions - very few more than would have been used if the variable was set directly. If the accessor method is marked inline and the optimiser decides to in-line it, the actual code generated is typically identical to setting the variable directly.
If not inline, the runtime cost is typically negligible - the processor can predict that the branch is always taken and fill the instruction cache directly. There's a small cost in code size which could potentially lead to more paging activity, but this is likely to be minor. The overall cost of your data structures typically dwarfs the cost of minor issues such as this. If your code profiler tool indicates that this is causing a problem, by all means remove the accessors. But not until then.
In terms of data size, there is no difference. If you make the accessors virtual , you will increase the size of the virtual function table (and if these are the only virtual members, the size of the object by one pointer). Simple accessors are very rarely made virtual .
It's far easier to change the code to bypass accessor functions, i.e. make a previously-private field public , later on than it is to introduce accessors. It's better to keep your data private to keep your clients' hands off it - to prevent accidental alteration and to preserve your contract - a concept known as information hiding. If you wanted to calculate a data value rather than to store a cached version, and you've used accessors, you can simply change the function body; you'll need to do a lot more work if you made it a public member.
The MFC classes you mention are simple wrappers which tend not to have problems with synchronising the values of different members of the class. CRect 's whole identity is based on the four data members top , left , bottom and right . CWnd already has abstraction behind the m_hwnd member - it's a handle to the underlying window object, which Windows never allows direct access to.
Stability. What an interesting concept. -- Chris Maunder
|
|
|
|
|
hey I am trying to build a DLL in pure C/C++ !!
I want to share the variable values accross multiple processes.
I am a novice and want to learn how to do it..
please help me..
thanx in advance..
Pritpal
Pritpal
< coder in evolution >
|
|
|
|
|
Well, Whenever you build a dll and if any exe is loading your dll, it will run in its own process,so no question arises that ur variables wil be shared. If you make a SINGLETON, you must build an ATL Server exe(which will be out process) and then you can share variable. But as you say you are novice, it will be little difficult to write the singleton in ATL.
regards
Balkrishna Talele
|
|
|
|
|
No
It is possible to share data/variables among processes in regular DLLs. We can use #pragma data_seg("SHARED") to declare shared data segments. A very good article is there in CP - http://www.codeproject.com/dll/data_seg_share.asp
rgds ...mil10
|
|
|
|
|
I'd recommend looking into memory-mapped files. The shared-segment previously mentioned doesn't work if the dll can't be mapped into the correct address space due to dll relocation. In other words, if a dll is already mapped to the address it needs to be shared, then it doesn't work right.
I believe there are articles that describe how memory-mapped files work on this site. If not, I can give you an example.
--
Joel Lucsy
|
|
|
|
|
Howdy,
I have a dialog with a CStatic positioned on it. The top left point is at the right placement. How would I maximise the CStatic so that it fits neatly on the screen width
and height (to bottom of screen) but not changing the top left point? Can't quite get it right.
Thanks,
|
|
|
|
|
Get the windowpointer of the CStatic control and use SetWindowPos to resize it in OnSize Eventof the dialog.
|
|
|
|
|
m_pView is defined in CMyDialog dirived from CDialog ,
private CView*m_pView;
in modeless dialog, for example CMyDialog::CMyDialog(CView*pView)
{
m_pView=pView;
}
i think using CView as the argument to allow CMyDialog to use with any other CView Class,how about you?
|
|
|
|
|
Most of the time we get such stuff null, onr foolproof method is to have the variable static
OR
2) The pointer is pointing to what , when the object is getting constructed, it will assign the default values which are in CViews constructor... You need to write copy constructor
regards
Balkrishna Talele
|
|
|
|
|
Hi,
How do I measure the height of the text portion of an item in a CListCtrl problematically?
Thanks,
|
|
|
|
|
Consider that you have a list control. This control has items placed into it. Each item has an image portion and a label portion, and these two together form the item's boundaries.
Now, you can get the rectangle of either the image, the label, or the entire boundary by using CListCtrl::GetItemRect and specifying one of the flags. If the label already has text, then you must query the list control for the font it uses (the system font, by default), then calculate the height of the text based on the font.
I am not sure if there is a direct method, but as CListCtrl is derived from CWnd , it should support GetFont method call. The best way, of course, would be to query a CWnd -pointer into the individual item, then query for the font. But how to get a pointer to the subitem's CWnd is something I have no clue of, as list control items are handled by the LVITEM structure. I'd first try using the GetFont call of the list control itself.
By getting the label rectangle, you can measure the maximum height of the text. This allows you to select a font size and typeface that fits smoothly into the area. SetFont , I think, allows you to select the font used. Again, experiment to be sure.
-Antti Keskinen
----------------------------------------------
The definition of impossible is strictly dependant
on what we think is possible.
|
|
|
|
|
Hiya I am trying to learn about serial comms. I have been trying to learn from an article on Code Project. But there seems to be alot to it.
What I am trying to do is send a file from one pc to other.
Has anyone done this before and could u please give me an example of doing this.
Thank you for any help.
|
|
|
|
|
that's not the correct place to ask this, except if you want to do it by yourself in Visual C...?!
TOXCCT >>> GEII power
|
|
|
|
|
Hey all.
I have an application, and I used the article by ashman to allow me to minimize to the system tray. Almost everything is working excellent, except I've noticed one flaw.
When I minimize the program to the taskbar, there are two ways to restore it. One way would be to double click, the other way would be to right click on the icon, and click restore. Right clicking and selecting restore works perfect, however, the double click does not.
I have checked the code (and put an AfxMessageBox() in there to make sure it was getting called correctly), and both the right click event as well as the "click on restore button" event are calling the same method to restore the window. When I double click on the icon, the window title appears on the taskbar (as it should) but it does not restore and show it. It just puts it on the taskbar, and the only way to view the window is to actually click on another program that's running in the taskbar, and then click on my program on the taskbar afterwards ... then, it appears as it should.
I can not figure out why since it is calling the same exact method it is behaving differently. What I have in the restore code is this:
ShowWindow(SW_RESTORE);
m_bHidden = FALSE; // <- this is just a variable that tells the program if its hidden or not
I've tried a plethora of ideas to make it work by putting different ShowWindow(SW_SHOWNORMAL), SW_SHOWDEFAULT, etc.... The only way that it seems to work (but only some of the time) is if I do ShowWindow(SW_SHOWMAXIMIZED); ... Then half of the time it pops up full screen and works great. The only problem here is that I have developed the app for a specific size and I do not allow the user to maximize, neither have I added any code to allow for this, so when I call ShowWindow(SW_SHOWMAXIMIZED) it looks very ugly.
Oh, and here's some other information. After I do this the first time (I restore the window, click on another program in the taskbar, then click on my program and regain view/focus), it works fine every time after that with the double click function. It's only the first time that the user tries to restore that I am running into the problems. Also, if I do:
ShowWindow(SW_SHOWMAXIMIZED);
Sleep(1000);
ShowWindow(SW_SHOWNORMAL);
Every time it pops up just fine (but obvisously there is a 1 second delay in there which stinks)
I am wondering how I can "un hide" a window, and set focus on it and ensure this happens every time. Any help would be appreciated.
|
|
|
|
|
|
Given the files below I am getting multiple errors . I think I need a point.cpp and a main driver, having a hard time tying it all together
is there a way to upload files instead of pasting all the code in 6+ files? soemone let me know please
Thanks
Thanks
|
|
|
|
|
Given the files below I am getting multiple errors . I think I need a point.cpp and a main driver, having a hard time tying it all together
is there a way to upload files instead of pasting all the code in 6+ files? soemone let me know please
Thanks
|
|
|
|
|
Yes
Sonork 100.11743 Chicken Little
"You're obviously a superstar." - Christian Graus about me - 12 Feb '03
Within you lies the power for good - Use it!
|
|
|
|
|
|
carefully
Sonork 100.11743 Chicken Little
"You're obviously a superstar." - Christian Graus about me - 12 Feb '03
Within you lies the power for good - Use it!
|
|
|
|