|
This is probably a dumb question, but I was examining the vector class source code (Visual C++ 2003) and noticed some method definitions that were confusing me. After some thought, I got on this tangent about template definitions in general and started to question why all the method definitions needed to be "templatized" (for lack of a better word) if some methods never dealt directly with any "typename" for the template.
In other words, if a template method (lets call it "GetVersion") did something simple like return an integer constant, irregardless of any "typename", and the class was instantiated twice, lets say...
CMyClass<int> first;
CMyClass<float> second;
and then somewhere in the code, each instance (first and second) calls "GetVersion", I'm assuming only one copy of "GetVersion" gets created by the compiler.
If this is true, why does the method definition need to "templatized"? Or Does it need to be "templatized"?
The code for vector almost appears to be taking advantage of some technique that only bothers with template syntax for the methods that deal with any typename but I could easily have been deceived. Is it possible to define non-templatized methods in a template class or am I just off my rocker? Or does the compiler require template syntax for every method? (So far, I only get compiler errors if I try to omit template syntax for such methods so I'm guessing I've strayed from reality a bit here but I thought I'd ask anyway)
|
|
|
|
|
This is a very good question.
I do think that a function within a templated class, such as GetVersion() will indeed be implemented for each class the template gets instantiated for, creating redundand code. If this is not the case I'd really like to know from someone more knowledgable than me.
You can however avoid it with various tricks, such as moving the function(s) in question out of the class definition, or into a base class that doesn't depend on a template class argument.
|
|
|
|
|
I've thought about it a bit, and found something that forces the compiler to create individual methods, even if it considers the code of the entire project. What I am thinking of is Template specialization: any code using a template class may define a specialized implementation of an otherwise 'non-templatized' method, and that might happen even outside the project the compiler is looking at:
template<class C>
class MyClass {
public:
int version() const { return 1; }
};
#include "MyClass.h"
template <>
class MyClass<int> {
public:
int version() const { return 2; }
};
|
|
|
|
|
Yeah. I'm now realizing I really lost track of reality on this one.
I think I got turned around a bit trying to think about it too much (thinking is such a dangerous pastime).
C# has been rotting my brain lately so I'll blame it on that. 
|
|
|
|
|
No worries. It was a good question. I had to think for quite a time to come up with a good reason.
While all you could get out of preventing the redundand template instantiation would be a few bytes less in your executable, I appreciate that some people at least consider the possibility to avoid unneccessary clutter. If all software developers were like you, Windows would run on 500 KB rather than 5 GB. 
|
|
|
|
|
Of course the compiler will generate two 'GetVersion ' methods, one for each class.
Please note, they have even different signature.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
You're right. I don't know what I was thinking.
I think my reality got altered a bit looking at the source code for vector and I started to believe the world was flat.
Thanks for helping straighten me back out! 
|
|
|
|
|
Hi I m trying to load the dll using LoadLibrary which works fine.
But for one of the method paramter is an object of class which is refered inside the dll . So in this case in my client app how do i refer the .h file which is already refered in the dll inside ?
As i m trying to link dynamically how does header file references are dealt with ?
Thanks
|
|
|
|
|
When you want to call a function that is in a DLL - whether it is dynamically loaded or not - you must tell the compiler what the function parameters are. The is what the .h file does.
|
|
|
|
|
See if this[^] helps.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
You repeatedly use the term 'refer', but didn't really clarify at which point you have trouble making the connection between your application and the DLL - it could be at compile time, at linking time, or at run time:
1. compiling
Any parts of the DLL that you are supposed to use from outside have to be declared in header files, using the 'extern ' keyword. These header files then have to be included in the application that wants to use the DLL. This is neccessary for the compiler to understand the format of the function calls and the types of the parameters and return values.
2. linking
Once your application is successfully compiled, the linker will try to resolve the function calls into the DLL. This requires information about the already compiled DLL, which is contained in a file called '<dll-name>.lib'. You have to specify this Lib file (and possibly it's path) in the linker options. With the help of this Lib file, the linker will look up the offset of the function within the DLL and encode this into the function call.
3. running
At run-time your application will eventually call the LoadLibrary function that you specified. Provided the DLL can be found at the same location as your application is, or else, if you provided a path, at that path's location, the DLL will now be loaded into memory. When your code runs into a DLL function call, the system will use the linker-provided offset to invoke the correct function within the DLL.
|
|
|
|
|
Hi all,
i m using a access file as databse with odbc connection.
and use CRecordset class to perform action over database file and its tables like add,update,movenext etc.
some time in access file table filed have value like "#Error" and when i select this row or column access file popup the error message "Not a valid bookmark"
When I compacted/repaired it I received these errors in a table called MSysCompactError:
-1206 Could not find field 'Description'
-1053 Could not find field 'Description'
please help me how can i resolve this and prevent to occur this error.
thanks in advance.
|
|
|
|
|
Sounds like an Access issue rather than a C/C++/MFC issue.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
|
|
|
|
|
Dear all,
Usually when I create a function in C++ I use 'const' for the input arguments that I do not change in the function. For example :
void myFunction( const float * pMyArray, const int numelMyArray, ... );
That allows you to see what arguments are inputs and protects against accidental modification of the data. The data in pMyArray is not const.
So far so good. My problem occurs when I want to pass the function an array of pointers to arrays, and still use const to avoid , like this:
void myFunction( const float ** ppMyPtrArray, const int numelMyPtrArray, ... );
When I pass the function an array of pointers to arrays of non-const floats, I get the the following error during compilation:
cannot convert parameter 1 from 'float **' to 'const float **' 1>
Conversion loses qualifiers
What on earth is happening here? Suddenly 'const' for function arguments does not work like (I) expected...
Please help me solve this mystery
A good day to you all!
|
|
|
|
|
The problem here is that the const qualifier does not apply to what you think it does: the next symbol next to const is float in both cases, so the compiler interprets these types as "pointer to const float " in the first case, and "pointer to pointer of const float " in the second.
In the first case, when you pass a pointer of type float* , the compiler will cast it to const float* , since casting a pointer of a non-const type to a pointer to const values of the same type is valid.
In the second case, the compiler cannot make that cast implicitely, as that would require casting a pointer to pointers of one type to a pointer to pointers of another type, which is a different animal. Implicit conversion only works so far - you sometimes have to help the compiler along.
Of course, you might not have meant the float elements to be the const part in this call, but rather the pointers to the floats? If so, you need to shift the const to the right location. Else the solution is as simple as making an explicit type cast.
|
|
|
|
|
const doesnt work as you expect. Sorry .
const will only affect the access to 1 thing, depending on where it is depends on what it affects.
const char *szMsg = "hello world";
szMsg = "hello back"; is valid
szMsg[0] = 'H'; is invalid
This const will stop you changing the contents of the string "hello world" , however it will not stop you changing the value of the pointer szMsg .
char const *szMsg = "hello world";
This is the same as the previous const char *szMsg = "hello world";
This is very uncommon and usually provides no use. What a function does with its pointers is its business. As long as it doesn't change your data you don't care.
char *const szMsg = "hello world";
szMsg = "hello back"; is invalid
szMsg[0] = 'H'; is valid
This makes the value of the pointer szMsg constant, but not the data pointed to
Combining the 2
const char *const szMsg = "hello world";
szMsg = "hello back"; is invalid
szMsg[0] = 'H'; is invalid
This makes everything constant.
What does this mean for you?
This means that your constant 2D array can be changed.
You are only protecting the bottom level pointer, which means you cannot change the values pointed to by the 2nd dimension of the array (the floats).
You are not protecting the 2D array itself. The location at which the array is located and the 2nd dimension of the array can be changed at will.
const char *szMsgs1[] = { "hello", "world" };
const char *szMsgs2[] = { "hello", "back" };
const char **szMsg = szMsgs1;
szMsg = szMsgs2;
szMsg[1] = szMsgs2[1];
szMsg[0][0] = 'H';
The reason it is complaining is that the thing you want to change from non-const to const is at the 2nd level. 2 pointers must be traversed before you reach the constant data.
|
|
|
|
|
Hi,
I have created a dialog box and calling in mainframe as DoModal() but still I am able to intract with parent window?
I have modified the dialog constrcuor only, code is below:
class CTxProgressDlg : public CDialog
{
public:
CTxProgressDlg (CUpdates *pUPdate,STRINFO* pstrInfo,DWORD dwSize,BOOL bUpgrade,BOOL *bStatus,BOOL *bDownloadAborted, CWnd* pParent = NULL);
....
....
};
IMPLEMENT_DYNAMIC(CTxProgressDlg , CDialog)
CTxProgressDlg ::CTxProgressDlg (CUpdates *pUPdate,STRINFO* pstrInfo,DWORD dwSize,BOOL bUpgrade, BOOL *bStatus,BOOL *bDownloadAborted,CWnd* pParent )
: CDialog(IDD_DIALOG_PROGRESS, pParent)
I am calling it as
CTxProgressDlg dlg(NULL,NULL,0,1,&bStatus,&m_bDownloadAborted,this);
dlg.DoModal();
Style of Dialog is Popup.
Why this is not DoModal?
modified on Monday, February 7, 2011 2:47 AM
|
|
|
|
|
Are you creating the dialog in a separate thread ? In which case that would explain why you are still able to interract with the parent window.
|
|
|
|
|
I have an CComboBoxExt derived from CCOmboBox .. but I have an small problem , perhaps you can help me :
CComboBoxExt m_Combo;
void CTestComboView::OnInitialUpdate()
{
CFormView::OnInitialUpdate();
GetParentFrame()->RecalcLayout();
ResizeParentToFit();
m_Combo.AddString("One");
m_Combo.AddString("Two");
m_Combo.AddString("Three");
}
and when I type 'O' in combobox , it select me automatically 'One' item from the list ... well , I try this one :
void CComboBoxExt::OnEditupdate()
{
CString sText;
GetWindowText(sText);
DWORD dwCurSel = GetEditSel();
WORD dStart = LOWORD(dwCurSel);
WORD dEnd = HIWORD(dwCurSel);
if(dStart == 0 && dEnd == sText.GetLength())
SetEditSel(sText.GetLength(),sText.GetLength());
}
but have no effect ....
|
|
|
|
|
I forget to say something :
I use this , may me this cause weird behaviour ?
void CComboBoxExt::OnEditchange()
{
CString sText;
GetWindowText(sText);
if(sText.IsEmpty())ShowDropDown(FALSE);
else ShowDropDown();
}
|
|
|
|
|
mesajflaviu wrote: ...but I have an small problem...
Which is?
You've shown code, but have failed to explain what is supposed to happen vs. what is actually happening.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
|
|
|
|
|
The problem is that when I type 'O' , the control autocompletion 'One' in editbox , select all text and put the mouse cursor at the end of the 'One' string .. is annoynig , because I want to type myself all string ... for more details , I put here[^] an source code .. .just type 'O' in combobox ....
|
|
|
|
|
Hello friend, I think a lot of people in this board have ever been used "Print to file" option in Printer Dialog.
That it can save our printing job to file and then print it out later.
I want to create a program that can read that file.
And the program also can revise the printer setting and page set up before the real printing.
Please advise me some concept or article of this task.
Thank you
|
|
|
|
|
I assume you are talking about the .xps file.
There is a COM API provided by Microsoft for working with this file format. See the documentation on MSDN[^]
Grab the latest version of the Windows SDK[^], which you will need to comiple your project, but also has some great little samples under the folder C:\Program Files\Microsoft SDKs\Windows\v7.0\Samples\xps (alternativley you can download just the samples from http://code.msdn.microsoft.com/xps[^]). The documentation for these samples can be found on MSDN[^].
|
|
|
|
|
hello, anyone have an idea of a code in MFC to shut down with time the pc ? in Vs 2008 ?
|
|
|
|