|
Kind of - if a constructor sets the values, you can use it to set them. But, once they are set, you cannot access them, unless public methods are offered to do so ( often a class will offer methods to get, but not set some of the values )
Christian Graus - Microsoft MVP - C++
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
Meaning you the drived Class can access the private members only once durning initilization I guess to put total control on a data members can
add const to private ?????
|
|
|
|
|
No, private members can't be accessed even at initialization. The constructor you are refering may be public or protected. If your constructor is private then that constructor cannot be accessed from outside the class.
|
|
|
|
|
Rajkumar_R wrote: private members can't be accessed even at initialization.
Rajkumar_R wrote: The constructor you are refering may be public or protected.
class myCls<br />
{<br />
private:<br />
int x,y;<br />
public:<br />
myCls()<br />
{<br />
x = 5100;
y = 5660;
}<br />
void disp()<br />
{<br />
printf("x = %d;y = %d",x,y);<br />
}<br />
};<br />
void main()<br />
{<br />
class myCls obj;<br />
obj.disp();<br />
}
-- modified at 10:46 Thursday 5th July, 2007
Nice talking to you. If you judge people, you have no time to love them. -- Mother Teresa
|
|
|
|
|
G Haranadh wrote: please give a conclusion for private data members.
What exactly are you wanting to do with them?
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
Still, here constructor is accessed in main() because it is public.
constructor in turn accessed the private member. private member is not accessed in the main.
|
|
|
|
|
If I write my own constructer for the Base Class what Can I do/access/modify with it
thankx
|
|
|
|
|
If a base class has private members, a derived class (or any other for that matter) can access them only by using the friend keyword. Use it with caution, though (overuse of friend is a sign of a bad class designer.)
Anyone who thinks he has a better idea of what's good for people than people do is a swine.
- P.J. O'Rourke
|
|
|
|
|
Well normally if you write your own constructer in a derived class what can you do in it
|
|
|
|
|
The access rules apply to all members of a class. Your class should be designed so that the base class constructors and members would Get/Set the private member data within it's direct scope.
To be more clear; any method or data member defined in a class is accessible within that class, regardless of whether it's private, protected or public. Access modifiers control the visibility of methods to derived classes.
Thus a private data member in a base class is visible to methods in that base class. It is not visible, however, to any derived classes (unless friend is used.)
Anyone who thinks he has a better idea of what's good for people than people do is a swine.
- P.J. O'Rourke
|
|
|
|
|
The point I was making that to access private members of a base class all you have to do is write your own base contructer (maybe I am just thinking like a Hacker) but.... I am just trying to understand
|
|
|
|
|
Yes. You could do that, provide protected or public methods in the base class to that data or make your derived class a friend of the base class.
If this is a one time initialization and you don't want anyone but derived classes to set this data, make a protected constructor in the base class with the proper parameters and reference that in the constructor initialization list of the derived class.
Anyone who thinks he has a better idea of what's good for people than people do is a swine.
- P.J. O'Rourke
|
|
|
|
|
Do I really need to do any of that cann't I just write my own base class contructer since its a base class contructer it should be able to access anything in the base
|
|
|
|
|
yes
Anyone who thinks he has a better idea of what's good for people than people do is a swine.
- P.J. O'Rourke
|
|
|
|
|
Thankx Just helps me understand appreciate it
|
|
|
|
|
I have a text file which has data written to it.. If I open the file, how would I read the data in the file one line at a time, then do something with the data, then read the next line etc
Thanks for your help!
--PerspX
|
|
|
|
|
Look for the ifstream class.
In order to use the ifstream, I tend to #include <fstream>.
MODIFIED: damn < > symbols...
-- modified at 17:29 Wednesday 4th July, 2007
|
|
|
|
|
If u can use MFC, use ReadString() of CStdioFile. It is suitable for ur requirement.
akt
|
|
|
|
|
|
I'd do it like this:
--------------------
// Console.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <string>
#include <iostream>
#include <fstream>
int main(int arvc, char* argv[])
{
using namespace std;
ifstream ifs("C:\\Data.txt");
if (!ifs)
{
cerr << "Failed to open file!" << endl;
return 1;
}
int line_count = 0;
string line;
while (getline(ifs, line))
{
cout << "Line " << ++line_count << ": " << line << endl;
}
return 0;
}
Steve
|
|
|
|
|
Thank you
--PerspX
"Nowadays, security guys break the Mac every single day. Every single day, they come out with a total exploit, your machine can be taken over totally. I dare anybody to do that once a month on the Windows machine." - Bill Gates
|
|
|
|
|
Is there a way to set a process priority using the Win32 API? Does creating more threads increase the time slice allocated for the process for the CPU?
Thanks for your help!
--PerspX
|
|
|
|
|
You can change the process's priority class with SetPriorityClass(), or the priority for a thread with SetThreadPriority().
|
|
|
|
|
Perspx wrote: Does creating more threads increase the time slice allocated for the process for the CPU?
At the kernel level there is a linked list of executing threads for each priority level. So increasing the number of threads in your application could in theory increase the number of time slices relative to other processes of the same priority. But adding more threads does not always mean better performance as you will also be increasing the number of context switches which are expensive.
Best Wishes,
-Randor (David Delaune)
|
|
|
|
|