LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-24-2004, 06:42 PM   #1
Ephracis
Senior Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Ubuntu, Debian
Posts: 1,109

Rep: Reputation: 50
External variables


I am doing a little C++ program here.

I have one sourcefile with main in it that declares a class-object. Now after doing some stuff the main-function calls to another function that is defined in another document.

I now want the second function to get access to the class-object. I have tried using the word "extern" but it seems like I have to define it outside of any function then, but this class-object must be defined within the main-function and I have several more files after this, all should be able to access the class-object so sending it as argument or such would not be as easy as just making it external and aviliable for all other files.
 
Old 12-24-2004, 10:20 PM   #2
btmiller
Senior Member
 
Registered: May 2004
Location: In the DC 'burbs
Distribution: Arch, Scientific Linux, Debian, Ubuntu
Posts: 4,290

Rep: Reputation: 378Reputation: 378Reputation: 378Reputation: 378
Put the class definition into a header file and include it in all the files that need to know the class definition.
 
Old 12-24-2004, 10:23 PM   #3
Ephracis
Senior Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Ubuntu, Debian
Posts: 1,109

Original Poster
Rep: Reputation: 50
Quote:
Originally posted by btmiller
Put the class definition into a header file and include it in all the files that need to know the class definition.
Yeah that was not the problem. The files knew about the class but the object created in main was not external. I could not figure out any other way to do it but create the object outside of main and move all code from the constructor into a init-function. It worked, though.
 
Old 12-25-2004, 12:00 AM   #4
btmiller
Senior Member
 
Registered: May 2004
Location: In the DC 'burbs
Distribution: Arch, Scientific Linux, Debian, Ubuntu
Posts: 4,290

Rep: Reputation: 378Reputation: 378Reputation: 378Reputation: 378
Sorry, I misread your question. IIRC there's no way around your situation -- extern variables must be declared at file scope in the file that contains their real definition. This sort of makes sense, given that these variables might go out of scope otherwise. Can you simply pass the object (or a reference to it) to all the functions that need to use it? That would be the best way, IMO and avoid the excessive use of globals.

I mostly use C, though, so I'm not aware if there are any special C++ features that would modify this answer (though I tinkered around with a test program a little). I'm also not 100% certain of the scoping rules (bbeen some time since I had to deal with them), so someone please correct me if I've got this wrong (I'm curious myself now), but the answer I gave above is how I remember it.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
variables jpostma Linux - General 4 05-07-2004 12:26 PM
Passing external variables/arguments to SQL program ganninu Programming 12 11-25-2003 05:30 AM
Shel scripting: variables pointing to variables and case Dark_Helmet Programming 5 06-08-2003 11:07 AM
variables granny Linux - Newbie 2 02-12-2003 04:36 PM
Variables hubabuba Linux - Software 1 05-17-2002 11:48 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:24 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration