|
Ok, this is not an answer - just a 'hey what a neat problem' reply.
The only difference between C and C++ in this area is that C++ enforces typechecking on parameters passed to a function. So you can, in C, pass a void * to a method and then let that method figure out what it is (if need be).
In C++, there's probably a few workarounds - including overloads of a fn for each type that might be received, IDing the classes in the array by first casting them to the base then retrieving an ID denoting their type etc.
I get a feeling that you could somehow modify the C code to work, involving some ugly casting, but that there's probably a creation pattern that covers this.
Hmmm...
|
|
|
|
|
I split the view of my application in two parts. My problem is that the status bar and toolbar disappear after the split and I don't know why!!!
This is my code :
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
CCreateContext* pContext = (CCreateContext*)lpCreateStruct->lpCreateParams;
int rc;
CRect cr;
if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1;
if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
{
TRACE0("Failed to create toolbar\n");
return -1; // fail to create
}
if (!m_wndStatusBar.Create(this) ||
!m_wndStatusBar.SetIndicators(indicators,
sizeof(indicators)/sizeof(UINT)))
{
TRACE0("Failed to create status bar\n");
return -1; // fail to create
}
// TODO: Delete these three lines if you don't want the toolbar to
// be dockable
m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
EnableDocking(CBRS_ALIGN_ANY);
DockControlBar(&m_wndToolBar);
GetClientRect(&cr);
CSize paneSize(cr.Width() / 2, cr.Height());
m_wndSplitter.CreateStatic(this, 1, 2);
if(!m_wndSplitter.CreateView(0, 0,
RUNTIME_CLASS(CBOFListView),
paneSize, pContext))
{
TRACE0("Failed to create Splitter Wnd\n");
return -1; // fail to create
}
if(!m_wndSplitter.CreateView(0, 1,
RUNTIME_CLASS(CBOFInfoView),
paneSize, pContext))
{
TRACE0("Failed to create Splitter Wnd\n");
return -1; // fail to create
}
return 0;
}
Best Regards!
|
|
|
|
|
Try to set
CSize paneSize(200,200);
then try to handle the OnSize message as follows:
void CMainFrame::OnSize(UINT nType, int cx, int cy)
{
CFrameWnd::OnSize(nType, cx, cy);
CRect cr;
if ( m_initSplitters && nType != SIZE_MINIMIZED )
{
m_mainSplitter.GetClientRect( &cr );
m_mainSplitter.SetColumnInfo( 0, cr.Width() / 2, 0 );
m_mainSplitter.SetColumnInfo( 1, cr.Width() / 2, 0 );
m_mainSplitter.RecalcLayout();
}
}
m_initSplitters is a BOOL set to TRUE after the splitters have been created in the OnCreateClient-function. Use this function to create your splitters instead of the OnCreate function
regards!
|
|
|
|
|
My application "uses" tons of memory. And yes, all of it get's deallocated when program exits. What I would like to do is to find out where all allocation goes too... Application has 100's of classes.
Memory allocated by "new"
Brian
#define new new(_T(_FILE_), _T(_LINE_))
Anyone has class that agregate line above?
|
|
|
|
|
you need to use delete for every variable that you had allocated with new
Cheers!!!!
Carlos Antollini.
|
|
|
|
|
I am not a newbie.
No I don't any leaks. I am looking for a way to optimize structures
to reduce memry overhead.
what does "delete" has to do with this?
Brian
|
|
|
|
|
Wow, that was rude.
The only thing you can do is delete the pointers as soon as you don't need them any more, but then, you take a chance on fragmenting memory.
Other than that, we really can't help you much without seeing the code and analyzing your structures.
|
|
|
|
|
OK, if I understand your question, you're saying that you don't have memory leaks, but you want to know which functions are allocating (and deleting) lots of memory, and which aren't so that you know which ones to optimise?
I'm not sure about your redefinition of new, but you could always try profiling your code, and find out how often the constructors of your various classes get called. That should give you some idea of where the most memory is being allocated. Have a look at "profiling" in MSDN for more info.
Hope this helps.
------------------------
Derek Waters
derek@lj-oz.com
|
|
|
|
|
My program has gone psycho, and I'd really appreciate some help reducing the 115 compilation errors down to none The problem is with the VC++ generated line:
static char THIS_FILE[] = __FILE__;
that is placed in every cpp file. For some reason, this is giving me serious errors in every class!
Looks something like this:
error C2258: illegal pure syntax, must be '= 0'
error C2252: 'THIS_FILE' : pure specifier can only be specified for functions
error C2838: illegal qualified name in member declaration
error C2059: syntax error : 'return'
error C2238: unexpected token(s) preceding ';'
error C2143: syntax error : missing ';' before '*'
error C2501: 'GetMessageMap' : missing storage-class or type specifiers
error C2556: 'int *__thiscall CTestApp::GetMessageMap(void) const' : overloaded function differs
43) : see declaration of 'GetMessageMap'
error C2373: 'GetMessageMap' : redefinition; different type modifiers
43) : see declaration of 'GetMessageMap'
error C2143: syntax error : missing ';' before 'tag::id'
error C2734: 'AFX_MSGMAP' : const object must be initialized if not extern
error C2371: 'AFX_MSGMAP' : redefinition; different basic types
p(38) : see declaration of 'AFX_MSGMAP'
fatal error C1004: unexpected end of file found
Every one of those errors points to the BEGIN_MESSAGE_MAP macro. Needless to say, this is the lamest thing ever, and I don't have a clue what caused all this craziness. Any help is much needed
thanks a ton,
Jake
|
|
|
|
|
If you have any inline functions in your header file, make sure they have a matching {} pair. If you use an ")" instead of a "}", you'll get all kinds of bizarre errors. Also, make sure the statement inside the "{}" pair ends with a semi-colon.
Lastly, if this is an abstract class, make sure you've completed the definitions inside the class header.
If you're deriving forom an abstract class, the pure virtual functions have to be prototyped AND have a body.
|
|
|
|
|
Sweet Jesus it actually worked! I had a non-inline function without the semicolon, and the compiler wasn't happy. Thanks a ton!
Jake
|
|
|
|
|
Hello ,
I have small , probably , beginners' question ... Why all coders always put Definition of functions in .h files , but function's body in .CPP (.C) files ?
Is not more good to put body of function into .H file , without using .CPP ? Is not more good to define all variables in .H files ?
I just don't understand what's more effective and why that's heppens , please help !!!!
Thanks for reading , GregQ
|
|
|
|
|
I think that you need to read the Kernigan-Richi Book. If you didnt read that, is good for before to sleep.
I need to make a question..
Why don't to put all the code of a project over 100.000 lines in a only .cpp?
Try to make some like you say and see what happens with your compiler....
I never did something like you say or like I say.....
For finished- You can write all your code in a same cpp, Is True you don't will have problems with that. But if you add a new cpp and in this last cpp you need to use a function that is in the first cpp, you have two solutions. Or copy the function into the new cpp or declare the function into a header. But a question, Your code is Good?
In the first case you will have serious problems when you need to change something into that function, because you need to remember taht you have a copy of that function in other file.
In the second case, you are starting to write a good code... Is my point of view.
Regards!!!!
Carlos Antollini.
|
|
|
|
|
Heh ! No .. I mean why not to put body of function in .h files ... MANY .h files .. If you want 1 function per .h file .... But I can define and describe function in the same file (.h) !!! Why I should define it in .h , but describe in .cpp ???
|
|
|
|
|
Is better for the compiler and for your code.
You need to read how work the compiler. I don't remember a good book.
If you have a oportunity to read this, you will understand that. For me is a little difficult to explain it.
Carlos Antollini.
|
|
|
|
|
Oh , If it's better then OK ... Thank you very much ...
|
|
|
|
|
Atleast ONE good reason is that if you put all your code in the .H files they will be
very tightly coupled to the implementation of the classes. This means that if you change a minor imp. detail in one memberfunction ALL files including that file will have to be recompiled. With a large project this can take *very* long time...
Publish your interfaces, not your implementation.
Another point is that some uses of templates really get messy when using only .H files.
|
|
|
|
|
While it's certainly possible to put code in header files (ATL and WTL do this) it's more efficient at build time to not do so.
Imagine a big project, say 100 CPP files. There's one header file, call it "funcs.h", that has the code for some utility functions. Every CPP file uses at least one utility function, so they all #include funcs.h.
Now suppose you fix a bug in a utility function. Next build, every CPP file must be recompiled because they all depend on the header file.
If you put the code into its own funcs.cpp file, only that file needs to be compiled.
There's also the whole issue of implementation hiding in C++. And commercial libraries that ship header files obviously don't put their code in the publically-available header files.
--Mike--
http://home.inreach.com/mdunn/
"Make sure that if you are using a blow torch that you don't set anything on fire."
-- Chris Maunder
|
|
|
|
|
It makes compiling more efficient because if you had everything in the .h file everytime you modified one function you would have to recompile the entire application. Using the header files you can link code together and build an application across multiple files. header files also are needed when working with libraries.
|
|
|
|
|
does anybody know how to find a word in a string?
for example I want to find the word "test" in the string "This is a test"...
Is String.Find(... the right way?
|
|
|
|
|
yeah, CString::Find will return the position where "test" begins (10, I think), or -1 if it's not in the string.
Jake
|
|
|
|
|
You can use strstr()
char str_to_find[] = "test";
char *pdest;
int result;
pdest = strstr( your_string, str_to_find );
result = pdest - you_string + 1;
if( pdest != NULL )
printf( "%s found at position %d\n\n", str_to_find, result );
else
printf( "%s not found\n", str );
I more or less just copied the code from the MSDN.
OR you can use the MFC-function Find()
int res = string.Find("test")
"string" needs to be from type CString
hope this helps
|
|
|
|
|
If you're using MFC's CString, use this:
CString myString = "This is a test...";
int pos = myString.Find("test");
if (pos == -1)
{
// string not found
}
else
{
// string found
}
If you're using Standard C++, use this, check out the strstr() function.
|
|
|
|
|
Thank you guys!!!!!!!!!!!!!!!
|
|
|
|
|
Does anybody know how to make a tree controled options dialog, without mfc? Like the one in winamp? with a tree on the left that changes the dialog layout on the right with each selection... do they use property sheets? i've read the articles on this... but i don't want to use MFC! my app is written entirely in pure win32 api from the platform sdk.
thanks,
john
|
|
|
|