Thread: simple class, any problems?

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    203

    simple class, any problems?

    This is a class i created to get some experience with classes and linked lists. I think i got all the bugs out, but i'm not sure. Are there any problems i didn't see? When assigning a null pointer to next or prev I didn't allow it to autoset(look and you should understand) just because *shrug*, so that isn't a bug. Please feel free to comment on my style and usage.

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    203
    going over it again i realized 2 problems that I fixed below
    Code:
    TestClass::TestClass(const TestClass& x)
    {
    	prev = x.prev;
    	next = x.next;
    	id = x.id;
    	if(x.name)
    	{
    		name = new char[strlen(x.name)+1];
    		strcpy(name, x.name);
    	}
    	else
    		name = NULL;
    }
    
    TestClass TestClass::operator=(TestClass x)
    {
    	prev = x.prev;
    	next = x.next;
    	id = x.id;
    	if(name)
    		delete name;
    	if(x.name)
    	{
    		name = new char[strlen(x.name)+1];
    		strcpy(name, x.name);
    	}
    	else
    		name = NULL;
    	return *this;
    }

  3. #3
    Unregistered
    Guest
    classes shouldn't be used for linked lists

  4. #4
    endo
    Guest
    Originally posted by Unregistered
    classes shouldn't be used for linked lists
    Why not???

  5. #5
    Registered User biosx's Avatar
    Join Date
    Aug 2001
    Posts
    230
    I don't see why he says classes shouldn't be used. They are quite handy. You just have to know what you're doing.

    I didn't look at your code, but I will now

  6. #6
    Registered User biosx's Avatar
    Join Date
    Aug 2001
    Posts
    230
    Do you really need 10 constructors? I don't think so.

    Also, for your == operator.. I would make it return bool and have it work on a const reference:

    Code:
    bool TestClass::operator==(const TestClass &x) const
    {
       if( (!strcmp(name, x.name)) && 
           ( id == x.id )          && 
           ( prev == x.prev )      && 
           ( next == x.next ) )
          return true;
       else
          return false;
    }
    Hope that helps

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    203
    thanks, changed the == to return bool
    as for the constructors, i tried to overload it for all possible ways someone might want to initialize the class. If it were a real class then i would have only defined what the class would need and leave the rest to accessors.
    (hey i missed int, char* ..better run and add it! HAHA 11 constructors!)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 10-31-2005, 12:05 PM
  2. Replies: 8
    Last Post: 10-02-2005, 12:27 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Simple class problem
    By savageag in forum C++ Programming
    Replies: 1
    Last Post: 10-04-2003, 10:50 AM
  5. Problems with functions in a class with pointer to self
    By BigDaddyDrew in forum C++ Programming
    Replies: 6
    Last Post: 02-03-2003, 11:24 PM