Results 1 to 5 of 5

Thread: Class Problem

  1. #1

    Thread Starter
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024

    Class Problem

    I am not sure how to ask this but here it goes; Lets say I have a class:

    Code:
    class FILE
    {
      public:
        std::string sName;
    };
    Then I use it:
    Code:
    FILE f1;
    FILE f2;
    f1.sName = "Test.txt";
    f2.sName = "Test2.txt";
    What I want is to check and make sure this doesnt happen. I want to ensure that each class has a unique sName. So how can I check or ensure this doesnt happen?
    MSVS 6, .NET & .NET 2003 Pro
    I HATE MSDN with .NET & .NET 2003!!!

    Check out my sites:
    http://www.filthyhands.com
    http://www.techno-coding.com


  2. #2
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403
    here's one way
    (general outline)

    Code:
    class FILE
    {
         private:
           static vector<FILE&> myFILES;
           string sName;
    
         public:
           FILE()
           {
                myFILES.push_back(*this);       
           };
           ~FILE()
           {
                // remove (*this) from myFILES
           };
    
            bool SetName(const string &s)
            {
                  if ( a FILE exists in the vector with the name s)
                     return false;
                  
                  sName = s;
                  return true;     
            }
    };
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

  3. #3

    Thread Starter
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024
    Thanks Sunburnt

    I had thought of that I just was wondering if there is an easier and less memory intensive then doing it that way. Seems like there should be a way to access the other classess instances.

    Worse case I might just do it that way.
    MSVS 6, .NET & .NET 2003 Pro
    I HATE MSDN with .NET & .NET 2003!!!

    Check out my sites:
    http://www.filthyhands.com
    http://www.techno-coding.com


  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    There is none, not really. It wouldn't make sense: it would cost large amounts of memory (because the compiler would have to do what sunburnt is doing) and be hardly used.

    A way to save some time is to make every class directly a linked list node:
    Code:
    class FILE
    {
         private:
           static FILE *pFirst; // Initialize to 0
           FILE *pNext, pPrev;
    
           string sName;
    
         public:
           FILE()
           {
                  if(pFirst) {
                         pFirst->pPrev = this;
                         pNext = pFirst;
                  } else {
                         pNext = 0;
                  }
                  pPrev = 0;
                  pFirst = this;
           };
           ~FILE()
           {
                  if(pNext) {
                         pNext->pPrev = pPrev;
                  }
                  if(pPrev) {
                         pPrev->pNext = pNext;
                  } else {
                         pFirst = pNext;
                  }
           };
    
            bool SetName(const string &s)
            {
                  if ( a FILE exists in the list with the name s)
                     return false;
                  
                  sName = s;
                  return true;     
            }
    };
    You can trade construction speed for SetName speed by using a balanced search tree (e.g. std::set) instead of a list. You'd have to write your own sorting predicate though.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5

    Thread Starter
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024
    Yeah to much work for what I am doing. I just figured if there was a way to safely and maybe quickly do it, I would put it in. Thanks everyone.

    POST - 900

    PS - Thanks Sunburnt for your MP3 script link, its much better than the other one.
    Last edited by Technocrat; Oct 8th, 2003 at 10:39 AM.
    MSVS 6, .NET & .NET 2003 Pro
    I HATE MSDN with .NET & .NET 2003!!!

    Check out my sites:
    http://www.filthyhands.com
    http://www.techno-coding.com


Posting Permissions

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



Click Here to Expand Forum to Full Width