Click here to Skip to main content
15,991,287 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: System.* not defined or imported Pin
Pieter Claassens15-Aug-24 4:17
Pieter Claassens15-Aug-24 4:17 
QuestionIs it possible to find a shortlist of Bios registers and methods to use them? C/ASM programming Pin
Alex D Aug202414-Aug-24 13:04
Alex D Aug202414-Aug-24 13:04 
AnswerRe: Is it possible to find a shortlist of Bios registers and methods to use them? C/ASM programming Pin
jschell14-Aug-24 13:53
jschell14-Aug-24 13:53 
AnswerRe: Is it possible to find a shortlist of Bios registers and methods to use them? C/ASM programming Pin
Dave Kreskowiak14-Aug-24 16:29
mveDave Kreskowiak14-Aug-24 16:29 
QuestionInheritance and STL containers Pin
Calin Negru12-Aug-24 3:55
Calin Negru12-Aug-24 3:55 
AnswerRe: Inheritance and STL containers Pin
Richard MacCutchan12-Aug-24 4:52
mveRichard MacCutchan12-Aug-24 4:52 
GeneralRe: Inheritance and STL containers Pin
Calin Negru12-Aug-24 5:42
Calin Negru12-Aug-24 5:42 
AnswerRe: Inheritance and STL containers Pin
Mircea Neacsu12-Aug-24 6:38
Mircea Neacsu12-Aug-24 6:38 
Let me expand a bit on my previous answer[^].

As I said, you can convert a pointer (or reference) to pointer (or reference) to the base class. This called "upcasting" because you are moving "up" in the inheritance tree. The conversion is "non-destructive", in other words you can later on "downcast" (move from base to derived) as long as you know what type of object was in the first place. An example:
C++
struct Unit {
  Unit (int tag) : dog_tag(tag){};
  virtual std::string kind () = 0;
  int dog_tag;
};

struct Soldier : public Unit {
  Soldier (int tag) : Unit(tag) {};
  virtual std::string kind() {return "soldier";};
  float running_speed;
}

struct Sailor : public Unit {
  Sailor (int tag) : Unit(tag) {};
  virtual std::string kind() {return "sailor";}
  int life_jackets;
};

std::vector<Unit*> actors {new Soldier(555), new Sailor(666);};

int main () {
  Unit* u0 = actors[0];
  std::cout << "Actor 0 is a " << u0.kind() " with tag " << u0.dog_tag << std::endl;

If you would look with a debugger at u0 you would see a memory layout something like this:
   --------          ------------------------------
u0| 0x1234 |------->| pointer to vtable of Soldier |
   --------         |------------------------------|
                    | dog_tag                      |
                    | -----------------------------|
                    | running_speed                |
                     ------------------------------

You can see now why the code works: no matter what kind of object you deal with, the first part of the memory layout is the same. Compiler simply ignores whatever is after the dog_tag field.

A few more lines of code (please ignore that I didn't initialize other fields):
C++
Soldier *s = (Soldier *)u0;
std::cout << "Running speed " << s->running_speed << std::endl;
s has exactly the same value as u0 but compiler considers it a pointer to a Soldier object so it uses the value from the running_speed field. I could have written:
C++
Sailor *ss = (Sailor*)u0;
std::cout << "Sailor has " << ss->life_jackets << " life jackets";
And compiler wouldn't have cared at all. It would have accessed the memory at life_jackets location as an integer and this is that.

Now, what happens if I use dynamic_cast instead of C-style casts?
C++
Sailor *ss = dynamic_cast<Sailor*>(u0)
The generated code would have used the value stored in the vtable member to check if the 0x1234 truly is a Sailor object. As the vtable value doesn't match the address of vtable for Sailor objects, the dynamic cast operation would have returned a NULL pointer.

Things to remember from this story:
- upcast is always safe. Downcast not so much.
- dynamic_cast is safer than C-style cast provided you check the result.
- dynamic_cast works only for classes that have at least one virtual function. If your object doesn't have a virtual table compiler cannot figure out what type of object it really is.
Mircea

QuestionSOLVED EDIT / BUMP Qt QtCreator "connect" - need help with lambda C++ syntax Pin
jana_hus10-Aug-24 7:12
jana_hus10-Aug-24 7:12 
AnswerRe: Qt QtCreator "connect" - need help with lambda C++ syntax Pin
RedDk10-Aug-24 9:47
RedDk10-Aug-24 9:47 
AnswerRe: Qt QtCreator "connect" - need help with lambda C++ syntax Pin
Mircea Neacsu10-Aug-24 9:53
Mircea Neacsu10-Aug-24 9:53 
AnswerRe: SOLVED EDIT / BUMP Qt QtCreator "connect" - need help with lambda C++ syntax Pin
Richard MacCutchan10-Aug-24 21:19
mveRichard MacCutchan10-Aug-24 21:19 
QuestionDELETED Please help me (with) improving my coding skill(s) - how to access iterated object. Pin
jana_hus8-Aug-24 6:41
jana_hus8-Aug-24 6:41 
SuggestionRe: Please help me (with) improving my coding skill(s) - how to access iterated object. Pin
David Crow8-Aug-24 7:37
David Crow8-Aug-24 7:37 
QuestionInheritance and arrays Pin
Calin Negru8-Aug-24 0:49
Calin Negru8-Aug-24 0:49 
AnswerRe: Inheritance and arrays Pin
Mircea Neacsu8-Aug-24 0:58
Mircea Neacsu8-Aug-24 0:58 
GeneralRe: Inheritance and arrays Pin
Calin Negru8-Aug-24 4:28
Calin Negru8-Aug-24 4:28 
AnswerRe: Inheritance and arrays Pin
Richard MacCutchan8-Aug-24 1:01
mveRichard MacCutchan8-Aug-24 1:01 
GeneralRe: Inheritance and arrays Pin
Mircea Neacsu8-Aug-24 2:31
Mircea Neacsu8-Aug-24 2:31 
GeneralRe: Inheritance and arrays Pin
Richard MacCutchan8-Aug-24 2:50
mveRichard MacCutchan8-Aug-24 2:50 
GeneralRe: Inheritance and arrays Pin
Calin Negru8-Aug-24 4:30
Calin Negru8-Aug-24 4:30 
GeneralRe: Inheritance and arrays Pin
Richard MacCutchan8-Aug-24 4:34
mveRichard MacCutchan8-Aug-24 4:34 
QuestionNotes in MFC/Win32 App Pin
john56326-Aug-24 4:34
john56326-Aug-24 4:34 
AnswerRe: Notes in MFC/Win32 App Pin
Richard MacCutchan6-Aug-24 4:36
mveRichard MacCutchan6-Aug-24 4:36 
AnswerRe: Notes in MFC/Win32 App Pin
Maximilien6-Aug-24 7:38
Maximilien6-Aug-24 7:38 

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.