Click here to Skip to main content
16,005,734 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Obtain a value from a variable in a class method. [modified] Pin
e40s8-Nov-07 5:58
e40s8-Nov-07 5:58 
GeneralRe: Obtain a value from a variable in a class method. Pin
toxcct8-Nov-07 6:23
toxcct8-Nov-07 6:23 
GeneralRe: Obtain a value from a variable in a class method. Pin
e40s8-Nov-07 6:37
e40s8-Nov-07 6:37 
GeneralRe: Obtain a value from a variable in a class method. Pin
toxcct8-Nov-07 6:45
toxcct8-Nov-07 6:45 
GeneralRe: Obtain a value from a variable in a class method. Pin
e40s8-Nov-07 7:00
e40s8-Nov-07 7:00 
GeneralRe: Obtain a value from a variable in a class method. Pin
BadKarma8-Nov-07 5:13
BadKarma8-Nov-07 5:13 
GeneralRe: Obtain a value from a variable in a class method. Pin
e40s8-Nov-07 5:26
e40s8-Nov-07 5:26 
GeneralRe: Obtain a value from a variable in a class method. Pin
David Crow8-Nov-07 8:43
David Crow8-Nov-07 8:43 
At this point, your confusion seems to be less about C++ and classes and more about high-level programming languages in general. For example:

void foo ( void )
{
    x = 1;
}
 
void main( void )
{
    int x;
}
In this example, the variable x is an automatic variable that is local to the main() function. It cannot be accessed by the foo() function. To remedy that, you'd need to give x global scope like:

int x;
 
void foo ( void )
{
    x = 1;
}
 
void main( void )
{
    x = 5;
}
Now foo() and main() both have access to the global variable x.

Fast forward to C++ and classes results in much the same. Consider:

class myClass
{
    void foo( void )
    {
        int x;
    }
};
In this example, the variable x is an automatic variable that is local to the foo() method. It cannot be accessed by any other member of the class. To remedy that, you'd need to give x class-level scope like:

class myClass
{
private:
    int x;
 
    void foo( void )
    {
        x = 9;
    }
};
In this example, the variable x is accessible by all methods of the class. Now if you wanted x to be made accessible to users of the class, then you'd have to put it in the public section like:

class myClass
{
public:
    int x;
 
    void foo( void )
    {
        x = 9;
    }
};
Any clearer? There's much more to it (e.g., multiple instances sharing the same data), but I think this is enough to get you going.


"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman

"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne


GeneralRe: Obtain a value from a variable in a class method. [modified] Pin
e40s8-Nov-07 9:05
e40s8-Nov-07 9:05 
Questionc++ path management functions Pin
Capitanevs7-Nov-07 10:15
Capitanevs7-Nov-07 10:15 
AnswerRe: c++ path management functions Pin
Mark Salsbery7-Nov-07 10:44
Mark Salsbery7-Nov-07 10:44 
GeneralRe: c++ path management functions Pin
Peter Weyzen7-Nov-07 15:33
Peter Weyzen7-Nov-07 15:33 
GeneralRe: c++ path management functions Pin
Roger Broomfield7-Nov-07 15:52
Roger Broomfield7-Nov-07 15:52 
QuestionHow can i open some Form application as child of other Form application ? Pin
Yanshof7-Nov-07 10:05
Yanshof7-Nov-07 10:05 
AnswerRe: How can i open some Form application as child of other Form application ? Pin
Hamid_RT7-Nov-07 17:36
Hamid_RT7-Nov-07 17:36 
GeneralRe: How can i open some Form application as child of other Form application ? Pin
Yanshof7-Nov-07 19:20
Yanshof7-Nov-07 19:20 
AnswerRe: How can i open some Form application as child of other Form application ? Pin
Nelek7-Nov-07 21:20
protectorNelek7-Nov-07 21:20 
GeneralRe: How can i open some Form application as child of other Form application ? Pin
Hamid_RT8-Nov-07 2:46
Hamid_RT8-Nov-07 2:46 
QuestionProject snapshot Pin
Larrson127-Nov-07 9:34
Larrson127-Nov-07 9:34 
AnswerRe: Project snapshot Pin
Maximilien7-Nov-07 9:52
Maximilien7-Nov-07 9:52 
AnswerRe: Project snapshot Pin
Peter Weyzen7-Nov-07 15:40
Peter Weyzen7-Nov-07 15:40 
QuestionURGENT : strange error C4430 VS2005 with ostream Pin
AhmedOsamaMoh7-Nov-07 8:08
AhmedOsamaMoh7-Nov-07 8:08 
AnswerRe: URGENT : strange error C4430 VS2005 with ostream Pin
Mark Salsbery7-Nov-07 8:18
Mark Salsbery7-Nov-07 8:18 
JokeRe: URGENT : strange error C4430 VS2005 with ostream Pin
AhmedOsamaMoh7-Nov-07 8:22
AhmedOsamaMoh7-Nov-07 8:22 
GeneralRe: URGENT : strange error C4430 VS2005 with ostream Pin
led mike7-Nov-07 8:44
led mike7-Nov-07 8:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.