LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 07-30-2006, 09:34 AM   #1
random0
LQ Newbie
 
Registered: Jun 2006
Location: Gainesville, Fl, USA
Distribution: Debian GNU/Linux
Posts: 18

Rep: Reputation: 0
Static Member Data


I'm writing a shared library in C++ in which one of my classes contains a declaration of a static object of another class.

Code:
class class1
{
};

class class2
{
    private:
        static class1 object1;
};

class1 class2::object1;
g++ compiles this and the test program without problem and it links as well, but when I run my test program it seg faults on initialization. Any idea on what's going on?

Last edited by random0; 07-30-2006 at 02:47 PM.
 
Old 07-30-2006, 10:00 AM   #2
vharishankar
Senior Member
 
Registered: Dec 2003
Distribution: Debian
Posts: 3,178
Blog Entries: 4

Rep: Reputation: 138Reputation: 138
I have an idea you cannot create static objects of classes. It's because of something to do with constructors/destructors and the like and the way they're called. Somebody correct me if I'm wrong...

P.S. That code doesn't even link because it returns an undefined reference to that object. You're trying to access something which doesn't even exist in memory.

Can you try and give the portion where you're actually using this? I am unable to recreate the scenario since I cannot even get the code to compile.

Last edited by vharishankar; 07-30-2006 at 10:09 AM.
 
Old 07-30-2006, 10:38 AM   #3
Bruce Hill
HCL Maintainer
 
Registered: Jun 2003
Location: McCalla, AL, USA
Distribution: Arch, Gentoo
Posts: 6,940

Rep: Reputation: 129Reputation: 129
Also put your code in code tags, so we don't
see smiley faces, etc.
 
Old 07-30-2006, 10:46 AM   #4
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Another minor thing is the missing semi-colons after the class' closing brace, e.g.

Code:
class class1
{
  // Stuff
};
 
Old 07-30-2006, 11:10 AM   #5
random0
LQ Newbie
 
Registered: Jun 2006
Location: Gainesville, Fl, USA
Distribution: Debian GNU/Linux
Posts: 18

Original Poster
Rep: Reputation: 0
Sorry about that lack of code tags. I ended up finding the error after a little gdb work and it was actually an unrelated bit of code that caused the error with class2. It's working properly now.
 
Old 07-30-2006, 11:33 AM   #6
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Cool - glad you got it working.

And yes, of course you can create a static class object, if you need to! Or (as you did) a class that contains a static class object as a member.
 
Old 07-30-2006, 11:37 AM   #7
vharishankar
Senior Member
 
Registered: Dec 2003
Distribution: Debian
Posts: 3,178
Blog Entries: 4

Rep: Reputation: 138Reputation: 138
Will it not lead to unpredictable program behaviour if static class objects are nested within other classes?
 
Old 07-30-2006, 04:40 PM   #8
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Harishankar -

If you try hard enough, you can probably find a way to abuse just about any feature of any language. Heck - you probably wouldn't have too much difficulty finding at least a half dozen (or more!) ways to "mess up" adding two integer numbers in C++!

Nevertheless you can definitely have static class objects, whenever they're appropriate to your design.

And from what random0 posted, both his declaration and definition of "object1" seem perfectly OK.

IMHO .. PSM

PS:
Note the need for a default (no-argument) constructor to handle any initialization for static class objects.
 
Old 07-30-2006, 05:09 PM   #9
spooon
Senior Member
 
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 51
Quote:
Originally Posted by paulsm4
PS:
Note the need for a default (no-argument) constructor to handle any initialization for static class objects.
Well, you just need a constructor to handle whatever initializer you used in your definition. Since here they gave no arguments in the initializer, they need a default constructor.
 
  


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
Website member login and data retrieval lord-fu Programming 3 03-13-2006 02:27 PM
static member and non-static member allomeen Programming 9 01-06-2006 05:31 PM
shared libraries, dlopen and static member variables? (C/C++) Thinking Programming 2 12-19-2005 12:55 AM
invalid data member initialization ...error..plz help gogettergary Programming 1 05-20-2005 07:16 AM
LQ member progress status from member to senior member............. emailssent LQ Suggestions & Feedback 3 10-11-2004 01:31 PM

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

All times are GMT -5. The time now is 08:18 PM.

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