Solved

About Global Var

Posted on 2000-04-13
3
179 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
Comment Utility
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
Comment Utility
>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
Comment Utility
Same as above .. please comment if you reject and answer.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now