ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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.
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.