c + + an interesting little program ---- this pointer and polymorphism doubts

Category: C/C++ -> C++ Author: yixue17 Date: 2006-09-20 09:27:58
 
yixue17
2006-09-20 09:27:58
/ / Comments are added according to their own understanding, do not know right

# include <IOSTREAM.H>

class Animal
{
public:
Animal * a;

Animal ()
{
this-> breath ();
a = ; this ;/ / here is this, if there is a subclass to instantiate an object,
/ / then that this will point to the subclass object (or point to the first address of the parent class)

}

Animal * getAniaml ()
{
Animal an;
return a;
/ / return this ;/ / This method is called that object is returned it this
}

virtual void breath ()
{
cout << "Animal breath" ; << endl;
}

};

class Fish: public Animal
{
public :
Fish ()
{
/ / this-> breath ();
}
void breath ()
{
cout << "Fish breath" << endl;
}
};

Fish f;

void main ()
{
f.getAniaml () -> breath ();
}

# ------------- --- output ------------------
Animal breath
Animal breath
Fish breath

I used to debug a one debugging, still can not understand the "a" value of the address change process and the final output. marked in red are key sentence reasonable to say that the second call Animal constructor, will once "a" value coverage, Animal * getAniaml () returns a pointer pointing Animal objects, resulting in f.getAniaml () -> breath () will be called when the Animal breath () method, can result is called Fish's breath () method, I can not understand!!!
book_superb
2006-09-20 09:30:40
how would have done one of f ah, one is an a.
certainly not overwritten.
a is that each object has a.
aizaimengzhong
2006-09-20 09:45:50

modify a pointer member does not affect this pointer. Through which object called getAniaml () so that the inside of a pointer to an object on the preservation of the object's address. This is the only.
xzlszh
2006-09-20 10:03:05
From C + + Proverbs:
 , , 。  Java   C#  , ,  C++  , 。 

   , , , 。 ,  Transaction  , 。 :

class Transaction { // base class for all
 public: // transactions
  Transaction();

  virtual void logTransaction() const = 0; // make type-dependent
  // log entry
  ...
};

Transaction::Transaction() // implementation of

 // base class ctor
 ...
 logTransaction(); // as final action, log this
} // transaction

class BuyTransaction: public Transaction { 
 // derived class
 public:
  virtual void logTransaction() const; // how to log trans-
  // actions of this type
  ...
};

class SellTransaction: public Transaction { 
// derived class
public:
 virtual void logTransaction() const; // how to log trans-
 // actions of this type
...
}; 

   :

BuyTransaction b; 

    BuyTransaction  , ,Transaction  , 。Transaction   logTransaction, ,  logTransaction   Transaction  ,  BuyTransaction  ——  BuyTransaction。 , (go down) 。 , 。 , , 。  。 , , 。 (go down) , , 。 。 ,  C++  。

   。 , 。 , (runtime type information) ( ,dynamic_cast  typeid), 。 ,  Transaction   BuyTransaction  ,  Transaction。C++  , :  BuyTransaction  , 。 , 。

   。 , ,  C++  。 , ,C++  —— ,dynamic_casts  —— 。

   ,Transaction  ,  Item  。 , 。( ) , ,  logTransaction   Transaction  。 ( , ), :  Transaction::logTransaction  。

   。  Transaction  , , , ,  logTransaction  , ,  init:

class Transaction {
public:
 Transaction()
 { init(); } // call to non-virtual...

 virtual void logTransaction() const = 0;
 ...

private:
 void init()
 {
  ...
  logTransaction(); // ...that calls a virtual!
 }
}; 

   , , 。 ,  logTransaction   Transaction  , , ( )。 ,  logTransaction  “ ” ( , ),  Transaction  , , ,  logTransaction  。 , , 。

   ,  Transaction  ,  logTransaction  ? ,  Transaction  。

   。  Transaction   logTransaction  ,  Transaction  。  logTransaction。 :

class Transaction {
public:
 explicit Transaction(const std::string& logInfo);

 void logTransaction(const std::string& logInfo) const; // now a non-
 // virtual func
 ...
};

Transaction::Transaction(const std::string& logInfo)
{
 ...
 logTransaction(logInfo); // now a non-
} // virtual call

