Solved

About Global Var

Posted on 2000-04-13
3
207 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Snap images to the bottom of a div 5 67
How to convert MFC::CString to UTF8 wchar_t* 10 278
Import data into excel from web page 10 101
.php tree directory? 5 83
Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
It's sometimes a bit tricky to use date functions in Oracle BPEL. I'll explain quickly how you can add N days to the current date. In a BPEL process this can be useful, and you can adapt it to fit your needs. First of all, let's see how to add 1 …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

861 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