Solved

About Global Var

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
pre4 challenge 19 110
Magento Indexing 1 112
"Black Box" Testing of Control System Software 2 89
Windows shortcut location resolution on double click open 4 47
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…
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
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…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

713 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