library, qsort, class, static, member access question
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: library, qsort, class, static, member access question

  1. #1
    Join Date
    May 2002
    Location
    Germany
    Posts
    441

    library, qsort, class, static, member access question

    Hi There,

    So I have something here what I try to resolve but could not so far.

    I have something like:
    Code:
    class CLib {
    ...
      static SortFunc(const void *, const void *);
    ...
    <lots of variables>
    ...
      static CLib *pSelf;
    };
    
    CLib::CLib {
      pSelf = this;
    ...
    }
    
    CLib::SortFunc(const void *p1, const void *p2) {
      if (p1 != 0) {
        pSelf->OneVariable = 1;
      }
    ...
    }
    When I compile and build the library it is fine, However when I link the library I get LNK2019 from Visual Studio (I intend to compile the same code on linux once it works). Okay, accessing non-static variable from a static function is not permitted and I just looked for a workaround. Do you have any clue how to make that work, either by eliminating the linking error or restructuring the code in order to access the non-static variables?

    Thanks

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,885

    Re: library, qsort, class, static, member access question

    You seem to be missing a basic understanding of what "static" means. If your class C has a non-static member "A", and you have an array of 100 Cs, then you have 100 As as well----one in each object. If C also has a static member B, then in that array (and everywhere else in the program) there is only one B, shared by all objects of the class.

    Similarly for static functions, they do not operate on any one object; hence, it doesn't make sense to refer to a member of an object directly when using them. It would be like saying, "I have a carwash that cleans cars. Since it does something to cars, it must have a steering wheel." It doesn't make sense to think that way. You can refer to the steering wheel of any car currently *in* the carwash, but the carwash itself doesn't have one.

    In a lot of ways, declaring a function of a class as static is very similar to declaring a non-class function a friend.

    Secondly, don't use qsort in this circumstance. Since you have a class you're clearly in C++ rather than C, so you should be looking to use std::sort. To do that, you'll need to define the member function:
    Code:
        bool CLib::operator<(const CLib &rhs) const
    or else a function with an equivalent signature (taking two const CLibs and returning bool).
    Last edited by Lindley; January 8th, 2009 at 04:32 PM.

  3. #3
    Join Date
    May 2002
    Location
    Germany
    Posts
    441

    Re: library, qsort, class, static, member access question

    The situation is that I try to make a huge C code chunk work in a C++ class.

    The objective here is to return the sort value based on two incoming int values (the original sorter function was declared as
    Code:
    int srt(int *p1, int* p2)
    and called from a qsort).

    I tried to make it the C++ way by inserting the operator overload and see the very same problem. The operator inside the struct inside the class cannot access non-static variables at all. Well, the incoming values to be sorted are integers... Is there a chance to bypass duplication of the class?

  4. #4
    Join Date
    May 2002
    Location
    Germany
    Posts
    441

    Re: library, qsort, class, static, member access question

    Just to make it better, the data sorted is actually an array of integers of variable size. Hmm.

  5. #5
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,885

    Re: library, qsort, class, static, member access question

    Code:
    CLib::SortFunc(const void *p1, const void *p2) {
      if (p1 != 0) {
        pSelf->OneVariable = 1;
      }
    ...
    }
    What I don't understand is, why are you trying to modify anything in a comparison function?

    If you want to sort a list of CLib objects, then the sort function will take either two CLib references (for std::sort) or two pointers to CLibs (for qsort). You can access any member you want of the passed objects; but modifying them is typically out, since I think the sort function requires const arguments. Not certain about that in the case of std::sort.

  6. #6
    Join Date
    May 2002
    Location
    Germany
    Posts
    441

    Re: library, qsort, class, static, member access question

    The truth is that I do not really understand what is going on in that sort function. It is a mathematical modeling code made for twenty-something years and designed by scientists (lots of gotos for example). It is more or less impossible to follow what is going on (I am just a sw guy). There are several files, externs to everything, global variables only and variable names like a, b, c, d x, q, na, nb, nc, nd, etc (that was not one. ). So all what I would like to do with the lib is that it compiles in a C++ context, work just as before and instead of pure command line, it will work with a GUI, even probably multithreaded on the long run.

    As per the current sort, the actual sort function is called both for sorting and both standalone. The array elements are actually created within the sorter during the sort cycle but only if called as a sort, otherwise it just creates the elements unsorted. I am sweating blood to understand what is going on in the function but cannot. It looks that noone knows what is there, the guy who maintained that left two months before I started... So all I would like is to access those ex-global variables somehow.

  7. #7
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,885

    Re: library, qsort, class, static, member access question

    Two things.....

    1) You can just use C code "as-is" in a C++ program without modification. Just wrap the function declarations in "extern "C" {}" and you'll be able to link in the compiled C files just fine. I would suggest this over attempting to C++-ize the code if it's supremely messy, which it sounds to be.

    2) Multi-threading is going to be very dicey for any code involving global variables. You typically need to use mutexes to excess in those situations. Just a warning.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center