Click here to Skip to main content
16,012,061 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralMinimized Window doesnot show up Pin
8-Dec-00 9:38
suss8-Dec-00 9:38 
GeneralHelp with string class - return char * Pin
Christian Graus7-Dec-00 12:04
protectorChristian Graus7-Dec-00 12:04 
GeneralRe: Help with string class - return char * Pin
Michael Dunn7-Dec-00 13:35
sitebuilderMichael Dunn7-Dec-00 13:35 
GeneralRe: Help with string class - return char * Pin
Christian Graus7-Dec-00 14:04
protectorChristian Graus7-Dec-00 14:04 
GeneralRe: Help with string class - return char * Pin
Erik Funkenbusch8-Dec-00 10:20
Erik Funkenbusch8-Dec-00 10:20 
GeneralRe: Help with string class - return char * Pin
Christian Graus8-Dec-00 12:22
protectorChristian Graus8-Dec-00 12:22 
GeneralRe: Help with string class - return char * Pin
Erik Funkenbusch8-Dec-00 18:25
Erik Funkenbusch8-Dec-00 18:25 
GeneralRe: Help with string class - return char * Pin
Christian Graus9-Dec-00 9:42
protectorChristian Graus9-Dec-00 9:42 
>I take it you mean you want to create a class with the same interface as CString, which is portable.

Yes.

>You can mostly achieve this by using a std::string as a member of CGString class, then set up forwarding functions that call members in string,

Yes, that is what I do where possible - no point reinventing the wheel, and that was the point of inheriting string.

>or alternatively you can use private inheritance of std::string. This will make it so that you can't assign a
CGString pointer to a a std::string pointer.

Hmm... If I provide interfaces to *all* of string, then I *could* do that.... Any down side ?

>However, it won't be possible to completely duplicate CStrings functionality for a variety of reasons, especially if you provide a virtual destructor.

Actually, given that string's destructor will always be called, I removed my destructor, forgetting that I will be given one anyhow by my compiler. Some thought needed there, methinks.

>If you look at CString, you'll also see that it doesn't provide a virtual destructor. The reason is simple:

>You can cast a CString to a char* and use it as if it were an array of characters. It does this by making the array pointer the first member in the class, thus a pointer to the CString is also a pointer to it's first member. In classes that have vtables though, the pointer to the class is also the pointer to the vtable, which would not work.

So I can't cast to char* in my class for this reason then ?

>This isn't exactly good behavior though, so you might just want to say "You can't do that in my CGString", which would be fine unless you have existing code which expects that.

I doubt we have code that expects it, but this could be hairy, none of us there now were there for the early to mid stage of thge project.

>As far as external functions go, I think you're stuck in "everything has to be an object" mentality. Look at the standard C++ library. It provides all kinds of functions called algorithms that work on generic objects. These algorithms aren't part of the objects themselves, and it's still object oriented. You don't have to make everything a member for it to be OO. This is the biggest mistake most people make when using C++, too much encapsulation.

Fair enough. Although I understand what you're saying in general, surely you'd agree 'format' and 'makelower' are not terribly generic ?

>The things you mention are easily accomplished with external functions and std::string. You want to make lower, just use:

>std::string s("ThIs Is A mIxEd CaSe StRiNg");
>std::transform(s.begin(), s.end(), s.begin(), std::tolower);

I s'pose, but s.MakeLower sees to me easier to read and easier to use.

>Format is indeed handy, but it's simple enough to create your own format function that does the same thing using sprintf.

Funny you mention that, because I've done this:

CGString& CGString::Format(const char* pChar, ...)
{
if (strlen(pChar)<2)
return *this;

va_list argList;
va_start(argList, pChar);
char* pReturn = new char [400];
memset(pReturn,0,400);
sprintf(pReturn,pChar,argList);
va_end(argList);
this->assign(pReturn);
return *this;
}

as a starting point ( I wanted to get it to work *at all* before cleaning it up, for starters I need to allocate a correct size to the char * that receives the data instead of an arbitrary value ) and it compiles, runs and returns garbage in the sections of a string that is formated, but correct data for the section that was in the format string. For example:

int i = 27;
CGString g;
g.Format("test me %d",i);

would leave g equalling "test me *insert big number here". Can you see what I've done wrong ?

Thanks for all the help & advice. I admit to being far more up on MFC than standard C++ right now, which is half the reason I put my hand up for this class, to study a bit and redress the balance.

Christian

The content of this post is not necessarily the opinion of my yadda yadda yadda.

To understand recursion, we must first understand recursion.
GeneralRe: Help with string class - return char * Pin
Erik Funkenbusch9-Dec-00 12:54
Erik Funkenbusch9-Dec-00 12:54 
GeneralRe: Help with string class - return char * Pin
Christian Graus9-Dec-00 21:28
protectorChristian Graus9-Dec-00 21:28 
GeneralRe: Help with string class - return char * Pin
Erik Funkenbusch11-Dec-00 12:25
Erik Funkenbusch11-Dec-00 12:25 
GeneralRe: Help with string class - return char * Pin
Christian Graus8-Dec-00 12:58
protectorChristian Graus8-Dec-00 12:58 
GeneralRe: Help with string class - return char * Pin
Erik Funkenbusch8-Dec-00 18:26
Erik Funkenbusch8-Dec-00 18:26 
Generaldisable one ActiveX function Pin
7-Dec-00 7:57
suss7-Dec-00 7:57 
GeneralRe: disable one ActiveX function Pin
SAWilde7-Dec-00 23:03
SAWilde7-Dec-00 23:03 
GeneralRe: disable one ActiveX function Pin
8-Dec-00 6:23
suss8-Dec-00 6:23 
GeneralDesktop icons and their labels Pin
Daníel B. Sigurgeirsson7-Dec-00 4:46
Daníel B. Sigurgeirsson7-Dec-00 4:46 
GeneralRe: Desktop icons and their labels Pin
Christian Graus7-Dec-00 12:07
protectorChristian Graus7-Dec-00 12:07 
GeneralRe: Desktop icons and their labels Pin
Daníel B. Sigurgeirsson11-Dec-00 0:17
Daníel B. Sigurgeirsson11-Dec-00 0:17 
GeneralChanging text color on Buttons created on the fly Pin
Ishai Shotten7-Dec-00 4:27
Ishai Shotten7-Dec-00 4:27 
GeneralCRichEditView context menu Pin
7-Dec-00 1:42
suss7-Dec-00 1:42 
GeneralRe: CRichEditView context menu Pin
SAWilde7-Dec-00 23:23
SAWilde7-Dec-00 23:23 
GeneralToolBar Pin
6-Dec-00 17:04
suss6-Dec-00 17:04 
GeneralRe: ToolBar Pin
Michael Dunn6-Dec-00 17:32
sitebuilderMichael Dunn6-Dec-00 17:32 
GeneralRe: ToolBar Pin
Christian Graus6-Dec-00 19:06
protectorChristian Graus6-Dec-00 19:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.