Thread: Though implementation problem

  1. #1
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654

    Tough implementation problem

    OK, here's another tough implementation problem.
    CTmplStringBase will have one of two functions depending on what type T is, so naturally to this, a specialized template would be required.
    Unfortunately, that means I have to create a new class and inherit from, else I would have to remake the entire class in the specialization.
    But this new class returns an object of the class it inherits from, so it, in turn, needs the inheriting class to be defined first.
    But then, the code won't compile because CTmplStringBase would try to derive from an undefined class.
    So what's a solution to this?

    CTmplStringBase wants to derive from StrConversions:
    Code:
    	template<typename T, typename Traits> class CTmplStringBase:
    		public StrConversion<T>,
    Because it wants access to specialized template member functions:
    Code:
    	template<typename T> class StrConversion { };
    	template<> class StrConversion<char>
    	{
    	public:
    		CStringExW ToUTF16();
    	
    	private:
    		virtual const char* GetData() = 0;
    	};
    	
    	template<> class StrConversion<wchar_t>
    	{
    	public:
    		CStringExA ToANSI();
    	
    	private:
    		virtual const wchar_t* GetData() = 0;
    	};
    But StrConversion wants to return objects of CTmplStringBase, so I get circular dependencies.
    Last edited by Elysia; 05-11-2008 at 10:00 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Your irrational fear of non-member functions has caused you to do all sorts of bizarre things...

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Fear? Hardly. They're evil.
    I don't like them; not I fear them.
    But regardless. If I wanted, I could just add two methods to the class, in which case they would show up for both Unicode and ansi specialization of the class (the same as for non-member functions). But I wanted something extra - to include a member only for specific specializations.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Elysia View Post
    Fear? Hardly. They're evil.
    I don't like them; not I fear them.
    You're inheriting a String class from something called StrConversion. Your design is critically ill, and I don't mean that in the rapper sense.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Then suggest a better solution (I don't like it anymore than you do), which does not include non-member functions.
    So far as I see, I'm hitting a language barrier and have to retort to some workaround to make it work.
    Last edited by Elysia; 05-11-2008 at 10:24 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Elysia View Post
    Then suggest a better solution, which does not include non-member functions.
    I can't give you what doesn't exist.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Oh sure, there does exist solutions. One I can think of is some workarounds. Temporary object inside class to return by reference. Or better, return via a smart pointer.
    Those seems like big hacks to me, though, and was wondering if there's another solution or what another dev would do to make it work like it, if they would at all.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Elysia View Post
    Oh sure, there does exist solutions. One I can think of is some workarounds. Temporary object inside class to return by reference. Or better, return via a smart pointer.
    Those seems like big hacks to me, though, and was wondering if there's another solution or what another dev would do to make it work like it, if they would at all.
    You're willing to accept "big hacks" but not a non-member function. Okay..

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What's wrong with providing ToUTF16 and ToANSI as free functions?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    They are free functions (currently) and would like to integrate them into the class, preferably as implementations that exist only in appropriate specializations, if possible.
    So long as it doesn't have a big impact on performance, I'm willing to accept hacks if it's possible to get around.
    OOP is more important than free functions and algorithms to me.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Elysia View Post
    They are free functions (currently) and would like to integrate them into the class, preferably as implementations that exist only in appropriate specializations, if possible.
    So long as it doesn't have a big impact on performance, I'm willing to accept hacks if it's possible to get around.
    OOP is more important than free functions and algorithms to me.
    You're high.

  12. #12
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    This is why Dr. Stroustrup designed C++ to support procedural, object-oriented, and generic programming. He understands that one paradigm isn't best for all problems. I'm not sure why you think every problem is best solved with objects.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    OOP is more important than free functions and algorithms to me.
    As far as I can tell your code remains object oriented with those two functions as free functions.

    I suppose if you really insist you could make them static member functions instead, like what the Java programmers would do
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Elysia has apparently already decided that all objects should be all things to all people. I'm waiting for the realization that this implies an infinite amount of code, but it seems to be a long time coming...

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I don't like the idea to be limited to one paradigm either. I use them as I see fit. However, to these kind of things, classes are best IMHO. All functionality for an object to be grouped inside its object and not broken out.
    Why is it so difficult to learn C++ and not VB or Java (unsure about this one) or such languages, I wonder? Because everything is neatly organized into one place.
    If a programmer has to rely on algorithms to get things done (and I mean simple tasks), or go browse through a namespace that's not named in the docs, things DO get harder.

    Difficulty is what I try to avoid. No wonder C++ is difficult to learn.

    And some don't seem to understand my point either...
    Classes, like functions, can be extended. It's your job to make sure they can be. They should be. Otherwise you did something wrong.

    One class should be one thing. Don't add everything into it. But a string class should have certain things, such as ToLower/ToUpper, which the standard library seem to lack.
    Classes can grow just as free functions can grow. Now, the problem is to find a way to make it possible for the classes to grow similar to free functions.

    I don't I'd use static members, though. They would appear inside the class namespace. I'd rather make them free and move them into the Strings namespace... Neatly organized.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  2. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  3. implementation file
    By bejiz in forum C++ Programming
    Replies: 5
    Last Post: 11-28-2005, 01:59 AM
  4. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  5. Memory Problem - I think...
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 10-24-2001, 12:14 PM