class BuyTransaction: public Transaction {
public:
 BuyTransaction( parameters )
 : Transaction(createLogString( parameters )) // pass log info
 { ... } // to base class
 ... // constructor

private:
 static std::string createLogString( parameters );
}; 

   , , 。  ,  BuyTransaction  ( )static   createLogString  。 , ( )。  static,  BuyTransaction  。 , , 。

  Things to Remember

  · , 。 
z892157421
2006-09-20 10:12:46
Fish compiler knows that f is an instance of the runtime type identification. Polymorphic ah.
danli110
2006-09-20 10:31:55
Why will overwrite the first time a? Are not static

PS: Do constructor and destructor calls a virtual function
hahatiancai
2006-09-20 10:42:20
1. Never call a virtual function inside the constructor.
2.Animal an; return a ;/ / or the creation of an object does not affect your global object through f this function is called when the this pointer which contains address. Who call a member function then this pointer contains the address of anyone. As you create and then the member function inside an object to be irrelevant.
xiaozhenmiss
2006-09-20 10:55:36
this pointer is a pointer constant. Animal * const this;
AIR8400
2006-09-20 11:02:52

is polymorphic, but why in the second after a call to Animal constructor will be reassigned (pointing to an of this), but return when it is The first point of f this, puzzled ~ ~
drvsoft
2006-09-20 11:15:52


call a virtual function is to test Down Under ~ ~
little understood, that in the first Secondary Animal an; calling Animal constructor, although a is pointing to an assignment of this, but the return is the first time a point of f this, strange approach ah! ~ ~
guigubingceo
2006-09-20 11:28:24
I fainted.
Animal * getAniaml ()
{
Animal an ;/ / here you generate a an, then this object is indeed a point inside it, but the inside with a The outside is a different one
return a ;/ / like you wrote the code, is a global one is local. out of the top function, you must be using a global ah.
/ / return this ;/ / This method is called that object is returned it this
}
uuzjh
2006-09-20 11:42:19

amount, you still understand, an object of this this pointer and f objects this pointer two completely different when you are going through this object to call f
getAniaml () then this function inside the this pointer points to f the object inside the function will never create of an thus pointing an. If the function inside a member function through an invocation, then the call to this member function pointer is an. Two is to take the industry.
zhyqbbs
2006-09-20 11:59:36
If you still do not understand then you look at me 5L give you tips it.
czblike66
2006-09-20 12:13:11


you mean to say that an is a local variable, out of the top of this constructor it will disappear? but its this is saved to a Lane ah, a The scope should be able to cover return a;, right?
yif
2006-09-20 12:15:36


That is to say in the instance of an object, each object will retain a copy constructor copy myself?
wslixd
2006-09-20 12:22:52
can be understood it, so being the first to understand it.
jiangxiaotc
2006-09-20 12:35:35


But Animal * getAniaml () method does not involve in to this ah? it just returns an address to save this a just ...
laiguanqu
2006-09-20 12:52:40
Fish first instantiated object f, first call the base class constructor, output an Animal beath, then call getAniaml (), the execution Animal an, note this is not an object pointer, so here again call Animal constructor output again Animal beath, finally calling the breath (), due to the multi-state relations, it will produce Fish beath.
tureyy
2006-09-20 13:08:53
each class with virtual functions has its own virtual table, whether it is the parent class or sub-class; subclass inherits the parent class's virtual Table; into each instance of the class constructor, it will change the virtual pointer to the instance of the virtual table;
Animal in the parent class constructor, Fish f instance of the virtual pointer to the parent class Animal virtual table (the virtual table only Animal :: breath address); while entering Fish constructor, the virtual pointer is modified to point to the Fish class virtual table (this table has two virtual functions virtual address is in the front Fish :: breath () address, followed behind the Animal :: breath () address);

so in the parent class's constructor calls breath (); is to call the parent class Animal :: breath ()
in the subclass constructor calls breath (); is called sub-category Fish :: breath ()

f.Animal :: breath ( ); vc6.0 a viable, will be forced to call the parent class Animal :: breath (), Why can call that a subclass inherits the parent class's virtual table;
This seems to illustrate Fish subclass virtual table contains two function addresses Fish :: breath () and the Animal :: breath ()
virtual table which shows the inheritance

idler_zhao
2006-09-20 13:17:00
Landlord entangled in a a This variable, in fact, each object has its own a, in getAniaml, although an also called constructor modifies a a, but this is an own a and does not affect the getAniaml return back to that a, if a is set to static, then all Aniaml objects share a a, who modified it values, the other a re-visit this is the new value of the.

Page 1 of 2    «  »