Solved

About Global Var

Posted on 2000-04-13
3
223 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Preface This article introduces an authentication and authorization system for a website.  It is understood by the author and the project contributors that there is no such thing as a "one size fits all" system.  That being said, there is a certa…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

717 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