CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 17 of 17
  1. #16
    Join Date
    Aug 2006
    Posts
    515

    Re: Is there ASCII equivalent of CStringArray?

    I end up resolving this issue by using just unicode version of CStringArray after all. I liked the CStringArrayUtils namespace approach to extend the function but I am still not sure if I would prefer this over deriving from the class for any particular reason!? Your approach gives me a good insight but at the same time is more complex even if we remove the templates. For example my difference function look like this and I like the fact it is invoked just by - operator so the main code looks clean.
    Code:
    CStringArrayEx CStringArrayEx::operator - (const CStringArrayEx & rhs)
    {
    	CStringArrayEx diffList; // difference list
    
    
    	// number of items in reference array (right hand side)
    	int rhsItemCount = rhs.GetCount();
    
    	CString strItem;
    
    	for (int i = 0; i < rhsItemCount; i++)
    	{
    		strItem = rhs.GetAt(i);
    
    		// if rhs string item is not in the source array, add it to the difference list
    		if ( !FindNoCase( strItem ) )
    			diffList.Add( strItem );
    	}
    
    	return diffList;
    }

  2. #17
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Is there ASCII equivalent of CStringArray?

    Quote Originally Posted by zspirit View Post
    I end up resolving this issue by using just unicode version of CStringArray after all. I liked the CStringArrayUtils namespace approach to extend the function but I am still not sure if I would prefer this over deriving from the class for any particular reason!?
    You can do anything in C++, I'm saying that experts will tell you that deriving just for the sake of adding attributes is not what public inheritance was designed for. See my Dog class example in my previous post.
    Your approach gives me a good insight but at the same time is more complex even if we remove the templates.
    Well what I wrote is C++ -- if it's complex to you, then maybe you need more experience in C++, as that code should be understood by an intermediate to advanced C++ programmer.

    Also, when classes are designed, complexity of the internals shouldn't be a concern to the user. It is the usage of the classes and how flexible they are that matter. Classes are made so that they are the most flexible, easy to use, and difficult to use incorrectly. To accomplish this, the internals of such classes must be complex or somewhat complex. Have you seen the MFC internals, or STL internals, or any class internals? Of course it's going to be complex internally -- that's the only way to achieve flexibility, ease of use, and cut down on user mistakes.
    For example my difference function look like this and I like the fact it is invoked just by - operator so the main code looks clean.
    You're showing this code now, but how would this have worked if you did do as originally stated, and that is to have a CStringArrayA class? You would then need to duplicate that entire code. That's the point I was attempting to make with my example.

    Also, experts will tell you that you should write a concrete, non-operator overloaded version of operator -. It isn't intuitive as to what subtraction is supposed to do when you have an array of strings. A function called GenerateDifference() or something of that nature describes in words what is being done. A programmer would also be expecting operator -= to be overloaded if operator - is overloaded.

    When I write programs, I make it a rule that all base functionality is written concretely (no operator overloaded functions doing the real work). If I now need to overload operators, the overloaded operators call the concrete functions to do the work.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; May 24th, 2012 at 10:09 AM.

Page 2 of 2 FirstFirst 12

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

Featured