|
Let`s say I have a cocktail of objects of different type but having the same base class. Would it be possible to store the cocktail into a C++ array rather then a stl container?
class soldier : public unit
{
public:
void RunSM1();
};
unit* LUnits;
int lunitcount = 2;
int main()
{
LUnits = new unit[lunitcount];
LUnits[0].Init();
soldier * S = new soldier();
&LUnits[1] = S;
LUnits[1] = *S;
return 0;
}
modified 8-Aug-24 7:01am.
|
|
|
|
|
Yes, it is possible. Moreover, it is very common to do that.
When you access the objects you can convert a pointer to base to the derived type using either dynamic_cast or a specialized type function:
Soldier *s = dynamic_cast<Soldier*>(LUnits[0]);
if (!s)
printf("Element 0 is not a soldier\n");
s = dynamic_cast<Soldier*>(LUnits[1]);
if (s)
printf ("Element 1 is indeed a soldier\n");
Mircea
|
|
|
|
|
|
Yes, it is possible with correct casting, if you are just storing pointers. But you still need to know what type each entry is next time you refer to them. Also the line &LUnits[1] = S; is incorrect, you do not need the leading & character. So you can store them with something like:
soldier * S = new soldier();
LUnits[0] = dynamic_cast<unit*>(S);
sailor* AB = new sailor();
LUnits[1] = dynamic_cast<unit*>(AB);
sailor* tar = dynamic_cast<sailor*>(LUnits[1]);
soldier* squaddie = dynamic_cast<soldier*>(LUnits[1]);
[edit]
@Mircea-Neacsu provides a clearer explanation above.
[/edit]
modified 8-Aug-24 8:51am.
|
|
|
|
|
Let me respectfully comment:
soldier * S = new soldier();
LUnit[0] = S;
sailor* AB = new sailor();
LUnit[1] = AB;
sailor* tar = dynamic_cast<sailor*>(LUnits[1]);
soldier* squaddie = dynamic_cast<soldier*>(LUnits[1]);
For details see: dynamic_cast conversion - cppreference.com[^]
Mircea
|
|
|
|
|
Thank you, I was working from memory instead of reviewing some code I wrote years ago.
|
|
|
|
|
I need to know how to execute the conversion in both directions, I find your post still useful.
modified 8-Aug-24 11:57am.
|
|
|
|
|
Thank you for your comment.
|
|
|
|
|
I have a window containing some data. I need to add functionality to add notes anywhere in window. Please guide.
|
|
|
|
|
You need to provide considerably more details of the code you are using and exactly what you are trying to do. As it stands it is impossible to suggest anything useful.
|
|
|
|
|
Anywhere means nothing.
What do you want to do, exactly ?
What is or what kind of data in your window ?
How is it represented ?
What kind of notes you want to add ?
What relation has the note and the data ?
Do you have an example of something similar you want to do ?
Without more details...
I'd handle a click or double click event, open a modal popup window with a text box, and on the OK button, save the note whereever you want to save the note.
CI/CD = Continuous Impediment/Continuous Despair
|
|
|
|
|
Why don't the enumeration objects get recognized unless the enumeration is defined in the same header file where the enumeration object is created? If the enumeration is in a different header file I get an
"Error C3646 'PC': unknown override specifier " error. I`m including the header file where enumeration is defined in the header where the enum object is created.
|
|
|
|
|
|
Thank you Victor. I will change the object name to lower caps and see if the error goes away
|
|
|
|
|
Since it is demonstrated that the community has no (technical) interest, or perhaps lacks knowledge in helping in resolving the issue , hence this post, as is , has no change to be resolved here and is also unwelcomed here ,
<b>it is therefore voluntarily removed.</b>
|
|
|
|
|
jana_hus wrote: This post is here because I should be able to ask,
Wrong!!! YOU don't get to dictate what you can or cannot post here. That's for the community and moderators to decide. This forum is specifically for standardC/C++ (with the addition of MS MFC), because that's what the moderators wanted. There's no mention of QT, or WxWidgets or any other C++ frameworks.
As I said in one of my earlier posts, get a copy Stroustrup: The C++ Programming Language (4th Edition) If your question can't be covered by the material therein, then ITS NOT A C++ QUESTION!!!. I only have the 2nd edition of that book, and QT isn't covered at all. I'm sure that it's not covered in the 4th edition either. So, as always, this question belongs in a forum that's dedicated to QT and its use, not here!
"A little song, a little dance, a little seltzer down your pants"
Chuckles the clown
|
|
|
|
|
The community IS the moderator.
|
|
|
|
|
jana_hus wrote: This post is here because I should be able to ask,
if it bothers you - DO NOT REPLY. Thanks.
Oh look, it's another Salvatore Terress.
|
|
|
|
|
|
Hi,
In my dialog i followed the link below to solve the flicker issue. It worked.
I am continuously capturing and updating an area of dialog.
currently, i do:
Invalidate()
UpdateWindow()
is there a faster way to repaint?. like RedrawWindow(), etc. Are they handled by the wm_paint?.
https://www.codeproject.com/Articles/33/Flicker-Free-Drawing-In-MFC
modified 3-Aug-24 19:20pm.
|
|
|
|
|
2 Things:
1) Please don't repost if your question does not appear immediately: all of these went to moderation and required a human being to review them for publication. In order to prevent you being kicked off as a spammer, both had to be accepted, and then I have to clean up the spares. Have a little patience, please!
I've deleted the 3 identical spares ...
2) Don't post this under Quick Answers - if you got the code from an article, then there is a "Add a Comment or Question" button at the bottom of that article, which causes an email to be sent to the author. They are then alerted that you wish to speak to them.
Posting this here relies on them "dropping by" and realising it is for them.
However, given that the article is 22 years old it's unlikely that anyone is going to know or remember details of how it works!
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
I'm using a CIPAddressCtrl which gets filled with a device's IP. The user can edit this IP and I need to make sure none of the fields (octets) are blank if the user wants to update the device with the new IP. If any are blank and the user tries to update (with a click of a button), then there will be an error.
I see there is a SetFieldFocus that can set the focus if an octet is blank, but I'm not sure if it's useful for what I need or how to use it in my case. Is there any work around to achieve this?
Thanks.
|
|
|
|
|
In order to validate the use input, call the GetAddress method and check its return value. See the documentation: CIPAddressCtrl Class | Microsoft Learn[^].
"In testa che avete, Signor di Ceprano?"
-- Rigoletto
|
|
|
|
|
Tried that already. GetAddress doesn't tell you whether or not an octet is blank. If you do GetAddress and get the four octets and one of them is blank, then it's just a zero. However, it could also mean that the user could have entered zero.
The IPM_GETADDRESS message returns the number of non-blank fields so that'll probably work for me unless anyone has any other ideas.
|
|
|
|
|
DWORD dwIPAddr;
iNonBlankOctets = ::SendMessage(hWnd, IPM_GETADDRESS, 0, (LPARAM)&dwIPAddr);
This worked for me.
|
|
|
|