Solved

About Global Var

Posted on 2000-04-13
3
199 Views
Last Modified: 2013-11-20
I define a class cpp file like:
.......
......
Cclass::function(){

};
Cclass theClass;
......

and theClass is global variable;

but when i define

"Cclass *ptheClass;"
and then use  
ptheClass = new Cclass ;"
in another cpp file the ptheClass can't be use

why?


0
Comment
Question by:lxtsy
  • 2
3 Comments
 
LVL 8

Expert Comment

by:VinExpert
ID: 2714697
Hi,

Say U have that class definition in ccc.h file, If U want to access that in ccc.cpp then
include ccc.h into it and then use.

Now as u have declared it as a global in
ccc.cpp and u want to use that in xxx.cpp, then in xxx.cpp u have to include ccc.h in xxx.cpp and then u can use it.

Try it out.
VinExpert
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 2714936
>I define a class cpp file like:
>Cclass::function(){
>};
>Cclass theClass;
>and theClass is global variable;

yes .. but if it is only defined in your class cpp file, then other files won't know about it.  it needs to also be declared in a .h file that the other cpp files #include

>but when i define
>Cclass *ptheClass;
>and then use  
>ptheClass = new Cclass ;"
>in another cpp file the ptheClass
>can't be use  

same problem.  it needs to be declared in some h file.

In addition, the line Cclass *ptheClass only DECALRED the pointer, it doesn't define it, and you need to define things exactly once in C/C++.  To do this, give in an initial value in you class cpp file eg.
  Cclass *ptheClass = NULL;
but in your .h file, just decalre it with:
  Cclass *ptheClass;

then all should be right with the world.

PS: as a matter of style, always prefix global variables with '::', even though it is not strictly necessary.  It makes it more obvious that they are global, and stops any clashes with local vars of the same name.

0
 
LVL 10

Accepted Solution

by:
RONSLOW earned 25 total points
ID: 2721244
Same as above .. please comment if you reject and answer.
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
modThree challenge 4 90
Unix Command -- Challenging  question 7 90
Problem to echo 6 97
Where do I learn ES6? 3 68
I will show you how to create a ASP.NET Captcha control without using any HTTP HANDELRS or what so ever. you can easily plug it into your web pages. For Example a = 2 + 3 (where 2 and 3 are 2 random numbers) Session("Answer") = 5 then we…
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question