C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 05-11-2008, 09:34 AM   #1
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
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.
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.

Last edited by Elysia; 05-11-2008 at 10:00 AM.
Elysia is offline   Reply With Quote
Old 05-11-2008, 10:16 AM   #2
Senior software engineer
 
brewbuck's Avatar
 
Join Date: Mar 2007
Location: Portland, OR
Posts: 5,381
Your irrational fear of non-member functions has caused you to do all sorts of bizarre things...
brewbuck is offline   Reply With Quote
Old 05-11-2008, 10:19 AM   #3
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
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.
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 05-11-2008, 10:20 AM   #4
Senior software engineer
 
brewbuck's Avatar
 
Join Date: Mar 2007
Location: Portland, OR
Posts: 5,381
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.
brewbuck is offline   Reply With Quote
Old 05-11-2008, 10:21 AM   #5
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
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.
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.

Last edited by Elysia; 05-11-2008 at 10:24 AM.
Elysia is offline   Reply With Quote
Old 05-11-2008, 10:22 AM   #6
Senior software engineer
 
brewbuck's Avatar
 
Join Date: Mar 2007
Location: Portland, OR
Posts: 5,381
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.
brewbuck is offline   Reply With Quote
Old 05-11-2008, 10:26 AM   #7
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
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.
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 05-11-2008, 10:29 AM   #8
Senior software engineer
 
brewbuck's Avatar
 
Join Date: Mar 2007
Location: Portland, OR
Posts: 5,381
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..
brewbuck is offline   Reply With Quote
Old 05-11-2008, 10:30 AM   #9
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,368
What's wrong with providing ToUTF16 and ToANSI as free functions?
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is online now   Reply With Quote
Old 05-11-2008, 10:32 AM   #10
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
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.
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 05-11-2008, 10:34 AM   #11
Senior software engineer
 
brewbuck's Avatar
 
Join Date: Mar 2007
Location: Portland, OR
Posts: 5,381
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.
brewbuck is offline   Reply With Quote
Old 05-11-2008, 10:35 AM   #12
Afraid of widths
 
medievalelks's Avatar
 
Join Date: Apr 2008
Location: Chicago
Posts: 887
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.
medievalelks is offline   Reply With Quote
Old 05-11-2008, 10:36 AM   #13
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,368
Quote:
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
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is online now   Reply With Quote
Old 05-11-2008, 10:39 AM   #14
Senior software engineer
 
brewbuck's Avatar
 
Join Date: Mar 2007
Location: Portland, OR
Posts: 5,381
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...
brewbuck is offline   Reply With Quote
Old 05-11-2008, 10:44 AM   #15
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
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.
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
WS_POPUP, continuation of old problem blurrymadness Windows Programming 1 04-20-2007 06:54 PM
Laptop Problem Boomba Tech Board 1 03-07-2006 06:24 PM
implementation file bejiz C++ Programming 5 11-28-2005 01:59 AM
Sorting problem.. well actually more of a string problem fatdunky C Programming 5 11-07-2005 11:34 PM
Memory Problem - I think... Unregistered C Programming 4 10-24-2001 12:14 PM


All times are GMT -6. The time now is 01:05 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22