I am so confused with the output of the following code:
#include <iostream> using namespace std; class Parent { public: Parent() : x(2) { } virtual ~Parent() { } void NonVirtual() { cout << "Parent::NonVirtual() x = " << x << endl; } private: int x; }; class Child : public Parent { public: Child() : x(1) { } virtual ~Child() { } void NonVirtual() { cout << "Child::NonVirtual() x = " << x << endl; } private: int x; }; int main() { Child c; Parent* p = &c; c.NonVirtual(); // Output: Child::NonVirtual() x = 1 p->NonVirtual(); // Output: Parent::NonVirtual() x = 2 // Question: are there two x'es in the Child object c? // where is x = 2 from (we have not defined any Parent object)? cout << sizeof(c) << endl; cout << sizeof(*p) << endl; return 0; }
, ++ , virtual, overriden. , , , . runtime type, , , , compile- (.. ). p Parent *, Parent, c Child, , Child. virtual, , Child.
virtual
, x... , "protected" ( , , ). x, Child, , , . , x private Parent, x Child, x Child.
. , , , .
Parent * p , . , , , , .
, ( ), . , , .
, , , P P, C, .
Source: https://habr.com/ru/post/1756399/More articles:Searching for a vector opposite to another? - c ++Sanitary entrance from irc - securityhttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1756396/does-html5-require-clients-to-support-javascript&usg=ALkJrhjnyl_wTeMdE04zPt_EkatTBKi2RgIs there any elegant way in LINQ to create a collection in a collection of lists based on a property - collectionsHow to align a switch in android - androidgive MySQL or PHP work? - performancePeriodically check if Android Android has new data - performanceImmutable Object in Objective-C: Big init Method? - immutabilityjQuery Advance Selector - jqueryиспользование разреза для циклических данных - rAll Articles