|
I am trying to pass in a vector of some subclass to a function defined with the base class in the parameter list, like void mod(vector < baseclass > &bcs). If I have a vector of subclass objects, as vector < subclass > sc, and call mod(sc), I get the error "A reference that is not to 'const' cannot be bound to a non-lvalue". Doing this without vectors is no problem; i.e, void mod(baseclass &bc), subclass sc; call mod(sc). I can get around this by defining 2 sum(*) functions, one with a baseclass parameter and one with a subclass parameter, but there goes the use of inheritance. The vector will by modified by mod(*), so I don't want to pass as const. Is there a way I can use 1 function on both the base and sub classes? Thanks.
|
|
|
|
|
OK, I haven't done C++ for 5 years but here are some thoughts (which may be wrong but may suggest some ideas)
1. Does C++ allow vector<subclass> to be substituted for vector<baseclass> in the first place (template covariance)?
2. If it does then try void mod(const vector<baseclass> &bcs) as your function signature. (Parameters should be const-qualified anyway.)Kevin
|
|
|
|
|
This would be a no, since vector< subclass > is not a subclass of vector< baseclass >. (Note that in the implementation the internal arrays may have different size entries. If you somehow forced this into one function, your single function would be using the wrong entry size and be reading misaligned garbage rather than valid objects.)
My first quick thought is that you could use a vector< std::tr1::shared_ptr< baseclass > > in both cases. This would, however, impact the rest of your software, and might not be acceptable.
My second thought is that you could turn your function into a template itself. This changes your maintenance to 1 place, but really still leaves you with 2 compiled function bodies.
My third thought would be to provide a wrapper ( internally akin to a discriminated union ) with forwarding functions. I believe this would be a bad idea and extra effort without real benefit.
Maybe someone else has a better idea, I've run out for now. It looks to me that making your function into a template function is about the best you will do.Please do not read this signature.
modified on Thursday, February 18, 2010 4:22 PM
|
|
|
|
|
Thanks for the responses everyone. I'll go with the template function.
|
|
|
|
|
can you change the parameter to vector < baseclass *> ?
yeah, you'd have to build vectors of pointers before each call (which might be a PITA).
|
|
|
|
|
That would seem like a better idea, unless it's part of the plan to slice. Steve
|
|
|
|
|
Does anyone know of a (mostly) accurate algorithm to determine if a block of text is EBCDIC? I'm not trying to convert it, just find out if it's EBCDIC.
|
|
|
|
|
is it a formatted block of text ? ie, do you know if there is numbers at a particular location ??
If you knew this, then, Hex F0..F9 represents digits 0..9, so you could scan for them .. If its 'straight text in a 'normal' codepage, then its highly unlikely it would have high-range characters like F0..F9 in ascii, becuase they are accented characters ...
Interesting question Hans, I know of no easy way to do it .. I'd be interested to hear any other answer(s)
(usually where I work we KNOW its EBCDIC because of the data source/trading agreement or spec between us and a third party)
'g'
edit : Another probable giveaway - if you suspect there's a number of spaces, you may be able to count the number of 0x20 ASCII space or 0x40 EBCDIC spaces ...
|
|
|
|
|
I was hoping to find something similar to the Win32 function IsTextUnicode(), but haven't had any luck. Microsoft has very little support for EBCDIC. It looks like some kind of heuristic as you suggest is what I'll have to do.
|
|
|
|
|
Have you checked Joe Newcomer's site? He's got some EBCDIC-related stuff on it."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
|
|
|
|
|
Hi Hans,
strange question. I haven't seen any EBCDIC in the last 30 years or so, and I think I would know when to expect it. But anyway, these are ways I would consider given some data that is supposed to be a text using either EBCDIC or ASCII:
1. binary read the first 1K bytes of it, and count ASCII letters and EBCDIC letters. As their codes are quite different, one of those must clearly outnumber the other.
2. seems like EBCDIC letters are >=128 (see http://www.astrodigital.org/digital/ebcdic.html[^]), so just looking at the ratio of number below and numbers above 128 could be quite sufficient.
3. alternative: if you are expecting some keywords, you might just search for those in either code (still using binary of course)
BTW: if the file starts with a byte-order-mask indicating UTF8 or Unicode, it won't be EBCDIC.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that. All Toronto weekends should be extremely wet until we get it automated in regular forums, not just QA.
|
|
|
|
|
good answer Luc - I was going to suggest that if he had characters in 0-127 range 'likely' he could assume ascii .. I was trying to think about the BOM, glad you had a thought.
Luc Pattyn wrote: I haven't seen any EBCDIC in the last 30 years or so, and I think I would know when to expect it.
welcome to my world - as I said to Hans, 'most of the time' your trading spec/agreement with a third party (generally sitting behind a mutha mainframe) lets you know when to expect it, and then again, sometimes not
'g'
|
|
|
|
|
Hi Luc,
Yes, I think you are right about counting letters. I'm not sure about the Unicode part, though. Apparently there is now something called UTF-EBCDIC. I'll have to read up on that.
Thanks, guys.
|
|
|
|
|
You have my profound sympathies having to deal with EBCDIC. In my industry (printing), it's a complete PITA: everybody uses a different encoding, and not even IBM can agree with itself on code pages.
|
|
|
|
|
Gary R. Wheeler wrote: You have my profound sympathies having to deal with EBCDIC. In my industry (printing), it's a complete PITA: everybody uses a different encoding, and not even IBM can agree with itself on code pages.
Agree with that. Same in communications...
|
|
|
|
|
Hi All,
I am using MAPIEx Libraries in my MFC application.
I am trying to replicate "New Mail Message" from my MFC application so that new outlook message opens up when user clicks a button from my application and gives him a chance to send the message.
It works fine when outlook is online/connected to server.
When i remove the network, the new message dosent come up and he library failes at m_pSession->OpenMsgStore
If any one encountered a similar issue or tried to use MAPIEx libraries to bring up new message when outlook is offline, please give me some inputs.
Thanks
Kasi.
|
|
|
|
|
I am having problem with communicating with 3M MT7 EX USB touch screen.
I need to send a calibrate request through C++ code, in order to do that I locate the global device path from SetupDi* functions. The global device path returned is:
\\?\USB#Vid_0596&Pid_0001#5&ee85354&0&2#{a5dcbf10-6530-11d2-901f-00c04fb951ed}
Using this device path I successfully get a handle from CreatFile():
<code> m_hUsbDevice = CreateFile(detailData->DevicePath, GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, (LPSECURITY_ATTRIBUTES)NULL, OPEN_EXISTING, 0, NULL);</code>
When I try writing to this handle I get an error:
ERROR_INVALID_FUNCTION
The code for writing is:
<code>WriteFile(m_hUsbDevice, pucString, 8 , &dwWritten, 0)</code>
pucString is pointer to the below structure
<code>struct stCalibrate
{
Byte RequestType;
Byte Request;
short Value;
short Index;
short Length;
stCalibrate()
{
RequestType = 0x40;
Request = 4;
Value = 0x01;
Index = 0;
Length = 0;
}
};</code>
|
|
|
|
|
|
what does this have to do with C, C++, or MFC?
|
|
|
|
|
Since you're comparing outtrainset(i,j) to 0.5, a floating point value, I'd assume just like I told you before that you already have a continuous value as output. As I also said, that if statement is thresholding the output to prodice a crisp boolean answer. If you want the continuous result, just don't do the threshold!
Also, peppering the forum with repeated questions is considered bad form and will generally get you yelled at.You measure democracy by the freedom it gives its dissidents, not the freedom it gives its assimilated conformists.
|
|
|
|
|
Oh, I remember you now. You're the Malaysian gal who was trying to get some fool to send her the code for her senior project a while ago. Guess you must have found one since you can't comprehend what you have. Maybe I'll report you to your school's acadamic affairs office. You measure democracy by the freedom it gives its dissidents, not the freedom it gives its assimilated conformists.
|
|
|
|
|
What, you've got mental problems? Steve
|
|
|
|
|
Hi All
I want to retrive the information for Last Used On of Installed Application of Computer. please give me idea how i retrive the information LAST USED ON and Freequency Just like Add/Remove Program . we can see all the information.in control panel so we can also retrive the information. So if any one idea then please share it. so please help me. You can also provide me where is Stored the LAST USED ON and FREQUENCY information. I am waiting Your quick response.
Thanks
Rakesh
|
|
|
|
|
|
I believe 'Add/Remove Program' which has the 'LAST USED ON' column enabled utilizes the NTFS Last Access timestamp[^]
Best Wishes,
-David Delaune
|
|
|
|