|
From: Lorry Astra on 13 Apr 2008 21:10 Question 1.-------------------------------------------------------------------------------------------------------------------------------------------- #include <iostream> using namespace std; class B { public: int i; B() {} }; class A { public: A(B& b1) { b1.i=0; } }; class C : public A { B b; public: C() : A(b) {} }; As the code above, b is a private object for class C. Why I can revise b as a reference in constructor of class A. As I know, even class C herites from class A, A can not revise any private properties in C, is that right? Question 2.-------------------------------------------------------------------------------------------------------------------------------------------- class A { public: A() {} friend ostream& operator << (ostream& os,A& ac) {} }; If I remove keyword "friend", operator << will be seen as a bit operator instead of a stream operator, why? (I think If I add keyword "friend" before the function declaration, it just means this function can revise private properties in class A, the keyword can not effect whether "<<" is a bit operator or not.) Question 3.-------------------------------------------------------------------------------------------------------------------------------------------- Sorry,I write ASM in C++ newsgroup, but it's related with my question. Coorder STRUCT X WORD ? Y WORD ? Coorder ENDS m_order Coorder <0,0> It's about storage space in C++ thinking from assembly language (IA 32 instruction set) ASM "STRUCT" itself(I mean declaration) will not be stored in storage space, but for ASM "STRUCT Variant"(m_order) it will live in storage space at runtime. (I don't know whether it's right.) this description let me think about "class" in C++. Can I say: the declaration of a "class" will not be stored in storage space? if what I said is right, how C++ compiler deals with inline function if "class" is not in storage space. Thank you for your help.
From: Igor Tandetnik on 13 Apr 2008 23:52 "Lorry Astra" <LorryAstra(a)discussions.microsoft.com> wrote in message news:41571945-5DC8-434E-97A5-A7BCE7125DA5(a)microsoft.com > #include <iostream> > using namespace std; > > class B > { > public: > int i; > > B() > {} > }; > > class A > { > public: > A(B& b1) > { > b1.i=0; > } > }; > > class C : public A > { > B b; > public: > C() : A(b) > {} > }; > > As the code above, b is a private object for class C. Why I can > revise b as a reference in constructor of class A. Accessibility controls access to names, not to objects. The same object can be referred to by many different names or expressions (known as aliasing), some of which may be public, others private, and still others not members at all. In your example, the name C::b is private to class C. But the name b1 (a parameter of A's constructor) is not a data member of any class, and is certainly accessible inside the body of the constructor. The constructor doesn't know nor care that the name refers to an object that happens to be a private member of some class. > As I know, even class C herites from class A, A can not revise any > private properties in C, is that right? A cannot look inside C and access its private members. But C can look inside itself and give others access to its internals if it so chooses. > class A > { > public: > A() > {} > > friend ostream& operator << (ostream& os,A& ac) > {} > }; > > If I remove keyword "friend", operator << will be seen as a bit > operator instead of a stream operator, why? If you remove "friend" keyword in this case, the code won't even compile. With the "friend" keyword, you declare a non-member function (that happens to be a friend of the class). Without the keyword, the same declaration would (attempt to) declare operator<< as a member function of class A. But a member function form of operator<< must take only one parameter, not two. > this description let me think about "class" in C++. Can I say: the > declaration of a "class" will not be stored in storage space? Quite. Types don't occupy storage: objects do. > if what > I said is right, how C++ compiler deals with inline function Formally, functions are not objects, and C++ language doesn't place any requirements on how they are represented in memory (if at all). In practice, of course, code occupies some memory - whether it resides in an inline member function, non-inline member function, nor a non-member function (inline or otherwise). -- With best wishes, Igor Tandetnik With sufficient thrust, pigs fly just fine. However, this is not necessarily a good idea. It is hard to be sure where they are going to land, and it could be dangerous sitting under them as they fly overhead. -- RFC 1925
|
Pages: 1 Prev: Array of char pointers. Next: How to restrict inheritance in C++ |