help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: cannot initialize static pointer class member in __attribute__ ((co


From: Paul Pluzhnikov
Subject: Re: cannot initialize static pointer class member in __attribute__ ((constructor))
Date: Mon, 06 Oct 2008 21:29:16 -0700
User-agent: Gnus/5.1006 (Gnus v5.10.6) XEmacs/21.4 (Jumbo Shrimp, linux)

jeremy  barrett <jeremyb@gmail.com> writes:

> Consider a FilePath class that has a static char* rootDir.  By
> default, you'd expect such a class to have rootDir set to "/" (or "c:\
> \" or similar).  A particular library may use this class, but want
> never to go outside the directory "/usr/local/example", or perhaps to
> be able to easily access said directory without requiring users to
> type the full path each time.  Some other library, for some other
> system may have a similar requirement for the directory "/opt/example"
> or "/opt/extra/question_mark".
>
> Because different use cases (installations) could have need of a
> different base path, I find it convenient to set that rootDir at load
> time, rather than altering the FilePath class and rebuilding it for
> each case.  I'm avoiding rebuilding by setting this path in the
> 'constructor function' (DllMain in windows) after reading a value from
> memory some where (registry, environment variable, ...).

I am still not following. AFAIU, what you have is this:

 const char *FilePath::rootDir = "c:\\";   // [1]

 DllMain()
 {
    FilePath::rootDir = GetRootValueFromRegistry();
    ...
 }

But why can't you write the exact same thing this way:

 const char *FilePath::rootDir = GetRootValueFromRegistry();

> To be clear, this is an actual scenario where I use this feature in
> windows, not some contrivance (as we've been working with before), so
> if there are design suggestions about how to solve this situation, I'd
> be open to those as well.

You *will* hit a major difference between Win32 and UNIX default
linkage model as soon as you get two libraries which don't agree
on the meaning of 'rootDir'.

Besides, changeable globals like this are generally considered Bad
Design (TM). Consider making 'rootDir' an instance of FilePath,
instead of being a static variable. For one thing, it nicely solves
the multiple library collision problem:


// libFoo.h
namespace LibFoo {
   FilePath rootDir = GetRootDirFromSomeWhere();
   FilePath foo(const char *p) {
      return rootDir + p;
   }
}

// libBar.h
namespace LibBar {
   FilePath rootDir = GetRootDirFromElseWhere();
   FilePath bar(const char *p) {
      return rootDir + p;
   }
}

Cheers,

[1] Contrary to popular belief, C:\ is *not* a proper root path
on Win32. It's surprising just how many programs break when the
system is installed on drive F:\ instead.
-- 
In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]